forked from mtusi2026/mtusi2026lab4
lab4 - оценка 4
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"sync/atomic"
|
||||
|
||||
"mtusi-messenger/internal/store"
|
||||
)
|
||||
|
||||
type Server struct {
|
||||
store *store.Store
|
||||
log *slog.Logger
|
||||
ready *atomic.Bool
|
||||
instanceID string // LAB 3: ID реплики
|
||||
}
|
||||
|
||||
func NewServer(st *store.Store, log *slog.Logger, ready *atomic.Bool, instanceID string) *Server {
|
||||
return &Server{store: st, log: log, ready: ready, instanceID: instanceID}
|
||||
}
|
||||
|
||||
func (s *Server) Handler() http.Handler {
|
||||
mux := http.NewServeMux()
|
||||
|
||||
// Публичные (security: [] в OpenAPI):
|
||||
mux.HandleFunc("/health/live", s.healthLive)
|
||||
mux.HandleFunc("/health/ready", s.healthReady)
|
||||
mux.HandleFunc("/api/v1/users", s.usersRoute)
|
||||
// Вход — ТОЛЬКО Basic:
|
||||
mux.HandleFunc("/api/v1/auth/sessions", s.basicOnly(s.createSession))
|
||||
|
||||
// Всё остальное — только Bearer:
|
||||
mux.HandleFunc("/api/v1/auth/sessions/current", s.auth(s.revokeSession))
|
||||
mux.HandleFunc("/api/v1/users/me", s.auth(s.me))
|
||||
mux.HandleFunc("/api/v1/chats", s.auth(s.chatsRoute))
|
||||
mux.HandleFunc("/api/v1/chats/{chat_id}", s.auth(s.chatRoute))
|
||||
mux.HandleFunc("/api/v1/chats/{chat_id}/messages", s.auth(s.messagesRoute))
|
||||
|
||||
// ─────────── LAB 4: вложения ───────────
|
||||
// Загрузка одного PNG/JPEG (сырые байты, НЕ multipart).
|
||||
mux.HandleFunc("/api/v1/chats/{chat_id}/attachments", s.auth(s.attachmentsRoute))
|
||||
// Метаданные вложения (доступ только участникам чата).
|
||||
mux.HandleFunc("/api/v1/attachments/{attachment_id}", s.auth(s.attachmentRoute))
|
||||
// Скачивание байтов через авторизованный API.
|
||||
mux.HandleFunc("/api/v1/attachments/{attachment_id}/content", s.auth(s.attachmentContentRoute))
|
||||
|
||||
mux.HandleFunc("/", s.notFound)
|
||||
|
||||
var h http.Handler = mux
|
||||
h = recoverMiddleware(s.log, h)
|
||||
h = loggingMiddleware(s.log, h)
|
||||
h = requestIDMiddleware(h)
|
||||
h = instanceIDMiddleware(s.instanceID, h)
|
||||
return h
|
||||
}
|
||||
Reference in New Issue
Block a user