this is nowhere near working yet
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
2022-11-28 04:59:20 +01:00
parent 0c3797ec30
commit 75442d261d
19 changed files with 567 additions and 308 deletions

View File

@@ -0,0 +1,30 @@
package handlers
import (
"net/http"
"time"
)
func (s *Handlers) handleHealthCheck() http.HandlerFunc {
type response struct {
Status string `json:"status"`
Now string `json:"now"`
UptimeSeconds int64 `json:"uptime_seconds"`
UptimeHuman string `json:"uptime_human"`
Version string `json:"version"`
Appname string `json:"appname"`
Maintenance bool `json:"maintenance_mode"`
}
return func(w http.ResponseWriter, req *http.Request) {
resp := &response{
Status: "ok",
Now: time.Now().UTC().Format(time.RFC3339Nano),
UptimeSeconds: int64(s.uptime().Seconds()),
UptimeHuman: s.uptime().String(),
Maintenance: s.maintenance(),
Appname: s.appname,
Version: s.version,
}
s.respondJSON(w, req, resp, 200)
}
}

View File

@@ -0,0 +1,20 @@
package handlers
import (
"html/template"
"net/http"
"git.eeqj.de/sneak/gohttpserver/templates"
)
func (s *Handlers) HandleIndex() http.HandlerFunc {
indexTemplate := template.Must(template.New("index").Parse(templates.MustString("index.html")))
return func(w http.ResponseWriter, r *http.Request) {
err := indexTemplate.ExecuteTemplate(w, "index", nil)
if err != nil {
s.log.Println(err.Error())
http.Error(w, http.StatusText(500), 500)
}
}
}

View File

@@ -0,0 +1,12 @@
package handlers
import (
"fmt"
"net/http"
)
func (s *Handlers) HandleLogin() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "hello login")
}
}

View File

@@ -0,0 +1,15 @@
package handlers
import (
"net/http"
"time"
)
func (s *Handlers) HandleNow() http.HandlerFunc {
type response struct {
Now time.Time `json:"now"`
}
return func(w http.ResponseWriter, r *http.Request) {
s.respondJSON(w, r, &response{Now: time.Now()}, 200)
}
}

View File

@@ -0,0 +1,14 @@
package handlers
import (
"net/http"
)
// CHANGEME you probably want to remove this,
// this is just a handler/route that throws a panic to test
// sentry events.
func (s *Handlers) HandlePanic() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
panic("y tho")
}
}

View File

@@ -0,0 +1,61 @@
package handlers
import (
"context"
"encoding/json"
"net/http"
"time"
"git.eeqj.de/sneak/gohttpserver/internal/globals"
"github.com/rs/zerolog"
"go.uber.org/fx"
"google.golang.org/genproto/googleapis/spanner/admin/database/v1"
)
type HandlersParams struct {
fx.In
Logger *zerolog.Logger
Globals globals.Globals
Database database.Database
}
type Handlers struct {
params HandlersParams
log *zerolog.Logger
}
func New(lc fx.Lifecycle, params HandlersParams) (*Handlers, error) {
s := new(Handlers)
s.params = params
s.log = params.Logger.Get()
lc.Append(fx.Hook{
OnStart: func(ctx context.Context) error {
// FIXME compile some templates here or something
},
})
return s, nil
}
func (s *Handlers) HandleNow() http.HandlerFunc {
type response struct {
Now time.Time `json:"now"`
}
return func(w http.ResponseWriter, r *http.Request) {
s.respondJSON(w, r, &response{Now: time.Now()}, 200)
}
}
func (s *Handlers) respondJSON(w http.ResponseWriter, r *http.Request, data interface{}, status int) {
w.WriteHeader(status)
w.Header().Set("Content-Type", "application/json")
if data != nil {
err := json.NewEncoder(w).Encode(data)
if err != nil {
s.log.Error().Err(err).Msg("json encode error")
}
}
}
func (s *Handlers) decodeJSON(w http.ResponseWriter, r *http.Request, v interface{}) error { // nolint
return json.NewDecoder(r.Body).Decode(v)
}