error
This commit is contained in:
parent
44500afd3f
commit
5d370e1077
13 changed files with 529 additions and 519 deletions
|
@ -13,18 +13,20 @@ import (
|
|||
|
||||
"otpm/config"
|
||||
"otpm/middleware"
|
||||
|
||||
"github.com/julienschmidt/httprouter"
|
||||
)
|
||||
|
||||
// Server represents the HTTP server
|
||||
type Server struct {
|
||||
server *http.Server
|
||||
router *http.ServeMux
|
||||
router *httprouter.Router
|
||||
config *config.Config
|
||||
}
|
||||
|
||||
// New creates a new server
|
||||
func New(cfg *config.Config) *Server {
|
||||
router := http.NewServeMux()
|
||||
router := httprouter.New()
|
||||
|
||||
server := &http.Server{
|
||||
Addr: fmt.Sprintf(":%d", cfg.Server.Port),
|
||||
|
@ -111,29 +113,46 @@ func (s *Server) Shutdown() error {
|
|||
}
|
||||
|
||||
// Router returns the router
|
||||
func (s *Server) Router() *http.ServeMux {
|
||||
func (s *Server) Router() *httprouter.Router {
|
||||
return s.router
|
||||
}
|
||||
|
||||
// RegisterRoutes registers all routes
|
||||
func (s *Server) RegisterRoutes(routes map[string]http.Handler) {
|
||||
func (s *Server) RegisterRoutes(routes map[string]httprouter.Handle) {
|
||||
for pattern, handler := range routes {
|
||||
s.router.Handle(pattern, handler)
|
||||
s.router.Handle("GET", pattern, handler)
|
||||
s.router.Handle("POST", pattern, handler)
|
||||
s.router.Handle("PUT", pattern, handler)
|
||||
s.router.Handle("DELETE", pattern, handler)
|
||||
}
|
||||
}
|
||||
|
||||
// RegisterAuthRoutes registers routes that require authentication
|
||||
func (s *Server) RegisterAuthRoutes(routes map[string]http.Handler) {
|
||||
func (s *Server) RegisterAuthRoutes(routes map[string]httprouter.Handle) {
|
||||
for pattern, handler := range routes {
|
||||
// Apply authentication middleware
|
||||
authHandler := middleware.Auth(s.config.JWT.Secret)(handler)
|
||||
s.router.Handle(pattern, authHandler)
|
||||
authHandler := func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
||||
// Convert httprouter.Handle to http.HandlerFunc for middleware
|
||||
wrappedHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
// Store params in request context
|
||||
ctx := context.WithValue(r.Context(), "params", ps)
|
||||
handler(w, r.WithContext(ctx), ps)
|
||||
})
|
||||
|
||||
// Apply auth middleware
|
||||
middleware.Auth(s.config.JWT.Secret)(wrappedHandler).ServeHTTP(w, r)
|
||||
}
|
||||
|
||||
s.router.Handle("GET", pattern, authHandler)
|
||||
s.router.Handle("POST", pattern, authHandler)
|
||||
s.router.Handle("PUT", pattern, authHandler)
|
||||
s.router.Handle("DELETE", pattern, authHandler)
|
||||
}
|
||||
}
|
||||
|
||||
// RegisterHealthCheck registers an enhanced health check endpoint
|
||||
func (s *Server) RegisterHealthCheck() {
|
||||
s.router.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
|
||||
s.router.GET("/health", func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
||||
response := map[string]interface{}{
|
||||
"status": "ok",
|
||||
"timestamp": time.Now().Format(time.RFC3339),
|
||||
|
@ -145,9 +164,8 @@ func (s *Server) RegisterHealthCheck() {
|
|||
}
|
||||
|
||||
// Add database status if configured
|
||||
if s.config.Database.DSN != "" { // Changed from URL to DSN to match config
|
||||
if s.config.Database.DSN != "" {
|
||||
dbStatus := "ok"
|
||||
// Removed DB ping check since we don't have DB instance in config
|
||||
response["database"] = dbStatus
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue