Merge branch 'dev/cedric' into dev/guerby

This commit is contained in:
gom-by
2025-01-17 15:40:06 +01:00
26 changed files with 515 additions and 97 deletions

View File

@@ -3,6 +3,7 @@ package main
import (
"fmt"
"github.com/joho/godotenv"
"github.com/uptrace/bun/extra/bundebug"
"log"
"net/http"
"os"
@@ -16,6 +17,21 @@ import (
func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "<html><body><h1>Hello, World!</h1></body></html>")
}
func Cors(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Allow all origins (can restrict to specific origins)
w.Header().Set("Access-Control-Allow-Origin", "*")
// Allow certain HTTP methods (you can customize these as needed)
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS, PATCH")
// Allow certain headers (you can add more as needed)
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization")
// Handle OPTIONS pre-flight request
if r.Method == http.MethodOptions {
return
}
next.ServeHTTP(w, r)
})
}
func main() {
err := godotenv.Load()
@@ -23,6 +39,7 @@ func main() {
log.Fatalf("Error loading .env file: %v", err)
}
environ := os.Getenv("ENVIRONMENT")
port := os.Getenv("BACKEND_DOCKER_PORT")
if environ == "DEV" {
port = os.Getenv("BACKEND_PORT")
@@ -41,15 +58,22 @@ func main() {
log.Fatal(err)
}
core.DB.AddQueryHook(bundebug.NewQueryHook(bundebug.WithVerbose(true)))
defer core.DB.Close()
mux := http.NewServeMux()
core.HandleRoutes(mux, map[string]core.Handler{
"/": {
Handler: handler,
Middlewares: []core.Middleware{api.Methods("post")}},
Middlewares: []core.Middleware{api.Methods("get")}},
"/users/login": {
Handler: api.HandleLogin,
Middlewares: []core.Middleware{api.Methods("POST")}},
"/users/me": {
Handler: api.HandleGetMe,
Middlewares: []core.Middleware{api.Methods("GET"), api.AuthJWT}},
"/users": {
Handler: api.HandleGetUsers,
Middlewares: []core.Middleware{api.Methods("GET"), api.AuthJWT}},
@@ -76,7 +100,7 @@ func main() {
})
fmt.Printf("Serving on port %s\n", port)
err = http.ListenAndServe(fmt.Sprintf(":%s", port), mux)
err = http.ListenAndServe(fmt.Sprintf(":%s", port), Cors(mux))
if err != nil {
fmt.Printf("Error starting server: %s\n", err)
}