add proxy server for dynamic api endpoint handling and auth

This commit is contained in:
ju09279
2024-09-01 14:51:19 +02:00
parent 2bf269cf74
commit aea7e147e5
21 changed files with 4643 additions and 4328 deletions

40
proxy/routes.go Normal file
View File

@@ -0,0 +1,40 @@
package main
import (
"encoding/json"
"fmt"
"net/http"
)
func run() error {
mux := http.NewServeMux()
mux.Handle("/me", AuthMiddleware(http.HandlerFunc(MeHandler)))
mux.Handle("/login", http.HandlerFunc(LoginRedirect))
fmt.Println("Starting Server on :42000")
//global middleware
handler := CookieMiddleware(mux)
return http.ListenAndServe(":42000", handler)
}
func MeHandler(w http.ResponseWriter, r *http.Request) {
user := r.Context().Value("user").(*User)
w.Header().Set("Content-Type", "application/Json")
JSONResponse(w, http.StatusOK, user)
}
func JSONResponse(w http.ResponseWriter, statusCode int, data interface{}) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(statusCode)
if err := json.NewEncoder(w).Encode(data); err != nil {
http.Error(w, "Failed to encode response as JSON", http.StatusInternalServerError)
}
}