Add basic webserver skeleton with healthcheck
This commit is contained in:
60
internal/handlers/handlers.go
Normal file
60
internal/handlers/handlers.go
Normal file
@@ -0,0 +1,60 @@
|
||||
// Package handlers provides HTTP request handlers.
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
|
||||
"go.uber.org/fx"
|
||||
"sneak.berlin/go/pixa/internal/healthcheck"
|
||||
"sneak.berlin/go/pixa/internal/logger"
|
||||
)
|
||||
|
||||
// HandlersParams defines dependencies for Handlers.
|
||||
type HandlersParams struct {
|
||||
fx.In
|
||||
Logger *logger.Logger
|
||||
Healthcheck *healthcheck.Healthcheck
|
||||
}
|
||||
|
||||
// Handlers provides HTTP request handlers.
|
||||
type Handlers struct {
|
||||
log *slog.Logger
|
||||
hc *healthcheck.Healthcheck
|
||||
}
|
||||
|
||||
// New creates a new Handlers instance.
|
||||
func New(lc fx.Lifecycle, params HandlersParams) (*Handlers, error) {
|
||||
s := &Handlers{
|
||||
log: params.Logger.Get(),
|
||||
hc: params.Healthcheck,
|
||||
}
|
||||
|
||||
lc.Append(fx.Hook{
|
||||
OnStart: func(ctx context.Context) error {
|
||||
return nil
|
||||
},
|
||||
})
|
||||
|
||||
return s, nil
|
||||
}
|
||||
|
||||
func (s *Handlers) respondJSON(w http.ResponseWriter, data interface{}, status int) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(status)
|
||||
if data != nil {
|
||||
err := json.NewEncoder(w).Encode(data)
|
||||
if err != nil {
|
||||
s.log.Error("json encode error", "error", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Handlers) respondError(w http.ResponseWriter, code string, message string, status int) {
|
||||
s.respondJSON(w, map[string]string{
|
||||
"error": code,
|
||||
"message": message,
|
||||
}, status)
|
||||
}
|
||||
12
internal/handlers/healthcheck.go
Normal file
12
internal/handlers/healthcheck.go
Normal file
@@ -0,0 +1,12 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func (s *Handlers) HandleHealthCheck() http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, _ *http.Request) {
|
||||
resp := s.hc.Healthcheck()
|
||||
s.respondJSON(w, resp, http.StatusOK)
|
||||
}
|
||||
}
|
||||
28
internal/handlers/image.go
Normal file
28
internal/handlers/image.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// HandleImage handles the main image proxy route:
|
||||
// /v1/image/<host>/<path>/<width>x<height>.<format>
|
||||
func (s *Handlers) HandleImage() http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, req *http.Request) {
|
||||
// FIXME: Implement image proxy handler
|
||||
// - Parse URL to extract host, path, size, format
|
||||
// - Validate signature and expiration
|
||||
// - Check source host whitelist
|
||||
// - Fetch from upstream (with SSRF protection)
|
||||
// - Process image (resize, convert format)
|
||||
// - Cache and serve result
|
||||
panic("unimplemented")
|
||||
}
|
||||
}
|
||||
|
||||
// HandleRobotsTxt serves robots.txt to prevent search engine crawling
|
||||
func (s *Handlers) HandleRobotsTxt() http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, req *http.Request) {
|
||||
// FIXME: Implement robots.txt handler
|
||||
panic("unimplemented")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user