package main import ( "fmt" "github.com/joho/godotenv" "github.com/uptrace/bun/extra/bundebug" "log" "net/http" "os" _ "github.com/lib/pq" api "fr.latosa-escrima/api" "fr.latosa-escrima/api/core" ) func handler(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "

Hello, World!

") } 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() if err != nil { log.Fatalf("Error loading .env file: %v", err) } environ := os.Getenv("ENVIRONMENT") port := os.Getenv("BACKEND_DOCKER_PORT") hostname := os.Getenv("DATABASE_HOSTNAME") if environ == "DEV" { port = os.Getenv("BACKEND_PORT") hostname = "localhost" } dsn := core.DSN{ Hostname: hostname, Port: os.Getenv("POSTGRES_PORT"), DBName: os.Getenv("POSTGRES_DB"), User: os.Getenv("POSTGRES_USER"), Password: os.Getenv("POSTGRES_PASSWORD"), } fmt.Println(dsn.ToString()) core.DB, err = core.InitDatabase(dsn) if err != nil { 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("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}}, "/users/new": { Handler: api.HandleCreateUser, Middlewares: []core.Middleware{api.Methods("POST"), api.AuthJWT}}, "/users/{user_uuid}": { Handler: api.HandleGetUser, Middlewares: []core.Middleware{api.Methods("GET"), api.AuthJWT}}, "/users/{user_uuid}/delete": { Handler: api.HandleDeleteUser, Middlewares: []core.Middleware{api.Methods("DELETE"), api.AuthJWT}}, "/users/{user_uuid}/update": { Handler: api.HandleUpdateUser, Middlewares: []core.Middleware{api.Methods("PATCH"), api.AuthJWT}}, // "/users/{user_uuid}/events": {Handler: nil, Middleware: nil}, // "/users/{user_uuid}/events/{event_uuid}": {Handler: nil, Middleware: nil}, // "/users/{user_uuid}/events/{event_uuid}/delete": {Handler: nil, Middleware: nil}, // "/users/{user_uuid}/events/{event_uuid}/update": {Handler: nil, Middleware: nil}, "/blogs/new": {Handler: api.HandleCreateBlog, Middlewares: nil}, "/blogs/{uuid}": {Handler: api.HandleGetBlog, Middlewares: nil}, }) fmt.Printf("Serving on port %s\n", port) err = http.ListenAndServe(fmt.Sprintf(":%s", port), Cors(mux)) if err != nil { fmt.Printf("Error starting server: %s\n", err) } }