forked from mtusi2026/mtusi2026lab2
83 lines
2.8 KiB
Go
83 lines
2.8 KiB
Go
package httpapi
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
|
|
"mtusi2026/internal/cache"
|
|
"mtusi2026/internal/model"
|
|
"mtusi2026/internal/sessionstore"
|
|
"mtusi2026/internal/store"
|
|
)
|
|
|
|
func NewRouter(s *store.Store, pool *pgxpool.Pool, sessionStores ...sessionstore.Store) http.Handler {
|
|
var ss sessionstore.Store
|
|
if len(sessionStores) > 0 {
|
|
ss = sessionStores[0]
|
|
}
|
|
|
|
a := &api{
|
|
store: s,
|
|
sessionStore: ss,
|
|
}
|
|
|
|
mux := http.NewServeMux()
|
|
mux.HandleFunc("GET /health/live", handleHealthLive)
|
|
mux.HandleFunc("GET /health/ready", handleHealthReady(pool))
|
|
|
|
mux.HandleFunc("POST /api/v1/users", a.handleRegisterUser)
|
|
mux.HandleFunc("GET /api/v1/users/me", a.authMiddleware(a.handleCurrentUser))
|
|
|
|
mux.HandleFunc("POST /api/v1/chats", a.authMiddleware(a.handleCreateChat))
|
|
mux.HandleFunc("GET /api/v1/chats", a.authMiddleware(a.handleListChats))
|
|
mux.HandleFunc("GET /api/v1/chats/{chat_id}", a.authMiddleware(a.handleGetChat))
|
|
|
|
mux.HandleFunc("POST /api/v1/chats/{chat_id}/messages", a.authMiddleware(a.handleCreateMessage))
|
|
mux.HandleFunc("GET /api/v1/chats/{chat_id}/messages", a.authMiddleware(a.handleListMessages))
|
|
|
|
mux.HandleFunc("POST /api/v1/auth/sessions", a.handleCreateSession)
|
|
mux.HandleFunc("DELETE /api/v1/auth/sessions/current", a.authMiddleware(func(w http.ResponseWriter, r *http.Request, u model.User, requestID string) {
|
|
a.handleDeleteSession(w, r)
|
|
}))
|
|
|
|
var handler http.Handler = mux
|
|
handler = loggingMiddleware(handler)
|
|
handler = concurrencyMiddleware(s.Limiter, handler)
|
|
return handler
|
|
}
|
|
|
|
func NewRouterWithCache(
|
|
s *store.Store,
|
|
pool *pgxpool.Pool,
|
|
sessionStore sessionstore.Store,
|
|
chatCache *cache.Cache,
|
|
) http.Handler {
|
|
a := &api{
|
|
store: s,
|
|
sessionStore: sessionStore,
|
|
chatCache: chatCache,
|
|
}
|
|
|
|
mux := http.NewServeMux()
|
|
mux.HandleFunc("GET /health/live", handleHealthLive)
|
|
mux.HandleFunc("GET /health/ready", handleHealthReady(pool))
|
|
mux.HandleFunc("POST /api/v1/users", a.handleRegisterUser)
|
|
mux.HandleFunc("GET /api/v1/users/me", a.authMiddleware(a.handleCurrentUser))
|
|
mux.HandleFunc("POST /api/v1/chats", a.authMiddleware(a.handleCreateChat))
|
|
mux.HandleFunc("GET /api/v1/chats", a.authMiddleware(a.handleListChats))
|
|
mux.HandleFunc("GET /api/v1/chats/{chat_id}", a.authMiddleware(a.handleGetChat))
|
|
mux.HandleFunc("POST /api/v1/chats/{chat_id}/messages", a.authMiddleware(a.handleCreateMessage))
|
|
mux.HandleFunc("GET /api/v1/chats/{chat_id}/messages", a.authMiddleware(a.handleListMessages))
|
|
mux.HandleFunc("POST /api/v1/auth/sessions", a.handleCreateSession)
|
|
mux.HandleFunc("DELETE /api/v1/auth/sessions/current", a.authMiddleware(func(w http.ResponseWriter, r *http.Request, u model.User, requestID string) {
|
|
a.handleDeleteSession(w, r)
|
|
}))
|
|
|
|
var handler http.Handler = mux
|
|
handler = loggingMiddleware(handler)
|
|
handler = concurrencyMiddleware(s.Limiter, handler)
|
|
|
|
return handler
|
|
}
|