feat: added http server

This commit is contained in:
Sven Vogel 2024-10-15 08:17:28 +02:00
parent 580594990d
commit c83197abcf
6 changed files with 188 additions and 0 deletions

2
server/.env Normal file
View File

@ -0,0 +1,2 @@
GREPFOOD_SYNCSERVER_PORT=8080
GREPFOOD_JWT_SECRET=9v8bnz7560983n765098z7nb30945

5
server/go.mod Normal file
View File

@ -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

2
server/go.sum Normal file
View File

@ -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=

54
server/main.go Normal file
View File

@ -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))
}

111
server/static/image.svg Normal file

File diff suppressed because one or more lines are too long

After

Width:  |  Height:  |  Size: 34 KiB

14
server/static/index.html Normal file
View File

@ -0,0 +1,14 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Grepfood</title>
</head>
<body>
<div style="width: 50%; margin-left: auto; margin-right: auto; margin-top: 20em;">
<object width="100%" data="image.svg"></object>
</div>
</body>
</html>