release edit page! + a lot of other stuff oml

Signed-off-by: ari melody <ari@arimelody.me>
This commit is contained in:
ari melody 2024-08-05 01:23:17 +01:00
parent f276ef1ff2
commit 10f19d46db
23 changed files with 981 additions and 347 deletions

View file

@ -15,12 +15,18 @@ import (
musicModel "arimelody.me/arimelody.me/music/model"
)
type loginData struct {
DiscordURI string
Token string
}
func Handler() http.Handler {
mux := http.NewServeMux()
mux.Handle("/login", LoginHandler())
mux.Handle("/logout", MustAuthorise(LogoutHandler()))
mux.Handle("/static/", http.StripPrefix("/static", staticHandler()))
mux.Handle("/release/", MustAuthorise(http.StripPrefix("/release", serveRelease())))
mux.Handle("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
http.NotFound(w, r)
@ -46,9 +52,9 @@ func Handler() http.Handler {
}
)
var tracks = []Track{}
for _, track := range global.Tracks {
if track.Release != nil { continue }
tracks = append(tracks, Track{
Track: track,
Lyrics: template.HTML(strings.Replace(track.Lyrics, "\n", "<br>", -1)),
@ -79,8 +85,9 @@ func MustAuthorise(next http.Handler) http.Handler {
}
func GetSession(r *http.Request) *Session {
// TODO: remove later- this bypasses auth!
return &Session{}
if os.Getenv("ARIMELODY_ADMIN_BYPASS") == "true" {
return &Session{}
}
var token = ""
// is the session token in context?
@ -137,7 +144,7 @@ func LoginHandler() http.Handler {
code := r.URL.Query().Get("code")
if code == "" {
serveTemplate("login.html", discord.REDIRECT_URI).ServeHTTP(w, r)
serveTemplate("login.html", loginData{DiscordURI: discord.REDIRECT_URI}).ServeHTTP(w, r)
return
}
@ -155,8 +162,9 @@ func LoginHandler() http.Handler {
return
}
if discord_user.Id != ADMIN_ID_DISCORD {
if discord_user.ID != ADMIN_ID_DISCORD {
// TODO: unauthorized user; revoke the token
fmt.Printf("Unauthorized login attempted: %s\n", discord_user.ID)
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
return
}
@ -174,16 +182,17 @@ func LoginHandler() http.Handler {
cookie.Path = "/"
http.SetCookie(w, &cookie)
w.WriteHeader(http.StatusOK)
w.Header().Add("Content-Type", "text/html")
w.Write([]byte(
"<!DOCTYPE html><html><head>"+
"<meta http-equiv=\"refresh\" content=\"5;url=/admin/\" />"+
"</head><body>"+
"Logged in successfully. "+
"You should be redirected to <a href=\"/admin/\">/admin/</a> in 5 seconds."+
"</body></html>"),
)
serveTemplate("login.html", loginData{Token: session.Token}).ServeHTTP(w, r)
// w.WriteHeader(http.StatusOK)
// w.Header().Add("Content-Type", "text/html")
// w.Write([]byte(
// "<!DOCTYPE html><html><head>"+
// "<meta http-equiv=\"refresh\" content=\"5;url=/admin/\" />"+
// "</head><body>"+
// "Logged in successfully. "+
// "You should be redirected to <a href=\"/admin/\">/admin/</a> in 5 seconds."+
// "</body></html>"),
// )
})
}
@ -194,36 +203,28 @@ func LogoutHandler() http.Handler {
return
}
token := r.Context().Value("token").(string)
if token == "" {
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
return
}
session := GetSession(r)
// remove this session from the list
sessions = func (token string) []*Session {
new_sessions := []*Session{}
for _, session := range sessions {
new_sessions = append(new_sessions, session)
if session.Token != token {
new_sessions = append(new_sessions, session)
}
}
return new_sessions
}(token)
}(session.Token)
w.WriteHeader(http.StatusOK)
w.Write([]byte(
"<meta http-equiv=\"refresh\" content=\"5;url=/\" />"+
"Logged out successfully. "+
"You should be redirected to <a href=\"/\">/</a> in 5 seconds."),
)
serveTemplate("logout.html", nil).ServeHTTP(w, r)
})
}
func serveTemplate(page string, data any) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
lp_layout := filepath.Join("views", "admin", "layout.html")
lp_layout := filepath.Join("admin", "views", "layout.html")
lp_prideflag := filepath.Join("views", "prideflag.html")
fp := filepath.Join("views", "admin", filepath.Clean(page))
fp := filepath.Join("admin", "views", filepath.Clean(page))
info, err := os.Stat(fp)
if err != nil {