Health endpoint
This commit is contained in:
parent
a2932d7b1c
commit
3cfeca65fc
|
|
@ -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
|
||||
|
|
@ -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()
|
||||
}()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,3 @@
|
|||
package env
|
||||
|
||||
var GitVersion string
|
||||
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
}}
|
||||
}
|
||||
|
|
@ -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)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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))
|
||||
|
|
|
|||
|
|
@ -0,0 +1,3 @@
|
|||
#!/bin/sh
|
||||
|
||||
echo $(git rev-parse HEAD)$(test -n "$(git status --porcelain)" && echo "*")
|
||||
Loading…
Reference in New Issue