45 lines
1.1 KiB
Go
45 lines
1.1 KiB
Go
package rest
|
|
|
|
import (
|
|
"log/slog"
|
|
"net/http"
|
|
|
|
"mtusi-messenger/internal/transport/rest/internal/oas"
|
|
)
|
|
|
|
func NewRouter(
|
|
handler oas.Handler,
|
|
security oas.SecurityHandler,
|
|
extensions *Extensions,
|
|
log *slog.Logger,
|
|
maxBodyBytes, maxUploadBytes int64,
|
|
) (http.Handler, error) {
|
|
server, err := oas.NewServer(
|
|
handler,
|
|
security,
|
|
oas.WithErrorHandler(newErrorHandler(log)),
|
|
oas.WithNotFound(newNotFoundHandler(log)),
|
|
oas.WithMethodNotAllowed(newMethodNotAllowedHandler(log)),
|
|
oas.WithMiddleware(loggingMiddleware(log)),
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
if extensions != nil {
|
|
// These routes are an optional contract extension (presigned uploads).
|
|
// They live outside the generated server so contracts/openapi.json stays
|
|
// byte-identical to the mandatory contract.
|
|
mux.Handle("/api/v1/chats/{chat_id}/attachments/presign", extensions.Presign())
|
|
mux.Handle("/api/v1/attachments/{attachment_id}/complete", extensions.Complete())
|
|
}
|
|
|
|
mux.Handle("/", server)
|
|
|
|
return requestIDMiddleware(
|
|
instanceIDMiddleware(InstanceID(), bodyLimitMiddleware(maxBodyBytes, maxUploadBytes, log, mux)),
|
|
), nil
|
|
}
|