2023-05-18 20:04:55 -06:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"embed"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
2024-06-27 21:23:51 -06:00
|
|
|
"log"
|
2023-05-18 20:04:55 -06:00
|
|
|
"net/http"
|
2024-06-27 21:23:51 -06:00
|
|
|
"os"
|
2023-05-18 20:04:55 -06:00
|
|
|
|
2024-06-27 13:13:46 -06:00
|
|
|
"pollo/api"
|
|
|
|
"pollo/api/webhooks"
|
|
|
|
"pollo/lib"
|
|
|
|
"pollo/pages"
|
2023-05-18 20:04:55 -06:00
|
|
|
|
|
|
|
"github.com/joho/godotenv"
|
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
"github.com/labstack/echo/v4/middleware"
|
|
|
|
)
|
|
|
|
|
|
|
|
//go:embed public/*
|
|
|
|
var PublicFS embed.FS
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
// Load environment variables
|
2024-11-21 00:48:45 -06:00
|
|
|
err := godotenv.Load()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Warning: Error loading .env file: %v", err)
|
2024-06-27 21:23:51 -06:00
|
|
|
}
|
|
|
|
|
2024-11-21 00:48:45 -06:00
|
|
|
// Initialize database connection
|
|
|
|
dbUrl := os.Getenv("DB_URL")
|
|
|
|
dbToken := os.Getenv("DB_TOKEN")
|
|
|
|
|
|
|
|
if dbUrl == "" || dbToken == "" {
|
|
|
|
log.Fatal("DB_URL and DB_TOKEN environment variables must be set")
|
2024-10-16 21:14:19 -06:00
|
|
|
}
|
|
|
|
|
2024-11-21 00:48:45 -06:00
|
|
|
// Initialize the database
|
|
|
|
if err := lib.InitializeDB(dbUrl, dbToken); err != nil {
|
|
|
|
log.Fatalf("Failed to initialize DB: %v", err)
|
2024-06-27 21:23:51 -06:00
|
|
|
}
|
|
|
|
|
2024-11-21 00:48:45 -06:00
|
|
|
// Initialize schema
|
|
|
|
if err := lib.InitializeSchema(); err != nil {
|
2024-06-27 21:23:51 -06:00
|
|
|
log.Fatalf("Failed to initialize schema: %v", err)
|
|
|
|
}
|
2023-05-18 20:04:55 -06:00
|
|
|
|
2024-07-12 01:28:59 -06:00
|
|
|
// ------------------------------
|
|
|
|
// Middlewares:
|
|
|
|
// ------------------------------
|
|
|
|
logMiddleware := middleware.Logger()
|
|
|
|
recoverMiddleware := middleware.Recover()
|
|
|
|
requestIDMiddleware := middleware.RequestID()
|
|
|
|
securityMiddleware := middleware.Secure()
|
|
|
|
gzipMiddleware := middleware.GzipWithConfig(middleware.GzipConfig{
|
|
|
|
Level: 5,
|
|
|
|
})
|
|
|
|
rateLimitMiddleware := middleware.RateLimiter(middleware.NewRateLimiterMemoryStore(50))
|
|
|
|
removeTrailingSlashMiddleware := middleware.RemoveTrailingSlash()
|
|
|
|
|
|
|
|
// Initialize Echo router route groups
|
2023-05-18 20:04:55 -06:00
|
|
|
e := echo.New()
|
2024-07-12 01:28:59 -06:00
|
|
|
e.Pre(removeTrailingSlashMiddleware)
|
|
|
|
e.Use(logMiddleware)
|
|
|
|
e.Use(recoverMiddleware)
|
|
|
|
e.Use(requestIDMiddleware)
|
|
|
|
e.Use(securityMiddleware)
|
|
|
|
e.Use(rateLimitMiddleware)
|
|
|
|
e.Use(lib.InitSessionMiddleware())
|
|
|
|
|
|
|
|
publicPageRoute := e.Group("", gzipMiddleware)
|
|
|
|
protectedPageRoute := e.Group("", lib.AuthenticatedPageMiddleware, gzipMiddleware)
|
|
|
|
authFlowPageRoute := e.Group("", lib.AuthFlowPageMiddleware, gzipMiddleware)
|
2024-07-05 23:14:21 -06:00
|
|
|
publicApiRoute := e.Group("/api")
|
|
|
|
protectedApiRoute := e.Group("/api", lib.AuthenticatedEndpointMiddleware)
|
|
|
|
webhookGroup := e.Group("/webhook")
|
2023-05-18 20:04:55 -06:00
|
|
|
|
2024-07-05 23:14:21 -06:00
|
|
|
// ------------------------------
|
|
|
|
// Static Server:
|
|
|
|
// ------------------------------
|
2023-05-18 20:04:55 -06:00
|
|
|
fs := http.FS(PublicFS)
|
2024-02-21 01:46:30 -07:00
|
|
|
e.GET("/public/*", echo.WrapHandler(http.FileServer(fs)))
|
2023-05-18 20:04:55 -06:00
|
|
|
|
2024-07-05 23:14:21 -06:00
|
|
|
// ------------------------------
|
|
|
|
// Page Routes:
|
|
|
|
// ------------------------------
|
|
|
|
publicPageRoute.GET("/", pages.Home)
|
2024-07-09 11:10:32 -06:00
|
|
|
authFlowPageRoute.GET("/signin", pages.SignIn)
|
|
|
|
authFlowPageRoute.GET("/register", pages.Register)
|
2024-07-05 23:14:21 -06:00
|
|
|
protectedPageRoute.GET("/dashboard", pages.Dashboard)
|
|
|
|
protectedPageRoute.GET("/room/:id", pages.Room)
|
2023-05-18 20:04:55 -06:00
|
|
|
|
2024-07-05 23:14:21 -06:00
|
|
|
// ------------------------------
|
2023-05-18 20:04:55 -06:00
|
|
|
// API Routes:
|
2024-07-05 23:14:21 -06:00
|
|
|
// ------------------------------
|
|
|
|
// Generic API routes (public)
|
|
|
|
publicApiRoute.GET("/ping", api.Ping)
|
|
|
|
publicApiRoute.GET("/sse", func(c echo.Context) error {
|
2024-06-04 15:40:29 -06:00
|
|
|
return api.SSE(c)
|
2023-05-18 20:04:55 -06:00
|
|
|
})
|
2024-07-05 23:14:21 -06:00
|
|
|
|
|
|
|
// Auth routes (public)
|
|
|
|
publicApiRoute.POST("/register", api.RegisterUserHandler)
|
|
|
|
publicApiRoute.POST("/signin", api.SignInUserHandler)
|
|
|
|
publicApiRoute.POST("/signout", api.SignOutUserHandler)
|
|
|
|
|
|
|
|
// Rooms routes (protected)
|
|
|
|
protectedApiRoute.POST("/room", api.CreateRoomHandler)
|
|
|
|
protectedApiRoute.GET("/room", api.GetAllRoomsHandler)
|
|
|
|
protectedApiRoute.DELETE("/room/:id", api.DeleteRoomHandler)
|
2023-05-18 20:04:55 -06:00
|
|
|
|
|
|
|
// Webhook Routes:
|
|
|
|
webhookGroup.POST("/clerk", webhooks.ClerkWebhookHandler)
|
|
|
|
|
|
|
|
// Parse command-line arguments for IP and port
|
|
|
|
ip := flag.String("ip", "", "IP address to bind the server to")
|
|
|
|
port := flag.String("port", "3000", "Port to bind the server to")
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
// Start server with HTTP/2 support
|
|
|
|
s := &http.Server{
|
|
|
|
Addr: fmt.Sprintf("%s:%s", *ip, *port),
|
|
|
|
Handler: e,
|
|
|
|
}
|
|
|
|
e.Logger.Fatal(e.StartServer(s))
|
2024-05-08 14:43:57 -06:00
|
|
|
lib.LogSuccess.Println("Server started on port", *port)
|
2023-05-18 20:04:55 -06:00
|
|
|
}
|