build: add makefile, dockerfile, ci and development stack
- build: add makefile, dockerfile, ci and development stack - feat(config): add environment configuration and logger - feat(server): add server lifecycle with health and version routes - feat(db): add connection, contract check, migrations and stores - feat(cli): add command dispatch and user management - test(db): add integration tests against mysql
This commit is contained in:
1 parent
45d1e307c6
commit
7089a6f408
46 files changed
+8456
No files matched your search
@@ -0,0 +1,58 @@
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
// Copyright (C) 2026 Bryan Joshua Pedini
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
)
|
||||
|
||||
type routeRegistrar func(s *WebServer, r *mux.Router)
|
||||
|
||||
type handlerMiddleware func(s *WebServer, next http.Handler) http.Handler
|
||||
|
||||
var appRouteRegistrars = []routeRegistrar{}
|
||||
|
||||
var appMiddlewares = []handlerMiddleware{}
|
||||
|
||||
func (s *WebServer) Handler() http.Handler {
|
||||
r := mux.NewRouter()
|
||||
s.Routes(r)
|
||||
var h http.Handler = r
|
||||
for i := len(appMiddlewares) - 1; i >= 0; i-- {
|
||||
h = appMiddlewares[i](s, h)
|
||||
}
|
||||
return h
|
||||
}
|
||||
|
||||
func (s *WebServer) Routes(r *mux.Router) {
|
||||
r.HandleFunc("/health", s.handleHealth).Methods(http.MethodGet)
|
||||
r.HandleFunc("/version", s.handleVersion).Methods(http.MethodGet)
|
||||
for _, register := range appRouteRegistrars {
|
||||
register(s, r)
|
||||
}
|
||||
}
|
||||
|
||||
const (
|
||||
healthOK = `{"status":"ok"}`
|
||||
healthUnavailable = `{"status":"unavailable"}`
|
||||
)
|
||||
|
||||
func (s *WebServer) handleHealth(w http.ResponseWriter, r *http.Request) {
|
||||
status, body := http.StatusOK, healthOK
|
||||
ctx, cancel := context.WithTimeout(r.Context(), healthPingTimeout)
|
||||
defer cancel()
|
||||
if s.pinger == nil {
|
||||
status, body = http.StatusServiceUnavailable, healthUnavailable
|
||||
} else if err := s.pinger.PingContext(ctx); err != nil {
|
||||
s.log.ErrorContext(r.Context(), "health check ping failed", "error", err)
|
||||
status, body = http.StatusServiceUnavailable, healthUnavailable
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Header().Set("Cache-Control", "no-store")
|
||||
w.WriteHeader(status)
|
||||
_, _ = w.Write([]byte(body))
|
||||
}
|
||||
Reference in new issue
Block a user