Merge branch 'main' of git.samantha42.xyz:samantha/Portifolio-Engine
This commit is contained in:
18
main.go
18
main.go
@@ -5,6 +5,7 @@ import (
|
||||
"Portifolio/internal/service"
|
||||
"Portifolio/internal/website"
|
||||
"database/sql"
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
@@ -17,10 +18,16 @@ var db *sql.DB
|
||||
func corsMiddleware(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
log.Printf("Incoming request: %s %s from %s", r.Method, r.URL.Path, r.Header.Get("Origin"))
|
||||
|
||||
w.Header().Set("Access-Control-Allow-Origin", "http://localhost:8081")
|
||||
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS")
|
||||
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization")
|
||||
|
||||
if r.Method == http.MethodOptions {
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
return
|
||||
}
|
||||
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
@@ -62,6 +69,14 @@ func RegisterRoutes(mux *http.ServeMux, db *sql.DB) {
|
||||
mux.HandleFunc("POST /api/v1/revenue/add", svc.AddRevenueEntryHandler())
|
||||
}
|
||||
func main() {
|
||||
// --- CLI flags ---
|
||||
port := flag.String("port", "8080", "Port to listen on")
|
||||
host := flag.String("host", "", "Host/IP to listen on (default: all interfaces)")
|
||||
flag.Parse()
|
||||
|
||||
addr := fmt.Sprintf("%s:%s", *host, *port)
|
||||
|
||||
// --- Database ---
|
||||
var err error
|
||||
db, err = sql.Open("sqlite3", "./app.db?_foreign_keys=on")
|
||||
if err != nil {
|
||||
@@ -76,9 +91,10 @@ func main() {
|
||||
database.InitDB(db)
|
||||
fmt.Println("Connected to SQLite database")
|
||||
|
||||
// --- Routes ---
|
||||
mux := http.NewServeMux()
|
||||
RegisterRoutes(mux, db)
|
||||
|
||||
fmt.Println("Server running on :8080")
|
||||
http.ListenAndServe(":8080", corsMiddleware(mux))
|
||||
http.ListenAndServe(addr, corsMiddleware(mux))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user