2c7f8de242
- Create internal/constants package with all hardcoded values (environment, cookies, themes, headers, routes, cache) - Create internal/httputil package for HTTP helper functions - Update all handlers and middleware to use centralized constants - Reorganize documentation with numbered prefixes (00-26) - Remove duplicate docs from validation folder and docs/ - Delete handlers/constants.go (moved to internal/constants)
35 lines
948 B
Go
35 lines
948 B
Go
package httputil
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
)
|
|
|
|
// JSON writes a JSON response with the given status code.
|
|
func JSON(w http.ResponseWriter, status int, data interface{}) error {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(status)
|
|
return json.NewEncoder(w).Encode(data)
|
|
}
|
|
|
|
// JSONOk writes a 200 OK JSON response.
|
|
func JSONOk(w http.ResponseWriter, data interface{}) error {
|
|
return JSON(w, http.StatusOK, data)
|
|
}
|
|
|
|
// JSONCached writes a JSON response with caching headers.
|
|
func JSONCached(w http.ResponseWriter, data interface{}, maxAge int) error {
|
|
w.Header().Set("Cache-Control", "public, max-age="+string(rune(maxAge)))
|
|
return JSONOk(w, data)
|
|
}
|
|
|
|
// HTML sets HTML content type header.
|
|
func HTML(w http.ResponseWriter) {
|
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
|
}
|
|
|
|
// NoContent sends a 204 No Content response.
|
|
func NoContent(w http.ResponseWriter) {
|
|
w.WriteHeader(http.StatusNoContent)
|
|
}
|