Initial commit: gocarlog project — Milestones 1-10 completed
This commit is contained in:
@@ -0,0 +1,105 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"gocarlog/internal/db"
|
||||
"gocarlog/internal/router"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// Datenbank initialisieren
|
||||
database, err := db.NewDatabase("gocarlog.db")
|
||||
if err != nil {
|
||||
log.Fatalf("Fehler beim Initialisieren der Datenbank: %v", err)
|
||||
}
|
||||
defer database.Close()
|
||||
|
||||
// API-Router aufsetzen
|
||||
api := router.New(database)
|
||||
|
||||
// Statische Dateien ausliefern
|
||||
staticHandler := http.StripPrefix("/static/", http.FileServer(http.Dir("frontend")))
|
||||
|
||||
// Root-Handler: Frontend
|
||||
rootHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
http.ServeFile(w, r, "frontend/index.html")
|
||||
})
|
||||
|
||||
// Composite Router: statische Dateien und Frontend vor API
|
||||
// Reihenfolge ist wichtig: spezifische Pfade zuerst
|
||||
mux := &simpleMux{}
|
||||
mux.HandleFunc("GET", "/static/*", handleStaticPrefix(staticHandler))
|
||||
mux.HandleFunc("GET", "/", rootHandler)
|
||||
mux.addHandler(api)
|
||||
|
||||
port := os.Getenv("PORT")
|
||||
if port == "" {
|
||||
port = "8080"
|
||||
}
|
||||
|
||||
fmt.Printf("GoCarLog Server läuft auf http://localhost:%s\n", port)
|
||||
log.Fatal(http.ListenAndServe(":"+port, mux))
|
||||
}
|
||||
|
||||
// simpleMux ist ein einfacher Multiplexer, der den API-Router und statische Routen kombiniert.
|
||||
type simpleMux struct {
|
||||
routes []route
|
||||
api http.Handler
|
||||
}
|
||||
|
||||
type route struct {
|
||||
method string
|
||||
pattern string
|
||||
handler http.Handler
|
||||
}
|
||||
|
||||
func (m *simpleMux) HandleFunc(method, pattern string, handler http.Handler) {
|
||||
m.routes = append(m.routes, route{method: method, pattern: pattern, handler: handler})
|
||||
}
|
||||
|
||||
func (m *simpleMux) addHandler(h http.Handler) {
|
||||
m.api = h
|
||||
}
|
||||
|
||||
func (m *simpleMux) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
for _, rt := range m.routes {
|
||||
if rt.method != r.Method {
|
||||
continue
|
||||
}
|
||||
if matchPattern(rt.pattern, r.URL.Path) {
|
||||
rt.handler.ServeHTTP(w, r)
|
||||
return
|
||||
}
|
||||
}
|
||||
if m.api != nil {
|
||||
m.api.ServeHTTP(w, r)
|
||||
return
|
||||
}
|
||||
http.NotFound(w, r)
|
||||
}
|
||||
|
||||
// handleStaticPrefix delegiert auf den Static-Handler, wenn der Pfad mit /static/ beginnt.
|
||||
func handleStaticPrefix(h http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if len(r.URL.Path) > 8 && r.URL.Path[:8] == "/static/" {
|
||||
h.ServeHTTP(w, r)
|
||||
} else {
|
||||
http.NotFound(w, r)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// matchPattern prüft, ob ein pattern auf den path passt (unterstützt * als Wildcard).
|
||||
func matchPattern(pattern, path string) bool {
|
||||
if pattern == path {
|
||||
return true
|
||||
}
|
||||
if pattern[len(pattern)-1] == '*' {
|
||||
return len(path) >= len(pattern)-1 && path[:len(pattern)-1] == pattern[:len(pattern)-1]
|
||||
}
|
||||
return false
|
||||
}
|
||||
Reference in New Issue
Block a user