forked from baron/baron-sso
98 lines
2.7 KiB
Go
98 lines
2.7 KiB
Go
package main
|
|
|
|
import (
|
|
"log"
|
|
"os"
|
|
"strconv"
|
|
|
|
"baron-sso-backend/internal/handler"
|
|
"baron-sso-backend/internal/repository"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
"github.com/gofiber/fiber/v2/middleware/cors"
|
|
"github.com/gofiber/fiber/v2/middleware/encryptcookie"
|
|
"github.com/gofiber/fiber/v2/middleware/logger"
|
|
"github.com/gofiber/fiber/v2/middleware/recover"
|
|
)
|
|
|
|
func getEnv(key, fallback string) string {
|
|
if value, ok := os.LookupEnv(key); ok {
|
|
return value
|
|
}
|
|
return fallback
|
|
}
|
|
|
|
func main() {
|
|
// 1. Initialize DB Connections
|
|
chHost := getEnv("CLICKHOUSE_HOST", "localhost")
|
|
chPort, _ := strconv.Atoi(getEnv("CLICKHOUSE_PORT_NATIVE", "9000"))
|
|
chUser := getEnv("CLICKHOUSE_USER", "default")
|
|
chPass := getEnv("CLICKHOUSE_PASSWORD", "")
|
|
chDB := getEnv("CLICKHOUSE_DB", "default")
|
|
|
|
auditRepo, err := repository.NewClickHouseRepository(chHost, chPort, chUser, chPass, chDB)
|
|
if err != nil {
|
|
log.Printf("Warning: Failed to connect to ClickHouse: %v. Audit logs will fail.", err)
|
|
// Proceeding mostly for Dev purposes, but in Prod should generally fail or fallback.
|
|
}
|
|
|
|
// 2. Initialize Handlers
|
|
auditHandler := handler.NewAuditHandler(auditRepo)
|
|
authHandler := handler.NewAuthHandler()
|
|
|
|
// 3. Initialize Fiber
|
|
app := fiber.New(fiber.Config{
|
|
AppName: "Baron SSO Backend",
|
|
})
|
|
|
|
// Middleware
|
|
app.Use(logger.New())
|
|
app.Use(recover.New())
|
|
app.Use(cors.New(cors.Config{
|
|
AllowOrigins: "*", // Adjust in production
|
|
AllowHeaders: "Origin, Content-Type, Accept, Authorization",
|
|
}))
|
|
app.Use(encryptcookie.New(encryptcookie.Config{
|
|
Key: getEnv("COOKIE_SECRET", "secret-key-must-be-32-bytes-long!"),
|
|
}))
|
|
|
|
// Routes
|
|
app.Get("/", func(c *fiber.Ctx) error {
|
|
return c.SendString("Baron SSO Audit Backend Online")
|
|
})
|
|
|
|
app.Get("/health", func(c *fiber.Ctx) error {
|
|
return c.JSON(fiber.Map{"status": "ok"})
|
|
})
|
|
|
|
// API Group
|
|
api := app.Group("/api/v1")
|
|
api.Post("/audit", auditHandler.CreateLog)
|
|
|
|
// Auth Proxy Routes
|
|
auth := api.Group("/auth")
|
|
auth.Post("/enchanted-link/init", authHandler.InitEnchantedLink)
|
|
auth.Post("/enchanted-link/poll", authHandler.PollEnchantedLink)
|
|
auth.Post("/magic-link/verify", authHandler.VerifyMagicLink)
|
|
auth.Post("/sms", authHandler.SendSms)
|
|
auth.Post("/verify-sms", authHandler.VerifySms)
|
|
|
|
// Client Logging Route (For Debugging)
|
|
api.Post("/client-log", func(c *fiber.Ctx) error {
|
|
type LogReq struct {
|
|
Level string `json:"level"`
|
|
Message string `json:"message"`
|
|
}
|
|
var req LogReq
|
|
if err := c.BodyParser(&req); err != nil {
|
|
return c.SendStatus(fiber.StatusBadRequest)
|
|
}
|
|
log.Printf("[CLIENT-LOG] [%s] %s", req.Level, req.Message)
|
|
return c.SendStatus(fiber.StatusOK)
|
|
})
|
|
|
|
// Start Server
|
|
port := getEnv("PORT", "3000")
|
|
log.Fatal(app.Listen(":" + port))
|
|
}
|