101 lines
2.9 KiB
Go
101 lines
2.9 KiB
Go
|
|
package main
|
||
|
|
|
||
|
|
import (
|
||
|
|
"log/slog"
|
||
|
|
"net/http"
|
||
|
|
"os"
|
||
|
|
"time"
|
||
|
|
|
||
|
|
"github.com/labstack/echo/v4"
|
||
|
|
"github.com/labstack/echo/v4/middleware"
|
||
|
|
"queryorchestration/internal/cognitoauth" // Replace with your actual package import path
|
||
|
|
)
|
||
|
|
|
||
|
|
func main() {
|
||
|
|
// Create a new Echo instance
|
||
|
|
e := echo.New()
|
||
|
|
|
||
|
|
// Add basic middleware
|
||
|
|
e.Use(middleware.Logger())
|
||
|
|
e.Use(middleware.Recover())
|
||
|
|
|
||
|
|
// Initialize logger with appropriate level based on DEBUG env var
|
||
|
|
var logLevel slog.Level
|
||
|
|
if os.Getenv("DEBUG") == "true" {
|
||
|
|
logLevel = slog.LevelDebug
|
||
|
|
} else {
|
||
|
|
logLevel = slog.LevelInfo
|
||
|
|
}
|
||
|
|
|
||
|
|
logHandler := slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
|
||
|
|
Level: logLevel,
|
||
|
|
})
|
||
|
|
logger := slog.New(logHandler)
|
||
|
|
|
||
|
|
// Create auth config from environment variables
|
||
|
|
config := cognitoauth.NewConfigFromEnv("http://localhost:8080", logger)
|
||
|
|
|
||
|
|
// Set route permissions
|
||
|
|
routePermissions := map[string][]string{
|
||
|
|
"/users": {"exporters", "uploaders", "querybuilders"},
|
||
|
|
"/users/:id": {"exporters", "querybuilders"},
|
||
|
|
"/orders": {"exporters"},
|
||
|
|
"/reports/sales": {"exporters", "uploaders", "querybuilders"},
|
||
|
|
"/settings": {"exporters"},
|
||
|
|
"/api/inventory/update": {"exporters", "uploaders"},
|
||
|
|
}
|
||
|
|
config.SetRoutePermissions(routePermissions)
|
||
|
|
|
||
|
|
// Register authentication routes
|
||
|
|
cognitoauth.RegisterRoutes(e, config)
|
||
|
|
|
||
|
|
// Register your application routes
|
||
|
|
// These will be protected based on the permissions set above
|
||
|
|
e.GET("/users", func(c echo.Context) error {
|
||
|
|
return c.String(http.StatusOK, "Users endpoint (OK)")
|
||
|
|
})
|
||
|
|
|
||
|
|
e.GET("/users/:id", func(c echo.Context) error {
|
||
|
|
id := c.Param("id")
|
||
|
|
return c.String(http.StatusOK, "User details endpoint for ID: "+id)
|
||
|
|
})
|
||
|
|
|
||
|
|
e.GET("/orders", func(c echo.Context) error {
|
||
|
|
return c.String(http.StatusOK, "Orders endpoint (OK)")
|
||
|
|
})
|
||
|
|
|
||
|
|
e.GET("/reports/sales", func(c echo.Context) error {
|
||
|
|
return c.String(http.StatusOK, "Sales reports endpoint (OK)")
|
||
|
|
})
|
||
|
|
|
||
|
|
e.GET("/settings", func(c echo.Context) error {
|
||
|
|
return c.String(http.StatusOK, "Settings endpoint (OK)")
|
||
|
|
})
|
||
|
|
|
||
|
|
e.PUT("/api/inventory/update", func(c echo.Context) error {
|
||
|
|
return c.String(http.StatusOK, "Inventory update endpoint (OK)")
|
||
|
|
})
|
||
|
|
|
||
|
|
// Alternative approach: Use the RequireGroups middleware for specific routes
|
||
|
|
// This can be used instead of or in addition to the global permissions map
|
||
|
|
adminGroup := e.Group("/admin")
|
||
|
|
adminGroup.Use(cognitoauth.RequireGroups("admin")) // Only admin group can access this route
|
||
|
|
|
||
|
|
adminGroup.GET("/dashboard", func(c echo.Context) error {
|
||
|
|
return c.String(http.StatusOK, "Admin Dashboard (OK)")
|
||
|
|
})
|
||
|
|
|
||
|
|
// Configure server
|
||
|
|
server := &http.Server{
|
||
|
|
Addr: ":8080",
|
||
|
|
ReadHeaderTimeout: 3 * time.Second,
|
||
|
|
ReadTimeout: 20 * time.Second,
|
||
|
|
WriteTimeout: 20 * time.Second,
|
||
|
|
IdleTimeout: 120 * time.Second,
|
||
|
|
}
|
||
|
|
|
||
|
|
// Start server
|
||
|
|
logger.Info("Server starting on :8080")
|
||
|
|
e.Logger.Fatal(e.StartServer(server))
|
||
|
|
}
|