2024-03-18 10:34:43 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-04-16 21:58:39 +00:00
|
|
|
"fmt"
|
|
|
|
"html/template"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
2024-07-31 12:45:34 +00:00
|
|
|
"path/filepath"
|
2024-04-16 21:58:39 +00:00
|
|
|
|
2024-07-31 03:09:22 +00:00
|
|
|
"arimelody.me/arimelody.me/api/v1/admin"
|
2024-04-16 21:58:39 +00:00
|
|
|
"arimelody.me/arimelody.me/api/v1/music"
|
2024-07-31 12:45:34 +00:00
|
|
|
"arimelody.me/arimelody.me/global"
|
2024-03-18 10:34:43 +00:00
|
|
|
)
|
|
|
|
|
2024-07-31 03:09:22 +00:00
|
|
|
const DEFAULT_PORT int = 8080
|
2024-03-18 10:34:43 +00:00
|
|
|
|
2024-07-31 03:09:22 +00:00
|
|
|
// func handle_music(res http.ResponseWriter, req *http.Request, root *template.Template) int {
|
|
|
|
// if !global.IsModified(req, global.LAST_MODIFIED) {
|
|
|
|
// res.WriteHeader(304)
|
|
|
|
// return 304
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// music_template := template.Must(root.ParseFiles("views/music.html"))
|
|
|
|
// err := music_template.Execute(res, music.Releases)
|
|
|
|
// if err != nil {
|
|
|
|
// http.Error(res, err.Error(), http.StatusInternalServerError)
|
|
|
|
// return 500
|
|
|
|
// }
|
|
|
|
// return 200
|
|
|
|
// }
|
2024-07-31 12:45:34 +00:00
|
|
|
|
2024-07-31 03:09:22 +00:00
|
|
|
// func handle_music_gateway(res http.ResponseWriter, req *http.Request, root *template.Template) int {
|
|
|
|
// if !global.IsModified(req, global.LAST_MODIFIED) {
|
|
|
|
// res.WriteHeader(304)
|
|
|
|
// return 304
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// id := req.URL.Path[len("/music/"):]
|
|
|
|
// release, err := music.GetRelease(id)
|
|
|
|
// if err != nil {
|
|
|
|
// return handle_not_found(res, req, root)
|
|
|
|
// }
|
|
|
|
// gateway_template := template.Must(root.ParseFiles("views/music-gateway.html"))
|
|
|
|
// err = gateway_template.Execute(res, release)
|
|
|
|
// if err != nil {
|
|
|
|
// http.Error(res, err.Error(), http.StatusInternalServerError)
|
|
|
|
// return 500
|
|
|
|
// }
|
|
|
|
// return 200
|
|
|
|
// }
|
2024-03-18 20:19:04 +00:00
|
|
|
|
2024-03-18 10:34:43 +00:00
|
|
|
func main() {
|
2024-07-31 03:09:22 +00:00
|
|
|
db := InitDatabase()
|
|
|
|
defer db.Close()
|
|
|
|
|
|
|
|
var err error
|
|
|
|
music.Artists, err = PullAllArtists(db)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Failed to pull artists from database: %v\n", err);
|
|
|
|
panic(1)
|
|
|
|
}
|
|
|
|
music.Releases, err = PullAllReleases(db)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Failed to pull releases from database: %v\n", err);
|
|
|
|
panic(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
|
2024-07-31 12:45:34 +00:00
|
|
|
mux.Handle("/api/v1/admin/", global.HTTPLog(http.StripPrefix("/api/v1/admin", admin.Handler())))
|
|
|
|
mux.Handle("/", http.FileServer(http.Dir("./public")))
|
|
|
|
// mux.Handle("/", global.HTTPLog(http.HandlerFunc(serveTemplate)))
|
2024-07-31 03:09:22 +00:00
|
|
|
|
|
|
|
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))
|
2024-03-18 10:34:43 +00:00
|
|
|
}
|
2024-07-31 12:45:34 +00:00
|
|
|
|
|
|
|
func serveTemplate(w http.ResponseWriter, r *http.Request) {
|
|
|
|
lp_base := 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(r.URL.Path))
|
|
|
|
|
|
|
|
template, _ := template.ParseFiles(lp_base, lp_header, lp_footer, lp_prideflag, fp)
|
|
|
|
template.ExecuteTemplate(w, "layout", nil)
|
|
|
|
}
|