82 lines
2.0 KiB
Go
82 lines
2.0 KiB
Go
package main
|
||
|
||
import (
|
||
"janex/admin-dashboard-backend/modules/config"
|
||
"janex/admin-dashboard-backend/modules/database"
|
||
"janex/admin-dashboard-backend/modules/grouptasks"
|
||
"janex/admin-dashboard-backend/modules/logger"
|
||
"janex/admin-dashboard-backend/modules/structs"
|
||
"janex/admin-dashboard-backend/modules/systempermissions"
|
||
"janex/admin-dashboard-backend/modules/utils"
|
||
"janex/admin-dashboard-backend/routers/router"
|
||
"janex/admin-dashboard-backend/socketserver"
|
||
|
||
"github.com/gofiber/fiber/v2"
|
||
"github.com/gofiber/fiber/v2/middleware/cors"
|
||
flogger "github.com/gofiber/fiber/v2/middleware/logger"
|
||
"github.com/gofiber/websocket/v2"
|
||
)
|
||
|
||
func init() {
|
||
config.LoadConfig()
|
||
logger.InitLogger()
|
||
utils.ValidatorInit()
|
||
logger.InitLanguageLogMessages()
|
||
systempermissions.InitSystemPermissions()
|
||
grouptasks.LoadGroups("")
|
||
database.InitDatabase()
|
||
}
|
||
|
||
func main() {
|
||
app := fiber.New()
|
||
|
||
app.Use(cors.New())
|
||
|
||
if config.Cfg.Debug {
|
||
app.Use(flogger.New(flogger.Config{
|
||
Format: "${pid} ${locals:requestid} ${status} - ${latency} ${method} ${path}\n",
|
||
}))
|
||
}
|
||
|
||
router.SetupRoutes(app)
|
||
|
||
app.Use("/ws", func(c *fiber.Ctx) error {
|
||
// IsWebSocketUpgrade returns true if the client
|
||
// requested upgrade to the WebSocket protocol.
|
||
if websocket.IsWebSocketUpgrade(c) {
|
||
sessionId := c.Query("auth")
|
||
|
||
if len(sessionId) != utils.LenHeaderXAuthorization {
|
||
return c.SendStatus(fiber.StatusUnauthorized)
|
||
}
|
||
|
||
// validate ws session
|
||
var userSession structs.UserSession
|
||
|
||
database.DB.First(&userSession, "id = ?", sessionId)
|
||
|
||
if userSession.Id != "" {
|
||
var user structs.User
|
||
|
||
database.DB.First(&user, "id = ?", userSession.UserId)
|
||
|
||
if user.Id != "" {
|
||
c.Locals("sessionId", sessionId)
|
||
c.Locals("userId", user.Id)
|
||
}
|
||
}
|
||
|
||
return c.Next()
|
||
}
|
||
|
||
return fiber.ErrUpgradeRequired
|
||
})
|
||
|
||
go socketserver.RunHub()
|
||
socketserver.WebSocketServer(app)
|
||
|
||
go grouptasks.StartUnlockLockedGroupTaskStepsTicker()
|
||
|
||
app.Listen(config.Cfg.Host + ":" + config.Cfg.Port)
|
||
}
|