diff --git a/server/.env b/server/.env new file mode 100644 index 0000000..bedd69f --- /dev/null +++ b/server/.env @@ -0,0 +1,2 @@ +GREPFOOD_SYNCSERVER_PORT=8080 +GREPFOOD_JWT_SECRET=9v8bnz7560983n765098z7nb30945 diff --git a/server/go.mod b/server/go.mod new file mode 100644 index 0000000..a057f25 --- /dev/null +++ b/server/go.mod @@ -0,0 +1,5 @@ +module git.montehaselino.de/grepfood-server-demo + +go 1.23.2 + +require github.com/golang-jwt/jwt v3.2.2+incompatible // indirect diff --git a/server/go.sum b/server/go.sum new file mode 100644 index 0000000..efdb2a9 --- /dev/null +++ b/server/go.sum @@ -0,0 +1,2 @@ +github.com/golang-jwt/jwt v3.2.2+incompatible h1:IfV12K8xAKAnZqdXVzCZ+TOjboZ2keLg81eXfW3O+oY= +github.com/golang-jwt/jwt v3.2.2+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I= diff --git a/server/main.go b/server/main.go new file mode 100644 index 0000000..4bbaf98 --- /dev/null +++ b/server/main.go @@ -0,0 +1,54 @@ +package main + +import ( + "log" + "net/http" + "encoding/json" + "os" + "github.com/golang-jwt/jwt" + "time" +) + +func GenerateJwt(username string) (string, error) { + token := jwt.New(jwt.SigningMethodEdDSA) + + claims := token.Claims.(jwt.MapClaims) + claims["exp"] = time.Now().Add(10 * time.Minute) + claims["authorized"] = true + claims["user"] = username + + tokenString, err := token.SignedString(os.Getenv("GREPFOOD_JWT_SECRET")) + if err != nil { + return "", err + } + + return tokenString, nil +} + +func WriteJson(w http.ResponseWriter, v any) { + w.Header().Set("Content-Type", "application/json") + + jsonData, err := json.Marshal(v) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + w.Write(jsonData) +} + +func handleVersion(w http.ResponseWriter, r *http.Request) { + version := struct {Version string `json:version`} { + Version: "0.1.0-demo", + } + + WriteJson(w, version) +} + +func main() { + + http.HandleFunc("/version", handleVersion) + http.Handle("/", http.FileServer(http.Dir("./static"))) + + log.Fatal(http.ListenAndServe(os.Getenv("GREPFOOD_SYNCSERVER_PORT"), nil)) +} diff --git a/server/static/image.svg b/server/static/image.svg new file mode 100644 index 0000000..42a59d5 --- /dev/null +++ b/server/static/image.svg @@ -0,0 +1,111 @@ + + + + diff --git a/server/static/index.html b/server/static/index.html new file mode 100644 index 0000000..2389538 --- /dev/null +++ b/server/static/index.html @@ -0,0 +1,14 @@ + + +
+ + + +