Files
Archivum/backend/internal/graph/server.go
brasse b 87821e8988 fix: server starts even without config (REQUIRE_SETUP mode)
main.go no longer fatals on ErrRequireSetup — it passes cfg=nil to NewServer.
NewServer returns REQUIRE_SETUP on every GraphQL call when cfg is nil, and
also adds CORS headers and serves the Vue SPA from /srv/archivum/ui.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-10 13:25:23 +02:00

45 lines
1.3 KiB
Go

package graph
import (
"net/http"
"github.com/brasse-b/archivum/internal/config"
)
// NewServer wires up the HTTP handler for the GraphQL endpoint.
// cfg may be nil when the system has not been configured yet; in that case
// every GraphQL request returns {"data":{"systemStatus":"REQUIRE_SETUP"}} so
// the frontend can redirect to the Setup Wizard.
func NewServer(cfg *config.Config) http.Handler {
mux := http.NewServeMux()
mux.HandleFunc("/graphql", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization")
if r.Method == http.MethodOptions {
w.WriteHeader(http.StatusNoContent)
return
}
if cfg == nil {
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(`{"data":{"systemStatus":"REQUIRE_SETUP"}}`))
return
}
// TODO: replace with gqlgen handler once generated.
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(`{"data":{"systemStatus":"OK"}}`))
})
mux.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
})
// Serve the embedded Vue SPA for every other path.
mux.Handle("/", http.FileServer(http.Dir("/srv/archivum/ui")))
return mux
}