mirror of
https://github.com/anotherhadi/iknowyou.git
synced 2026-04-12 00:47:26 +02:00
init
This commit is contained in:
73
back/internal/api/router.go
Normal file
73
back/internal/api/router.go
Normal file
@@ -0,0 +1,73 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
chimiddleware "github.com/go-chi/chi/v5/middleware"
|
||||
"golang.org/x/time/rate"
|
||||
|
||||
"github.com/anotherhadi/iknowyou/internal/api/handler"
|
||||
ikymiddleware "github.com/anotherhadi/iknowyou/internal/api/middleware"
|
||||
"github.com/anotherhadi/iknowyou/internal/search"
|
||||
"github.com/anotherhadi/iknowyou/internal/tools"
|
||||
)
|
||||
|
||||
func NewRouter(
|
||||
manager *search.Manager,
|
||||
factories []func() tools.ToolRunner,
|
||||
configPath string,
|
||||
frontDir string,
|
||||
demo bool,
|
||||
) *chi.Mux {
|
||||
r := chi.NewRouter()
|
||||
|
||||
r.Use(chimiddleware.Logger)
|
||||
r.Use(chimiddleware.Recoverer)
|
||||
r.Use(chimiddleware.RequestID)
|
||||
|
||||
searchHandler := handler.NewSearchHandler(manager, demo)
|
||||
toolsHandler := handler.NewToolsHandler(factories)
|
||||
configHandler := handler.NewConfigHandler(configPath, factories, demo)
|
||||
|
||||
searchLimiter := ikymiddleware.New(rate.Every(10*time.Second), 3)
|
||||
|
||||
r.Route("/api", func(r chi.Router) {
|
||||
r.Route("/searches", func(r chi.Router) {
|
||||
r.With(searchLimiter.Handler).Post("/", searchHandler.Create)
|
||||
r.Get("/", searchHandler.List)
|
||||
r.Get("/{id}", searchHandler.Get)
|
||||
r.Delete("/{id}", searchHandler.Delete)
|
||||
})
|
||||
|
||||
r.Route("/tools", func(r chi.Router) {
|
||||
r.Get("/", toolsHandler.List)
|
||||
r.Get("/{name}", toolsHandler.Get)
|
||||
})
|
||||
|
||||
r.Route("/config", func(r chi.Router) {
|
||||
r.Get("/", configHandler.Get)
|
||||
|
||||
r.Route("/tools", func(r chi.Router) {
|
||||
r.Patch("/{toolName}", configHandler.UpdateToolConfig)
|
||||
r.Delete("/{toolName}", configHandler.DeleteToolConfig)
|
||||
})
|
||||
|
||||
r.Route("/profiles", func(r chi.Router) {
|
||||
r.Get("/", configHandler.ListProfiles)
|
||||
r.Post("/", configHandler.CreateProfile)
|
||||
r.Get("/{name}", configHandler.GetProfile)
|
||||
r.Patch("/{name}", configHandler.UpdateProfile)
|
||||
r.Delete("/{name}", configHandler.DeleteProfile)
|
||||
r.Patch("/{name}/tools/{toolName}", configHandler.UpdateProfileToolConfig)
|
||||
r.Delete("/{name}/tools/{toolName}", configHandler.DeleteProfileToolConfig)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
if frontDir != "" {
|
||||
r.Handle("/*", newStaticHandler(frontDir))
|
||||
}
|
||||
|
||||
return r
|
||||
}
|
||||
Reference in New Issue
Block a user