2025-03-13 08:25:39 +01:00
|
|
|
package router
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2025-03-15 01:49:52 +01:00
|
|
|
"fmt"
|
2025-03-13 08:25:39 +01:00
|
|
|
"github.com/gorilla/handlers"
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
2025-03-15 01:49:52 +01:00
|
|
|
"sonarqube-badge/config"
|
2025-03-13 08:25:39 +01:00
|
|
|
"sonarqube-badge/router/api"
|
|
|
|
"sonarqube-badge/router/views"
|
|
|
|
)
|
|
|
|
|
|
|
|
func StartServer(ctx context.Context) {
|
|
|
|
r := mux.NewRouter()
|
|
|
|
|
|
|
|
// API ROUTES
|
2025-03-15 01:49:52 +01:00
|
|
|
api.ProjectRouter(r.PathPrefix("/api/project").Subrouter())
|
2025-03-13 08:25:39 +01:00
|
|
|
api.LoginRouter(r.PathPrefix("/api/login").Subrouter())
|
2025-03-15 01:49:52 +01:00
|
|
|
api.UserRouter(r.PathPrefix("/api/user").Subrouter())
|
2025-03-13 08:25:39 +01:00
|
|
|
|
|
|
|
// VIEW ROUTES
|
|
|
|
views.IndexRouter(r)
|
|
|
|
views.LoginRouter(r)
|
2025-03-15 01:49:52 +01:00
|
|
|
views.ProjectRouter(r)
|
|
|
|
views.BadgeRouter(r)
|
|
|
|
views.UserRouter(r)
|
2025-03-13 08:25:39 +01:00
|
|
|
|
|
|
|
credentials := handlers.AllowCredentials()
|
2025-03-15 01:49:52 +01:00
|
|
|
methods := handlers.AllowedMethods([]string{"POST", "GET", "DELETE"})
|
2025-03-13 08:25:39 +01:00
|
|
|
ttl := handlers.MaxAge(3600)
|
|
|
|
|
2025-03-15 01:49:52 +01:00
|
|
|
port := ctx.Value("config").(config.Config).Port
|
|
|
|
if port == 0 {
|
|
|
|
port = 8080
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("Starting server on http://localhost:%d ...", port)
|
|
|
|
|
2025-03-13 08:25:39 +01:00
|
|
|
server := http.Server{
|
2025-03-15 01:49:52 +01:00
|
|
|
Addr: fmt.Sprintf(":%d", port),
|
2025-03-13 08:25:39 +01:00
|
|
|
BaseContext: func(listener net.Listener) context.Context {
|
|
|
|
return ctx
|
|
|
|
},
|
|
|
|
Handler: handlers.CORS(credentials, methods, ttl)(r),
|
|
|
|
}
|
|
|
|
|
2025-03-15 01:49:52 +01:00
|
|
|
err := server.ListenAndServe()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2025-03-13 08:25:39 +01:00
|
|
|
}
|