areweplaying/cmd/web/main.go
Harry Culpan fc5c986141 Consolidate log file env vars: AWP_LOG_FILE (web) and AWP_CLI_LOG_FILE (CLI)
Removes the duplicate log setup from cli/cmd/root.go that was reading
AWP_LOG_FILE — log file config now lives only in each binary's main.go.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-30 19:57:46 -04:00

67 lines
1.4 KiB
Go

package main
import (
"fmt"
"log"
"net/http"
"os"
"path/filepath"
"strings"
"github.com/go-chi/chi/v5"
"github.com/hculpan/areweplaying/pkg/utils"
"github.com/joho/godotenv"
)
var appKey []byte
func main() {
port := "8080"
godotenv.Load()
if p := os.Getenv("PORT"); p != "" {
port = p
}
appKey = []byte(os.Getenv("APP_KEY"))
if len(appKey) == 0 {
log.Fatal("unable to load APP_KEY")
}
logFile := os.Getenv("AWP_LOG_FILE")
if len(logFile) > 0 {
utils.SetLogFile(logFile)
}
r := chi.NewRouter()
workDir, _ := os.Getwd()
filesDir := filepath.Join(workDir, "static")
FileServer(r, "/static", http.Dir(filesDir))
routes(r)
log.Default().Printf("Starting up server on port :%s\n", port)
http.ListenAndServe(fmt.Sprintf(":%s", port), r)
}
// FileServer conveniently sets up a http.FileServer handler to serve
// static files from a http.FileSystem.
func FileServer(r chi.Router, path string, root http.FileSystem) {
if strings.ContainsAny(path, "{}*") {
panic("FileServer does not permit URL parameters.")
}
if path != "/" && path[len(path)-1] != '/' {
r.Get(path, http.RedirectHandler(path+"/", 301).ServeHTTP)
path += "/"
}
path += "*"
r.Get(path, func(w http.ResponseWriter, r *http.Request) {
rctx := chi.RouteContext(r.Context())
pathPrefix := strings.TrimSuffix(rctx.RoutePattern(), "/*")
fs := http.StripPrefix(pathPrefix, http.FileServer(root))
fs.ServeHTTP(w, r)
})
}