feat: bootstrap temporserv project scaffold
Add the initial project blueprint, Go backend skeleton, frontend app shell, database schema draft, and local development/deployment files.
This commit is contained in:
23
internal/auth/service.go
Normal file
23
internal/auth/service.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package auth
|
||||
|
||||
import "time"
|
||||
|
||||
type User struct {
|
||||
ID string `json:"id"`
|
||||
Username string `json:"username"`
|
||||
IsAdmin bool `json:"isAdmin"`
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
LastLoginAt time.Time `json:"lastLoginAt"`
|
||||
}
|
||||
|
||||
func DemoUser() User {
|
||||
now := time.Now().UTC()
|
||||
return User{
|
||||
ID: "user-demo",
|
||||
Username: "demo",
|
||||
IsAdmin: true,
|
||||
CreatedAt: now,
|
||||
LastLoginAt: now,
|
||||
}
|
||||
}
|
||||
|
||||
36
internal/config/config.go
Normal file
36
internal/config/config.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package config
|
||||
|
||||
import "os"
|
||||
|
||||
type Config struct {
|
||||
AppEnv string
|
||||
ServerHost string
|
||||
ServerPort string
|
||||
DatabasePath string
|
||||
MediaRoot string
|
||||
CORSOrigins string
|
||||
}
|
||||
|
||||
func Load() Config {
|
||||
return Config{
|
||||
AppEnv: getenv("APP_ENV", "development"),
|
||||
ServerHost: getenv("SERVER_HOST", "0.0.0.0"),
|
||||
ServerPort: getenv("SERVER_PORT", "4040"),
|
||||
DatabasePath: getenv("DATABASE_PATH", "./data/app.db"),
|
||||
MediaRoot: getenv("MEDIA_ROOT", "./media"),
|
||||
CORSOrigins: getenv("CORS_ORIGINS", "http://localhost:5173"),
|
||||
}
|
||||
}
|
||||
|
||||
func (c Config) Address() string {
|
||||
return c.ServerHost + ":" + c.ServerPort
|
||||
}
|
||||
|
||||
func getenv(key, fallback string) string {
|
||||
value := os.Getenv(key)
|
||||
if value == "" {
|
||||
return fallback
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
53
internal/httpapi/middleware.go
Normal file
53
internal/httpapi/middleware.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package httpapi
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
func requestLogger(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
startedAt := time.Now()
|
||||
next.ServeHTTP(w, r)
|
||||
log.Printf("%s %s %s", r.Method, r.URL.Path, time.Since(startedAt))
|
||||
})
|
||||
}
|
||||
|
||||
func recoverer(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
defer func() {
|
||||
if recover() != nil {
|
||||
http.Error(w, "internal server error", http.StatusInternalServerError)
|
||||
}
|
||||
}()
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
|
||||
func cors(origins string) func(http.Handler) http.Handler {
|
||||
allowed := strings.Split(origins, ",")
|
||||
|
||||
return func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
origin := r.Header.Get("Origin")
|
||||
for _, candidate := range allowed {
|
||||
if strings.TrimSpace(candidate) == origin {
|
||||
w.Header().Set("Access-Control-Allow-Origin", origin)
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization")
|
||||
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, PATCH, DELETE, OPTIONS")
|
||||
|
||||
if r.Method == http.MethodOptions {
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
return
|
||||
}
|
||||
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
}
|
||||
81
internal/httpapi/router.go
Normal file
81
internal/httpapi/router.go
Normal file
@@ -0,0 +1,81 @@
|
||||
package httpapi
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
|
||||
"github.com/benya/temporserv/internal/auth"
|
||||
"github.com/benya/temporserv/internal/config"
|
||||
"github.com/benya/temporserv/internal/library"
|
||||
"github.com/benya/temporserv/internal/subsonic"
|
||||
)
|
||||
|
||||
func NewRouter(cfg config.Config) http.Handler {
|
||||
r := chi.NewRouter()
|
||||
r.Use(requestLogger)
|
||||
r.Use(recoverer)
|
||||
r.Use(cors(cfg.CORSOrigins))
|
||||
|
||||
r.Get("/health", func(w http.ResponseWriter, r *http.Request) {
|
||||
writeJSON(w, http.StatusOK, map[string]any{
|
||||
"status": "ok",
|
||||
"time": time.Now().UTC(),
|
||||
"env": cfg.AppEnv,
|
||||
})
|
||||
})
|
||||
|
||||
r.Route("/api", func(api chi.Router) {
|
||||
api.Get("/me", func(w http.ResponseWriter, r *http.Request) {
|
||||
writeJSON(w, http.StatusOK, auth.DemoUser())
|
||||
})
|
||||
api.Get("/home", func(w http.ResponseWriter, r *http.Request) {
|
||||
writeJSON(w, http.StatusOK, library.DemoHome())
|
||||
})
|
||||
api.Get("/tracks", func(w http.ResponseWriter, r *http.Request) {
|
||||
writeJSON(w, http.StatusOK, map[string]any{
|
||||
"items": library.DemoTracks(),
|
||||
})
|
||||
})
|
||||
api.Post("/auth/login", func(w http.ResponseWriter, r *http.Request) {
|
||||
writeJSON(w, http.StatusOK, map[string]any{
|
||||
"token": "dev-token",
|
||||
"user": auth.DemoUser(),
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
r.Route("/rest", func(rest chi.Router) {
|
||||
rest.Get("/ping.view", func(w http.ResponseWriter, r *http.Request) {
|
||||
writeJSON(w, http.StatusOK, subsonic.PingResponse())
|
||||
})
|
||||
rest.Get("/getLicense.view", func(w http.ResponseWriter, r *http.Request) {
|
||||
writeJSON(w, http.StatusOK, subsonic.PingResponse())
|
||||
})
|
||||
})
|
||||
|
||||
fs := http.FileServer(http.Dir("./web"))
|
||||
r.Handle("/*", spaFallback(fs))
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
func writeJSON(w http.ResponseWriter, status int, payload any) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(status)
|
||||
_ = json.NewEncoder(w).Encode(payload)
|
||||
}
|
||||
|
||||
func spaFallback(next http.Handler) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
if strings.HasPrefix(r.URL.Path, "/api") || strings.HasPrefix(r.URL.Path, "/rest") || strings.HasPrefix(r.URL.Path, "/health") {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
next.ServeHTTP(w, r)
|
||||
}
|
||||
}
|
||||
|
||||
56
internal/library/service.go
Normal file
56
internal/library/service.go
Normal file
@@ -0,0 +1,56 @@
|
||||
package library
|
||||
|
||||
type Artist struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
AlbumCount int `json:"albumCount"`
|
||||
}
|
||||
|
||||
type Album struct {
|
||||
ID string `json:"id"`
|
||||
ArtistID string `json:"artistId"`
|
||||
ArtistName string `json:"artistName"`
|
||||
Title string `json:"title"`
|
||||
Year int `json:"year"`
|
||||
TrackCount int `json:"trackCount"`
|
||||
}
|
||||
|
||||
type Track struct {
|
||||
ID string `json:"id"`
|
||||
AlbumID string `json:"albumId"`
|
||||
ArtistID string `json:"artistId"`
|
||||
Title string `json:"title"`
|
||||
ArtistName string `json:"artistName"`
|
||||
AlbumTitle string `json:"albumTitle"`
|
||||
TrackNumber int `json:"trackNumber"`
|
||||
DurationSecs int `json:"durationSeconds"`
|
||||
}
|
||||
|
||||
type HomePayload struct {
|
||||
RecentAlbums []Album `json:"recentAlbums"`
|
||||
Artists []Artist `json:"artists"`
|
||||
}
|
||||
|
||||
func DemoHome() HomePayload {
|
||||
return HomePayload{
|
||||
RecentAlbums: []Album{
|
||||
{ID: "album-1", ArtistID: "artist-1", ArtistName: "Tycho", Title: "Awake", Year: 2014, TrackCount: 8},
|
||||
{ID: "album-2", ArtistID: "artist-2", ArtistName: "Bonobo", Title: "Migration", Year: 2017, TrackCount: 11},
|
||||
{ID: "album-3", ArtistID: "artist-3", ArtistName: "Boards of Canada", Title: "Tomorrow's Harvest", Year: 2013, TrackCount: 17},
|
||||
},
|
||||
Artists: []Artist{
|
||||
{ID: "artist-1", Name: "Tycho", AlbumCount: 4},
|
||||
{ID: "artist-2", Name: "Bonobo", AlbumCount: 6},
|
||||
{ID: "artist-3", Name: "Boards of Canada", AlbumCount: 7},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func DemoTracks() []Track {
|
||||
return []Track{
|
||||
{ID: "track-1", AlbumID: "album-1", ArtistID: "artist-1", Title: "Awake", ArtistName: "Tycho", AlbumTitle: "Awake", TrackNumber: 1, DurationSecs: 224},
|
||||
{ID: "track-2", AlbumID: "album-2", ArtistID: "artist-2", Title: "Migration", ArtistName: "Bonobo", AlbumTitle: "Migration", TrackNumber: 1, DurationSecs: 301},
|
||||
{ID: "track-3", AlbumID: "album-3", ArtistID: "artist-3", Title: "Reach for the Dead", ArtistName: "Boards of Canada", AlbumTitle: "Tomorrow's Harvest", TrackNumber: 1, DurationSecs: 292},
|
||||
}
|
||||
}
|
||||
|
||||
26
internal/subsonic/service.go
Normal file
26
internal/subsonic/service.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package subsonic
|
||||
|
||||
type Envelope struct {
|
||||
SubsonicResponse Response `json:"subsonic-response"`
|
||||
}
|
||||
|
||||
type Response struct {
|
||||
Status string `json:"status"`
|
||||
Version string `json:"version"`
|
||||
Type string `json:"type"`
|
||||
Server string `json:"serverVersion"`
|
||||
OpenAPI bool `json:"openSubsonic"`
|
||||
}
|
||||
|
||||
func PingResponse() Envelope {
|
||||
return Envelope{
|
||||
SubsonicResponse: Response{
|
||||
Status: "ok",
|
||||
Version: "1.16.1",
|
||||
Type: "temporserv",
|
||||
Server: "0.1.0",
|
||||
OpenAPI: true,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user