refactoring everything teehee (i'm so glad this isn't a team project)
Signed-off-by: ari melody <ari@arimelody.me>
This commit is contained in:
parent
c684f0c7ae
commit
10f5f51e76
17 changed files with 970 additions and 547 deletions
102
main.go
102
main.go
|
@ -2,7 +2,6 @@ package main
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"html/template"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
|
@ -10,47 +9,57 @@ import (
|
|||
|
||||
"arimelody.me/arimelody.me/api/v1/admin"
|
||||
"arimelody.me/arimelody.me/api/v1/music"
|
||||
"arimelody.me/arimelody.me/db"
|
||||
"arimelody.me/arimelody.me/global"
|
||||
)
|
||||
|
||||
const DEFAULT_PORT int = 8080
|
||||
|
||||
func main() {
|
||||
db := InitDatabase()
|
||||
db := db.InitDatabase()
|
||||
defer db.Close()
|
||||
|
||||
var err error
|
||||
music.Artists, err = PullAllArtists(db)
|
||||
music.Artists, err = music.PullAllArtists(db)
|
||||
if err != nil {
|
||||
fmt.Printf("Failed to pull artists from database: %v\n", err);
|
||||
panic(1)
|
||||
}
|
||||
music.Releases, err = PullAllReleases(db)
|
||||
fmt.Printf("%d artists loaded successfully.\n", len(music.Artists))
|
||||
|
||||
music.Releases, err = music.PullAllReleases(db)
|
||||
if err != nil {
|
||||
fmt.Printf("Failed to pull releases from database: %v\n", err);
|
||||
panic(1)
|
||||
}
|
||||
fmt.Printf("%d releases loaded successfully.\n", len(music.Releases))
|
||||
|
||||
mux := http.NewServeMux()
|
||||
|
||||
mux.Handle("/api/v1/admin/", global.HTTPLog(http.StripPrefix("/api/v1/admin", admin.Handler())))
|
||||
|
||||
mux.Handle("/music/", global.HTTPLog(http.StripPrefix("/music", musicGatewayHandler())))
|
||||
mux.Handle("/music", global.HTTPLog(serveTemplate("music.html", music.Releases)))
|
||||
|
||||
mux.Handle("/", global.HTTPLog(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path == "/" || r.URL.Path == "/index.html" {
|
||||
serveTemplate("index.html", nil).ServeHTTP(w, r)
|
||||
return
|
||||
}
|
||||
staticHandler().ServeHTTP(w, r)
|
||||
})))
|
||||
mux := createServeMux()
|
||||
|
||||
port := DEFAULT_PORT
|
||||
fmt.Printf("now serving at http://127.0.0.1:%d\n", port)
|
||||
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", port), mux))
|
||||
}
|
||||
|
||||
func createServeMux() *http.ServeMux {
|
||||
mux := http.NewServeMux()
|
||||
|
||||
mux.Handle("/api/v1/admin/", global.HTTPLog(http.StripPrefix("/api/v1/admin", admin.Handler())))
|
||||
|
||||
mux.Handle("/music/", global.HTTPLog(http.StripPrefix("/music", music.ServeGateway())))
|
||||
mux.Handle("/music", global.HTTPLog(music.ServeCatalog()))
|
||||
|
||||
mux.Handle("/", global.HTTPLog(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path == "/" || r.URL.Path == "/index.html" {
|
||||
global.ServeTemplate("index.html", nil).ServeHTTP(w, r)
|
||||
return
|
||||
}
|
||||
staticHandler().ServeHTTP(w, r)
|
||||
})))
|
||||
|
||||
return mux
|
||||
}
|
||||
|
||||
func staticHandler() http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
info, err := os.Stat(filepath.Join("public", filepath.Clean(r.URL.Path)))
|
||||
|
@ -71,60 +80,3 @@ func staticHandler() http.Handler {
|
|||
http.FileServer(http.Dir("./public")).ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
|
||||
func musicGatewayHandler() http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
id := r.URL.Path[1:]
|
||||
release, err := music.GetRelease(id)
|
||||
if err != nil {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
lrw := global.LoggingResponseWriter{w, http.StatusOK}
|
||||
|
||||
serveTemplate("music-gateway.html", release).ServeHTTP(&lrw, r)
|
||||
|
||||
if lrw.Code != http.StatusOK {
|
||||
fmt.Printf("Error loading music gateway for %s\n", id)
|
||||
return
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func serveTemplate(page string, data any) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
lp_layout := filepath.Join("views", "layout.html")
|
||||
lp_header := filepath.Join("views", "header.html")
|
||||
lp_footer := filepath.Join("views", "footer.html")
|
||||
lp_prideflag := filepath.Join("views", "prideflag.html")
|
||||
fp := filepath.Join("views", filepath.Clean(page))
|
||||
|
||||
info, err := os.Stat(fp)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if info.IsDir() {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
template, err := template.ParseFiles(lp_layout, lp_header, lp_footer, lp_prideflag, fp)
|
||||
if err != nil {
|
||||
fmt.Printf("Error parsing template files: %s\n", err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
err = template.ExecuteTemplate(w, "layout.html", data)
|
||||
if err != nil {
|
||||
fmt.Printf("Error executing template: %s\n", err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue