mirror of
https://github.com/GlueOps/autoglue.git
synced 2026-02-13 12:50:05 +01:00
initial rebuild
This commit is contained in:
76
internal/api/routes.go
Normal file
76
internal/api/routes.go
Normal file
@@ -0,0 +1,76 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
httpPprof "net/http/pprof"
|
||||
|
||||
"github.com/glueops/autoglue/internal/config"
|
||||
"github.com/glueops/autoglue/internal/handlers/authn"
|
||||
"github.com/glueops/autoglue/internal/handlers/health"
|
||||
"github.com/glueops/autoglue/internal/handlers/orgs"
|
||||
"github.com/glueops/autoglue/internal/middleware"
|
||||
"github.com/glueops/autoglue/internal/ui"
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
func RegisterRoutes(r chi.Router) {
|
||||
r.Route("/api", func(api chi.Router) {
|
||||
api.Get("/healthz", health.Check)
|
||||
|
||||
api.Route("/v1", func(v1 chi.Router) {
|
||||
secret := viper.GetString("authentication.jwt_secret")
|
||||
authMW := middleware.AuthMiddleware(secret)
|
||||
|
||||
v1.Route("/auth", func(a chi.Router) {
|
||||
a.Post("/login", authn.Login)
|
||||
a.Post("/register", authn.Register)
|
||||
a.Post("/introspect", authn.Introspect)
|
||||
a.Post("/password/forgot", authn.RequestPasswordReset)
|
||||
a.Post("/password/reset", authn.ConfirmPasswordReset)
|
||||
a.Get("/verify", authn.VerifyEmail)
|
||||
a.Post("/verify/resend", authn.ResendVerification)
|
||||
|
||||
a.Group(func(pr chi.Router) {
|
||||
pr.Use(authMW)
|
||||
pr.Post("/refresh", authn.Refresh)
|
||||
pr.Post("/logout", authn.Logout)
|
||||
pr.Post("/logout_all", authn.LogoutAll)
|
||||
pr.Get("/me", authn.Me)
|
||||
pr.Post("/password/change", authn.ChangePassword)
|
||||
pr.Post("/refresh/rotate", authn.RotateRefreshToken)
|
||||
})
|
||||
})
|
||||
|
||||
v1.Route("/orgs", func(o chi.Router) {
|
||||
o.Use(authMW)
|
||||
o.Post("/", orgs.CreateOrganization)
|
||||
o.Get("/", orgs.ListOrganizations)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
r.Route("/debug/pprof", func(pr chi.Router) {
|
||||
pr.Get("/", httpPprof.Index)
|
||||
pr.Get("/cmdline", httpPprof.Cmdline)
|
||||
pr.Get("/profile", httpPprof.Profile)
|
||||
pr.Get("/symbol", httpPprof.Symbol)
|
||||
pr.Get("/trace", httpPprof.Trace)
|
||||
|
||||
pr.Handle("/allocs", httpPprof.Handler("allocs"))
|
||||
pr.Handle("/block", httpPprof.Handler("block"))
|
||||
pr.Handle("/goroutine", httpPprof.Handler("goroutine"))
|
||||
pr.Handle("/heap", httpPprof.Handler("heap"))
|
||||
pr.Handle("/mutex", httpPprof.Handler("mutex"))
|
||||
pr.Handle("/threadcreate", httpPprof.Handler("threadcreate"))
|
||||
})
|
||||
|
||||
if config.IsUIDev() {
|
||||
if h, err := ui.DevProxy("http://localhost:5173"); err == nil {
|
||||
r.NotFound(h.ServeHTTP)
|
||||
}
|
||||
} else {
|
||||
if h, err := ui.SPAHandler(); err == nil {
|
||||
r.NotFound(h.ServeHTTP)
|
||||
}
|
||||
}
|
||||
}
|
||||
59
internal/api/server.go
Normal file
59
internal/api/server.go
Normal file
@@ -0,0 +1,59 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/glueops/autoglue/docs"
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/go-chi/chi/v5/middleware"
|
||||
"github.com/go-chi/cors"
|
||||
httpSwagger "github.com/swaggo/http-swagger/v2"
|
||||
)
|
||||
|
||||
func NewRouter() http.Handler {
|
||||
r := chi.NewRouter()
|
||||
r.Use(middleware.RequestID)
|
||||
r.Use(middleware.RealIP)
|
||||
r.Use(middleware.Logger)
|
||||
r.Use(middleware.Recoverer)
|
||||
|
||||
r.Use(cors.Handler(cors.Options{
|
||||
AllowedOrigins: []string{
|
||||
"http://localhost:5173",
|
||||
"http://127.0.0.1:5173",
|
||||
"http://localhost:8080",
|
||||
"http://127.0.0.1:8080",
|
||||
},
|
||||
AllowedMethods: []string{"GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"},
|
||||
AllowedHeaders: []string{"Content-Type", "content-type", "Authorization", "authorization", "X-Org-ID", "x-org-id"},
|
||||
AllowCredentials: true,
|
||||
// OptionsPassthrough: false, // default; Chi will auto 200 OPTIONS
|
||||
// MaxAge: 300, // optional
|
||||
}))
|
||||
|
||||
RegisterRoutes(r)
|
||||
|
||||
r.Mount("/swagger", httpSwagger.WrapHandler)
|
||||
r.Get("/swagger/swagger.json", serveSwaggerFromEmbed(docs.SwaggerJSON, "application/json"))
|
||||
r.Get("/swagger/swagger.yaml", serveSwaggerFromEmbed(docs.SwaggerYAML, "application/x-yaml"))
|
||||
return r
|
||||
}
|
||||
|
||||
func NewServer(addr string) *http.Server {
|
||||
return &http.Server{
|
||||
Addr: addr,
|
||||
Handler: NewRouter(),
|
||||
ReadTimeout: 5 * time.Second,
|
||||
WriteTimeout: 10 * time.Second,
|
||||
IdleTimeout: 120 * time.Second,
|
||||
}
|
||||
}
|
||||
|
||||
func serveSwaggerFromEmbed(data []byte, contentType string) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, _ *http.Request) {
|
||||
w.Header().Set("Content-Type", contentType)
|
||||
w.WriteHeader(http.StatusOK)
|
||||
_, _ = w.Write(data)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user