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>
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
@@ -11,14 +12,20 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
cfg, err := config.Load(configPath())
|
cfg, err := config.Load(configPath())
|
||||||
if err != nil {
|
if err != nil && !errors.Is(err, config.ErrRequireSetup) {
|
||||||
log.Fatalf("failed to load config: %v", err)
|
log.Fatalf("failed to load config: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// cfg may be nil when REQUIRE_SETUP — NewServer handles that gracefully.
|
||||||
|
addr := ":4000"
|
||||||
|
if cfg != nil && cfg.ListenAddr != "" {
|
||||||
|
addr = cfg.ListenAddr
|
||||||
|
}
|
||||||
|
|
||||||
srv := graph.NewServer(cfg)
|
srv := graph.NewServer(cfg)
|
||||||
|
|
||||||
log.Printf("Archivum listening on %s", cfg.ListenAddr)
|
log.Printf("Archivum listening on %s", addr)
|
||||||
if err := http.ListenAndServe(cfg.ListenAddr, srv); err != nil {
|
if err := http.ListenAndServe(addr, srv); err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,22 +7,38 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// NewServer wires up the HTTP handler for the GraphQL endpoint.
|
// NewServer wires up the HTTP handler for the GraphQL endpoint.
|
||||||
// Replace the stub handler with the gqlgen-generated handler once
|
// cfg may be nil when the system has not been configured yet; in that case
|
||||||
// `go generate ./...` has been run.
|
// every GraphQL request returns {"data":{"systemStatus":"REQUIRE_SETUP"}} so
|
||||||
|
// the frontend can redirect to the Setup Wizard.
|
||||||
func NewServer(cfg *config.Config) http.Handler {
|
func NewServer(cfg *config.Config) http.Handler {
|
||||||
mux := http.NewServeMux()
|
mux := http.NewServeMux()
|
||||||
|
|
||||||
// Placeholder — replace with generated gqlgen handler.
|
|
||||||
mux.HandleFunc("/graphql", func(w http.ResponseWriter, r *http.Request) {
|
mux.HandleFunc("/graphql", func(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Header().Set("Content-Type", "application/json")
|
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.WriteHeader(http.StatusOK)
|
||||||
_, _ = w.Write([]byte(`{"data":{"systemStatus":"REQUIRE_SETUP"}}`))
|
_, _ = 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"}}`))
|
||||||
})
|
})
|
||||||
|
|
||||||
// Simple health check.
|
|
||||||
mux.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
|
mux.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// Serve the embedded Vue SPA for every other path.
|
||||||
|
mux.Handle("/", http.FileServer(http.Dir("/srv/archivum/ui")))
|
||||||
|
|
||||||
return mux
|
return mux
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user