Implement web API foundation

Add application container, Gin router, graceful shutdown handler,
and version endpoint. This establishes the skeleton for the WebDisk
HTTP API as described in the architecture.

- Add internal/app/WebApp for runtime dependencies and version
- Add internal/server/router with GET /api/v1/version route
- Add graceful shutdown runner with signal handling in cmd/serve
- Add internal/api/ErrorResponse for standard HTTP error body
- Update roadmap, architecture, and decisions documentation
This commit is contained in:
2026-04-27 23:06:06 +08:00
parent c0c34eb914
commit 7fb125ea87
15 changed files with 469 additions and 32 deletions

View File

@@ -0,0 +1,48 @@
package server
import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"github.com/dhao2001/mygo/internal/app"
"github.com/dhao2001/mygo/internal/config"
)
func TestVersionRoute(t *testing.T) {
webApp := app.NewWebApp(&config.Config{})
router := NewRouter(webApp)
req := httptest.NewRequest(http.MethodGet, "/api/v1/version", nil)
rec := httptest.NewRecorder()
router.ServeHTTP(rec, req)
if rec.Code != http.StatusOK {
t.Fatalf("status = %d, want %d", rec.Code, http.StatusOK)
}
if got := rec.Header().Get("Content-Type"); got != "application/json; charset=utf-8" {
t.Fatalf("content-type = %q, want %q", got, "application/json; charset=utf-8")
}
var body struct {
Version string `json:"version"`
}
if err := json.Unmarshal(rec.Body.Bytes(), &body); err != nil {
t.Fatalf("unmarshal response: %v", err)
}
if body.Version != app.AppVersion {
t.Errorf("version = %q, want %q", body.Version, app.AppVersion)
}
}
func TestAddress(t *testing.T) {
got := Address(config.ServerConfig{Host: "127.0.0.1", Port: 8080})
want := "127.0.0.1:8080"
if got != want {
t.Errorf("Address() = %q, want %q", got, want)
}
}