goshort/internal/server/api/router.go

59 lines
1.4 KiB
Go
Raw Normal View History

2023-08-17 14:14:59 -03:00
package apiserver
import (
"net/http"
2023-08-30 21:03:44 -03:00
authmiddleware "git.maronato.dev/maronato/goshort/internal/server/middleware/auth"
2023-08-17 14:14:59 -03:00
"github.com/go-chi/chi/v5"
)
2023-08-26 11:47:46 -03:00
func NewAPIRouter(handler *APIHandler) http.Handler {
2023-08-17 14:14:59 -03:00
mux := chi.NewRouter()
2023-08-17 17:55:28 -03:00
// Auth routes
2023-08-26 11:47:46 -03:00
mux.Post("/login", handler.Login)
mux.Post("/logout", handler.Logout)
mux.Post("/signup", handler.Signup)
2023-08-17 17:55:28 -03:00
2024-03-09 07:42:36 -03:00
// Public routes
mux.Get("/config", handler.PublicConfig)
2023-08-17 17:55:28 -03:00
// Authenticated routes
mux.Group(func(r chi.Router) {
2023-08-30 21:03:44 -03:00
// UI and API endpoints
r.Group(func(r chi.Router) {
r.Use(authmiddleware.AuthRequired(authmiddleware.TokenAuth, authmiddleware.SessionAuth))
// "Me" routes
r.Get("/me", handler.Me)
// Shorts routes
r.Get("/shorts", handler.ListShorts)
r.Post("/shorts", handler.CreateShort)
r.Get("/shorts/{id}", handler.FindShort)
r.Delete("/shorts/{id}", handler.DeleteShort)
r.Get("/shorts/{id}/logs", handler.ListShortLogs)
})
// UI-only endpoints
r.Group(func(r chi.Router) {
r.Use(authmiddleware.AuthRequired(authmiddleware.SessionAuth))
// "Me" routes
r.Delete("/me", handler.DeleteMe)
// Sessions routes
r.Get("/sessions", handler.ListSessions)
r.Delete("/sessions/{id}", handler.DeleteSession)
// Tokens routes
r.Get("/tokens", handler.ListTokens)
r.Post("/tokens", handler.CreateToken)
r.Patch("/tokens/{id}", handler.ChangeTokenName)
r.Delete("/tokens/{id}", handler.DeleteToken)
})
2023-08-17 17:55:28 -03:00
})
2023-08-17 14:14:59 -03:00
return mux
}