arimelody.me/main.go

89 lines
2.7 KiB
Go
Raw Normal View History

package main
import (
"fmt"
"html/template"
"log"
"net/http"
"path/filepath"
"arimelody.me/arimelody.me/api/v1/admin"
"arimelody.me/arimelody.me/api/v1/music"
"arimelody.me/arimelody.me/global"
)
const DEFAULT_PORT int = 8080
// 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
// }
// 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
// }
func main() {
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()
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)))
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 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)
}