mirror of
https://github.com/gazer-x/komari.git
synced 2026-06-22 00:05:52 +08:00
90aad4b48a
- Moved API route registration to dedicated init files for better organization. - Introduced event listeners for server initialization to dynamically register routes. - Removed redundant configuration loading in routers.go. - Added new API routes for various functionalities including client management, admin tasks, and notifications. - Implemented a standardized response structure for API responses. - Established WebSocket connections for terminal sessions and improved session management. - Created a new database initialization for default admin account creation. - Enhanced gRPC server setup for Nezha compatibility with dynamic configuration updates.
44 lines
1.2 KiB
Go
44 lines
1.2 KiB
Go
package server
|
|
|
|
import (
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/gookit/event"
|
|
"github.com/komari-monitor/komari/internal/conf"
|
|
"github.com/komari-monitor/komari/internal/eventType"
|
|
"github.com/komari-monitor/komari/public"
|
|
)
|
|
|
|
var (
|
|
AllowCors bool = false
|
|
)
|
|
|
|
func Init(r *gin.Engine) {
|
|
|
|
event.On(eventType.ConfigUpdated, event.ListenerFunc(func(e event.Event) error {
|
|
newConf := e.Get("new").(conf.Config)
|
|
AllowCors = newConf.Site.AllowCors
|
|
public.UpdateIndex(newConf.ToV1Format())
|
|
return nil
|
|
}), event.High)
|
|
|
|
r.Use(func(c *gin.Context) {
|
|
if AllowCors {
|
|
c.Header("Access-Control-Allow-Origin", "*")
|
|
c.Header("Access-Control-Allow-Methods", "GET, POST, PUT, PATCH, DELETE, HEAD, OPTIONS")
|
|
c.Header("Access-Control-Allow-Headers", "Origin, Content-Length, Content-Type, Authorization, Accept, X-CSRF-Token, X-Requested-With, Set-Cookie")
|
|
c.Header("Access-Control-Expose-Headers", "Content-Length, Authorization, Set-Cookie")
|
|
c.Header("Access-Control-Allow-Credentials", "false")
|
|
c.Header("Access-Control-Max-Age", "43200") // 12 hours
|
|
if c.Request.Method == "OPTIONS" {
|
|
c.AbortWithStatus(204)
|
|
return
|
|
}
|
|
}
|
|
c.Next()
|
|
})
|
|
|
|
public.Static(r.Group("/"), func(handlers ...gin.HandlerFunc) {
|
|
r.NoRoute(handlers...)
|
|
})
|
|
}
|