forked from baron/baron-sso
slog 통합
This commit is contained in:
@@ -1,27 +1,19 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"baron-sso-backend/internal/handler"
|
||||
"baron-sso-backend/internal/logger"
|
||||
"baron-sso-backend/internal/repository"
|
||||
"baron-sso-backend/internal/service"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"os"
|
||||
"strings"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/bwmarrin/snowflake"
|
||||
|
||||
"baron-sso-backend/internal/handler"
|
||||
|
||||
"baron-sso-backend/internal/logger"
|
||||
|
||||
"baron-sso-backend/internal/repository"
|
||||
|
||||
"baron-sso-backend/internal/service"
|
||||
|
||||
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
|
||||
|
||||
|
||||
"github.com/bwmarrin/snowflake"
|
||||
"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/recover"
|
||||
@@ -41,7 +33,6 @@ func main() {
|
||||
ServiceName: "baron-sso",
|
||||
Environment: getEnv("GO_ENV", "dev"),
|
||||
})
|
||||
|
||||
// Initialize Snowflake Node (Node 2 for Baron)
|
||||
node, err := snowflake.NewNode(2)
|
||||
if err != nil {
|
||||
@@ -50,10 +41,22 @@ func main() {
|
||||
}
|
||||
|
||||
// 1. Log Config on Startup
|
||||
slog.Info("Starting Baron SSO Backend",
|
||||
fmt.Println("============================================================")
|
||||
fmt.Println(`
|
||||
|\__/,| (\
|
||||
_.|o o |_ ) )
|
||||
-(((---(((--------
|
||||
`)
|
||||
fmt.Println("🚀 Baron SSO Backend Starting...")
|
||||
|
||||
slog.Info("Service starting",
|
||||
"service", "baron-sso",
|
||||
"app_env", getEnv("APP_ENV", "dev"),
|
||||
"db_port", getEnv("DB_PORT", "5532"),
|
||||
"backend_port", getEnv("BACKEND_PORT", "3000"),
|
||||
"frontend_port", getEnv("FRONTEND_PORT", "5000"),
|
||||
"frontend_url", getEnv("FRONTEND_URL", "http://ssologin.hmac.kr"),
|
||||
"redis_addr", getEnv("REDIS_ADDR", "redis:6379"),
|
||||
"descope_id", getEnv("DESCOPE_PROJECT_ID", "not-set"),
|
||||
)
|
||||
|
||||
// 2. Initialize DB Connections
|
||||
@@ -80,7 +83,7 @@ func main() {
|
||||
|
||||
// 3. Initialize Fiber
|
||||
app := fiber.New(fiber.Config{
|
||||
AppName: "Baron SSO Backend",
|
||||
AppName: "Baron SSO Backend",
|
||||
DisableStartupMessage: true, // Clean logs
|
||||
})
|
||||
|
||||
@@ -94,10 +97,10 @@ func main() {
|
||||
// [Standardized] HTTP Request Logger Middleware using slog
|
||||
app.Use(func(c *fiber.Ctx) error {
|
||||
start := time.Now()
|
||||
|
||||
|
||||
// Handle request
|
||||
err := c.Next()
|
||||
|
||||
|
||||
// Log after request
|
||||
latency := time.Since(start)
|
||||
status := c.Response().StatusCode()
|
||||
@@ -107,7 +110,7 @@ func main() {
|
||||
if status < 400 {
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
msg := "http_request"
|
||||
if err != nil {
|
||||
msg = "http_request_error"
|
||||
@@ -129,7 +132,7 @@ func main() {
|
||||
AllowOrigins: "*", // Adjust in production
|
||||
AllowHeaders: "Origin, Content-Type, Accept, Authorization",
|
||||
AllowMethods: "GET, POST, HEAD, PUT, DELETE, PATCH, OPTIONS",
|
||||
}))
|
||||
}))
|
||||
app.Use(encryptcookie.New(encryptcookie.Config{
|
||||
Key: getEnv("COOKIE_SECRET", "secret-key-must-be-32-bytes-long!"),
|
||||
}))
|
||||
@@ -185,7 +188,7 @@ func main() {
|
||||
// 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)
|
||||
@@ -196,7 +199,7 @@ func main() {
|
||||
auth.Post("/qr/init", authHandler.InitQRLogin)
|
||||
auth.Post("/qr/poll", authHandler.PollQRLogin)
|
||||
auth.Post("/qr/approve", authHandler.ScanQRLogin)
|
||||
|
||||
|
||||
// Admin Routes
|
||||
admin := api.Group("/admin")
|
||||
admin.Post("/users", adminHandler.CreateUser)
|
||||
@@ -205,10 +208,10 @@ func main() {
|
||||
admin.Patch("/users/:loginId", adminHandler.UpdateUser)
|
||||
admin.Delete("/users/:loginId", adminHandler.DeleteUser)
|
||||
admin.Patch("/users/:loginId/status", adminHandler.UpdateUserStatus)
|
||||
|
||||
|
||||
// Webhook for Descope Generic SMS Gateway
|
||||
auth.Post("/webhooks/descope-sms", authHandler.HandleDescopeSmsRelay)
|
||||
|
||||
|
||||
// Webhook for Descope Generic Email Gateway (Fake Email Strategy)
|
||||
auth.Post("/webhooks/descope-email", authHandler.HandleDescopeEmailRelay)
|
||||
|
||||
@@ -229,7 +232,7 @@ func main() {
|
||||
slog.String("source", "client"),
|
||||
}
|
||||
for k, v := range req.Data {
|
||||
// Skip svc if it's already set by the global logger to avoid confusion,
|
||||
// Skip svc if it's already set by the global logger to avoid confusion,
|
||||
// or keep it as client_svc
|
||||
if k == "svc" {
|
||||
attrs = append(attrs, slog.Any("client_svc", v))
|
||||
@@ -252,9 +255,10 @@ func main() {
|
||||
// Filter out noisy client navigation logs
|
||||
if level == slog.LevelInfo {
|
||||
msg := strings.ToLower(req.Message)
|
||||
if strings.Contains(msg, "navigating to") ||
|
||||
strings.Contains(msg, "going to") ||
|
||||
strings.Contains(msg, "redirecting to") {
|
||||
if strings.Contains(msg, "navigating to") ||
|
||||
strings.Contains(msg, "going to") ||
|
||||
strings.Contains(msg, "redirecting to") ||
|
||||
strings.Contains(msg, "full paths for routes") {
|
||||
return c.SendStatus(fiber.StatusOK)
|
||||
}
|
||||
}
|
||||
@@ -264,8 +268,9 @@ func main() {
|
||||
})
|
||||
|
||||
// Start Server
|
||||
port := getEnv("PORT", "3000")
|
||||
port := getEnv("BACKEND_PORT", "3000")
|
||||
slog.Info("Server listening", "port", port)
|
||||
fmt.Println("============================================================")
|
||||
if err := app.Listen(":" + port); err != nil {
|
||||
slog.Error("Server failed to start", "error", err)
|
||||
os.Exit(1)
|
||||
|
||||
Reference in New Issue
Block a user