From 3cfeca65fc8cf84a6c6af83c1efbb0aad0dbe94a Mon Sep 17 00:00:00 2001 From: Teajey <21069848+Teajey@users.noreply.github.com> Date: Thu, 18 Sep 2025 20:30:57 +0900 Subject: [PATCH] Health endpoint --- http/dev.sh | 4 ++++ http/env/env.go | 48 +++++++++++++++++++++++++++--------------- http/env/version.go | 3 +++ http/main.go | 8 +++---- http/routing/config.go | 25 ++++++++++++++++++++++ http/routing/home.go | 2 +- http/server/server.go | 3 ++- scripts/git-version | 3 +++ 8 files changed, 73 insertions(+), 23 deletions(-) create mode 100755 http/dev.sh create mode 100644 http/env/version.go create mode 100644 http/routing/config.go create mode 100755 scripts/git-version diff --git a/http/dev.sh b/http/dev.sh new file mode 100755 index 0000000..43c4bc8 --- /dev/null +++ b/http/dev.sh @@ -0,0 +1,4 @@ +top_level=$(git rev-parse --show-toplevel) +git_version=$($top_level/scripts/git-version) + +go run -ldflags=-X=lishwist/http/env.GitVersion=$git_version main.go diff --git a/http/env/env.go b/http/env/env.go index 10a998b..2c6a9f4 100644 --- a/http/env/env.go +++ b/http/env/env.go @@ -14,20 +14,34 @@ func GuaranteeEnv(key string) string { return variable } -var DatabaseFile = GuaranteeEnv("LISHWIST_DATABASE_FILE") -var SessionSecret = GuaranteeEnv("LISHWIST_SESSION_SECRET") -var HostRootUrl = GuaranteeEnv("LISHWIST_HOST_ROOT_URL") -var HostPort = os.Getenv("LISHWIST_HOST_PORT") -var ServePort = GuaranteeEnv("LISHWIST_SERVE_PORT") -var InDev = os.Getenv("LISHWIST_IN_DEV") != "" -var HostUrl = func() *url.URL { - rawUrl := HostRootUrl - if HostPort != "" { - rawUrl += ":" + HostPort - } - u, err := url.Parse(rawUrl) - if err != nil { - log.Fatalln("Couldn't parse host url:", err) - } - return u -}() +type Config struct { + DatabaseFile string + SessionSecret string + HostRootUrl string + HostPort string + ServePort string + InDev bool + HostUrl string +} + +var Configuration Config + +func init() { + Configuration.DatabaseFile = GuaranteeEnv("LISHWIST_DATABASE_FILE") + Configuration.SessionSecret = GuaranteeEnv("LISHWIST_SESSION_SECRET") + Configuration.HostRootUrl = GuaranteeEnv("LISHWIST_HOST_ROOT_URL") + Configuration.HostPort = os.Getenv("LISHWIST_HOST_PORT") + Configuration.ServePort = GuaranteeEnv("LISHWIST_SERVE_PORT") + Configuration.InDev = os.Getenv("LISHWIST_IN_DEV") != "" + Configuration.HostUrl = func() string { + rawUrl := Configuration.HostRootUrl + if Configuration.HostPort != "" { + rawUrl += ":" + Configuration.HostPort + } + u, err := url.Parse(rawUrl) + if err != nil { + log.Fatalln("Couldn't parse host url:", err) + } + return u.String() + }() +} diff --git a/http/env/version.go b/http/env/version.go new file mode 100644 index 0000000..57f13b3 --- /dev/null +++ b/http/env/version.go @@ -0,0 +1,3 @@ +package env + +var GitVersion string diff --git a/http/main.go b/http/main.go index a4ce39e..299700e 100644 --- a/http/main.go +++ b/http/main.go @@ -10,16 +10,16 @@ import ( ) func main() { - err := lishwist.Init(env.DatabaseFile) + err := lishwist.Init(env.Configuration.DatabaseFile) if err != nil { log.Fatalf("Failed to init Lishwist: %s\n", err) } - useSecureCookies := !env.InDev + useSecureCookies := !env.Configuration.InDev r := server.Create(useSecureCookies) - log.Printf("Running at http://127.0.0.1:%s\n", env.ServePort) - err = http.ListenAndServe(":"+env.ServePort, r) + log.Printf("Running at http://127.0.0.1:%s\n", env.Configuration.ServePort) + err = http.ListenAndServe(":"+env.Configuration.ServePort, r) if err != nil { log.Fatalln("Failed to listen and server:", err) } diff --git a/http/routing/config.go b/http/routing/config.go new file mode 100644 index 0000000..e7adaf4 --- /dev/null +++ b/http/routing/config.go @@ -0,0 +1,25 @@ +package routing + +import ( + lishwist "lishwist/core" + "lishwist/http/env" + "net/http" + + "github.com/Teajey/rsvp" +) + +type HealthProps struct { + GitVersion string + Config env.Config +} + +func Health(app *lishwist.Session, h http.Header, r *http.Request) rsvp.Response { + if app.Admin() == nil { + return rsvp.Ok() + } + + return rsvp.Response{Body: HealthProps{ + GitVersion: env.GitVersion, + Config: env.Configuration, + }} +} diff --git a/http/routing/home.go b/http/routing/home.go index 589b746..758fa60 100644 --- a/http/routing/home.go +++ b/http/routing/home.go @@ -37,7 +37,7 @@ func Home(app *lishwist.Session, h http.Header, r *http.Request) rsvp.Response { log.Printf("Failed to get groups: %s\n", err) return response.Error(http.StatusInternalServerError, "An error occurred while fetching your wishlist :(") } - p := HomeProps{Username: user.Name, Gifts: gifts, Todo: todo, Reference: user.Reference, HostUrl: env.HostUrl.String(), Groups: groups} + p := HomeProps{Username: user.Name, Gifts: gifts, Todo: todo, Reference: user.Reference, HostUrl: env.Configuration.HostUrl, Groups: groups} return response.Data("home.gotmpl", p) } diff --git a/http/server/server.go b/http/server/server.go index 11eb163..95c24f1 100644 --- a/http/server/server.go +++ b/http/server/server.go @@ -33,7 +33,7 @@ func Create(useSecureCookies bool) *router.VisibilityRouter { gob.Register(&api.RegisterProps{}) gob.Register(&api.LoginProps{}) - store := session.NewInMemoryStore([]byte(env.SessionSecret)) + store := session.NewInMemoryStore([]byte(env.Configuration.SessionSecret)) store.Options.MaxAge = 86_400 // 24 hours in seconds store.Options.Secure = useSecureCookies store.Options.HttpOnly = true @@ -49,6 +49,7 @@ func Create(useSecureCookies bool) *router.VisibilityRouter { r.Public.HandleFunc("POST /", routing.LoginPost) r.Public.HandleFunc("POST /register", routing.RegisterPost) + r.Private.HandleFunc("GET /health", routing.ExpectAppSession(routing.Health)) r.Private.HandleFunc("GET /", routing.NotFound) r.Private.HandleFunc("GET /groups", routing.ExpectAppSession(routing.Groups)) r.Private.HandleFunc("GET /groups/{groupReference}", routing.ExpectAppSession(routing.Group)) diff --git a/scripts/git-version b/scripts/git-version new file mode 100755 index 0000000..8e97e07 --- /dev/null +++ b/scripts/git-version @@ -0,0 +1,3 @@ +#!/bin/sh + +echo $(git rev-parse HEAD)$(test -n "$(git status --porcelain)" && echo "*")