100 lines
3.9 KiB
Go
100 lines
3.9 KiB
Go
// Throwaway local preview server — NOT part of the real app, never
|
|
// committed. Bypasses OIDC entirely (no live provider needed for a visual
|
|
// check) by minting a session JWT directly and seeding just a user (no car
|
|
// — this drives the FULL real UI flow end-to-end, same as a real user
|
|
// would, to catch anything a direct-API test wouldn't).
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"io/fs"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"vlog/internal/auth"
|
|
"vlog/internal/db"
|
|
"vlog/internal/handlers"
|
|
"vlog/internal/storage"
|
|
)
|
|
|
|
func main() {
|
|
dbPath := "/tmp/vlog-preview/preview.db"
|
|
os.MkdirAll("/tmp/vlog-preview/uploads", 0o755)
|
|
os.Remove(dbPath)
|
|
|
|
database, err := db.Connect("sqlite", dbPath)
|
|
if err != nil {
|
|
log.Fatalf("db: %v", err)
|
|
}
|
|
defer database.Close()
|
|
|
|
store, err := storage.NewLocal("/tmp/vlog-preview/uploads")
|
|
if err != nil {
|
|
log.Fatalf("storage: %v", err)
|
|
}
|
|
|
|
h := handlers.New(database, store, nil, "preview-secret", false)
|
|
|
|
userID := uuid.New().String()
|
|
database.Exec(`INSERT INTO users (id, oidc_sub, email, name) VALUES (?, ?, ?, ?)`,
|
|
userID, "preview-sub", "[email protected]", "Demo User")
|
|
|
|
token, err := auth.MintSessionJWT("preview-secret", userID, "[email protected]")
|
|
if err != nil {
|
|
log.Fatalf("mint jwt: %v", err)
|
|
}
|
|
fmt.Println("SESSION_TOKEN=" + token)
|
|
|
|
mux := http.NewServeMux()
|
|
protected := http.NewServeMux()
|
|
protected.HandleFunc("GET /api/me", h.Me)
|
|
|
|
protected.HandleFunc("GET /api/cars", h.ListCars)
|
|
protected.HandleFunc("POST /api/cars", h.CreateCar)
|
|
protected.HandleFunc("GET /api/cars/{carId}", h.GetCar)
|
|
protected.HandleFunc("PUT /api/cars/{carId}", h.UpdateCar)
|
|
protected.HandleFunc("DELETE /api/cars/{carId}", h.DeleteCar)
|
|
protected.HandleFunc("POST /api/cars/{carId}/photo", h.UploadCarPhoto)
|
|
protected.HandleFunc("GET /api/cars/{carId}/photo", h.GetCarPhoto)
|
|
protected.HandleFunc("PATCH /api/cars/{carId}/odometer", h.UpdateOdometer)
|
|
protected.HandleFunc("POST /api/cars/{carId}/share", h.GenerateShareLink)
|
|
protected.HandleFunc("DELETE /api/cars/{carId}/share", h.RevokeShareLink)
|
|
protected.HandleFunc("GET /api/cars/{carId}/mileage", h.Mileage)
|
|
|
|
protected.HandleFunc("GET /api/cars/{carId}/service-entries", h.ListServiceEntries)
|
|
protected.HandleFunc("POST /api/cars/{carId}/service-entries", h.CreateServiceEntry)
|
|
protected.HandleFunc("GET /api/service-entries/{entryId}", h.GetServiceEntry)
|
|
protected.HandleFunc("PUT /api/service-entries/{entryId}", h.UpdateServiceEntry)
|
|
protected.HandleFunc("DELETE /api/service-entries/{entryId}", h.DeleteServiceEntry)
|
|
protected.HandleFunc("POST /api/service-entries/{entryId}/files", h.UploadServiceEntryFile)
|
|
protected.HandleFunc("GET /api/files/{fileId}", h.GetFile)
|
|
protected.HandleFunc("DELETE /api/files/{fileId}", h.DeleteFile)
|
|
|
|
protected.HandleFunc("GET /api/cars/{carId}/reminders", h.ListReminders)
|
|
protected.HandleFunc("POST /api/cars/{carId}/reminders", h.CreateReminder)
|
|
protected.HandleFunc("PUT /api/reminders/{reminderId}", h.UpdateReminder)
|
|
protected.HandleFunc("DELETE /api/reminders/{reminderId}", h.DeleteReminder)
|
|
|
|
protected.HandleFunc("GET /api/cars/{carId}/knowledge", h.ListKnowledge)
|
|
protected.HandleFunc("POST /api/cars/{carId}/knowledge", h.CreateKnowledge)
|
|
protected.HandleFunc("PUT /api/knowledge/{knowledgeId}", h.UpdateKnowledge)
|
|
protected.HandleFunc("DELETE /api/knowledge/{knowledgeId}", h.DeleteKnowledge)
|
|
|
|
protected.HandleFunc("GET /api/cars/{carId}/fuel-entries", h.ListFuelEntries)
|
|
protected.HandleFunc("POST /api/cars/{carId}/fuel-entries", h.CreateFuelEntry)
|
|
protected.HandleFunc("PUT /api/fuel-entries/{entryId}", h.UpdateFuelEntry)
|
|
protected.HandleFunc("DELETE /api/fuel-entries/{entryId}", h.DeleteFuelEntry)
|
|
|
|
protected.HandleFunc("GET /api/dashboard", h.Dashboard)
|
|
mux.Handle("/api/", auth.RequireAuth(h.JWTSecret)(protected))
|
|
|
|
staticFS, _ := fs.Sub(frontendFS, "web")
|
|
mux.Handle("/", http.FileServer(http.FS(staticFS)))
|
|
|
|
log.Println("preview server on :8099")
|
|
log.Fatal(http.ListenAndServe(":8099", mux))
|
|
}
|