feat: added http server
This commit is contained in:
parent
580594990d
commit
c83197abcf
|
@ -0,0 +1,2 @@
|
||||||
|
GREPFOOD_SYNCSERVER_PORT=8080
|
||||||
|
GREPFOOD_JWT_SECRET=9v8bnz7560983n765098z7nb30945
|
|
@ -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
|
|
@ -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=
|
|
@ -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))
|
||||||
|
}
|
File diff suppressed because one or more lines are too long
After Width: | Height: | Size: 34 KiB |
|
@ -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>
|
Loading…
Reference in New Issue