refactored out global
. long live AppState
This commit is contained in:
parent
3d674515ce
commit
384579ee5e
24 changed files with 350 additions and 375 deletions
|
@ -8,10 +8,8 @@ import (
|
|||
"time"
|
||||
|
||||
"arimelody-web/controller"
|
||||
"arimelody-web/global"
|
||||
"arimelody-web/model"
|
||||
|
||||
"github.com/jmoiron/sqlx"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
|
@ -21,11 +19,11 @@ type TemplateData struct {
|
|||
Token string
|
||||
}
|
||||
|
||||
func AccountHandler(db *sqlx.DB) http.Handler {
|
||||
func AccountHandler(app *model.AppState) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
account := r.Context().Value("account").(*model.Account)
|
||||
|
||||
totps, err := controller.GetTOTPsForAccount(db, account.ID)
|
||||
totps, err := controller.GetTOTPsForAccount(app.DB, account.ID)
|
||||
if err != nil {
|
||||
fmt.Printf("WARN: Failed to fetch TOTPs: %v\n", err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
|
@ -47,10 +45,10 @@ func AccountHandler(db *sqlx.DB) http.Handler {
|
|||
})
|
||||
}
|
||||
|
||||
func LoginHandler(db *sqlx.DB) http.Handler {
|
||||
func LoginHandler(app *model.AppState) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method == http.MethodGet {
|
||||
account, err := controller.GetAccountByRequest(db, r)
|
||||
account, err := controller.GetAccountByRequest(app.DB, r)
|
||||
if err != nil {
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
fmt.Fprintf(os.Stderr, "WARN: Failed to fetch account: %v\n", err)
|
||||
|
@ -107,7 +105,7 @@ func LoginHandler(db *sqlx.DB) http.Handler {
|
|||
TOTP: r.Form.Get("totp"),
|
||||
}
|
||||
|
||||
account, err := controller.GetAccount(db, credentials.Username)
|
||||
account, err := controller.GetAccount(app.DB, credentials.Username)
|
||||
if err != nil {
|
||||
render(LoginResponse{ Message: "Invalid username or password" })
|
||||
return
|
||||
|
@ -123,7 +121,7 @@ func LoginHandler(db *sqlx.DB) http.Handler {
|
|||
return
|
||||
}
|
||||
|
||||
totps, err := controller.GetTOTPsForAccount(db, account.ID)
|
||||
totps, err := controller.GetTOTPsForAccount(app.DB, account.ID)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "WARN: Failed to fetch TOTPs: %v\n", err)
|
||||
render(LoginResponse{ Message: "Something went wrong. Please try again." })
|
||||
|
@ -147,7 +145,7 @@ func LoginHandler(db *sqlx.DB) http.Handler {
|
|||
}
|
||||
|
||||
// login success!
|
||||
token, err := controller.CreateToken(db, account.ID, r.UserAgent())
|
||||
token, err := controller.CreateToken(app.DB, account.ID, r.UserAgent())
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "WARN: Failed to create token: %v\n", err)
|
||||
render(LoginResponse{ Message: "Something went wrong. Please try again." })
|
||||
|
@ -155,10 +153,10 @@ func LoginHandler(db *sqlx.DB) http.Handler {
|
|||
}
|
||||
|
||||
cookie := http.Cookie{}
|
||||
cookie.Name = global.COOKIE_TOKEN
|
||||
cookie.Name = model.COOKIE_TOKEN
|
||||
cookie.Value = token.Token
|
||||
cookie.Expires = token.ExpiresAt
|
||||
if strings.HasPrefix(global.Config.BaseUrl, "https") {
|
||||
if strings.HasPrefix(app.Config.BaseUrl, "https") {
|
||||
cookie.Secure = true
|
||||
}
|
||||
cookie.HttpOnly = true
|
||||
|
@ -169,17 +167,17 @@ func LoginHandler(db *sqlx.DB) http.Handler {
|
|||
})
|
||||
}
|
||||
|
||||
func LogoutHandler(db *sqlx.DB) http.Handler {
|
||||
func LogoutHandler(app *model.AppState) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodGet {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
tokenStr := controller.GetTokenFromRequest(db, r)
|
||||
tokenStr := controller.GetTokenFromRequest(app.DB, r)
|
||||
|
||||
if len(tokenStr) > 0 {
|
||||
err := controller.DeleteToken(db, tokenStr)
|
||||
err := controller.DeleteToken(app.DB, tokenStr)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "WARN: Failed to revoke token: %v\n", err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
|
@ -188,10 +186,10 @@ func LogoutHandler(db *sqlx.DB) http.Handler {
|
|||
}
|
||||
|
||||
cookie := http.Cookie{}
|
||||
cookie.Name = global.COOKIE_TOKEN
|
||||
cookie.Name = model.COOKIE_TOKEN
|
||||
cookie.Value = ""
|
||||
cookie.Expires = time.Now()
|
||||
if strings.HasPrefix(global.Config.BaseUrl, "https") {
|
||||
if strings.HasPrefix(app.Config.BaseUrl, "https") {
|
||||
cookie.Secure = true
|
||||
}
|
||||
cookie.HttpOnly = true
|
||||
|
@ -201,9 +199,9 @@ func LogoutHandler(db *sqlx.DB) http.Handler {
|
|||
})
|
||||
}
|
||||
|
||||
func createAccountHandler(db *sqlx.DB) http.Handler {
|
||||
func createAccountHandler(app *model.AppState) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
checkAccount, err := controller.GetAccountByRequest(db, r)
|
||||
checkAccount, err := controller.GetAccountByRequest(app.DB, r)
|
||||
if err != nil {
|
||||
fmt.Printf("WARN: Failed to fetch account: %s\n", err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
|
@ -260,7 +258,7 @@ func createAccountHandler(db *sqlx.DB) http.Handler {
|
|||
}
|
||||
|
||||
// make sure code exists in DB
|
||||
invite, err := controller.GetInvite(db, credentials.Invite)
|
||||
invite, err := controller.GetInvite(app.DB, credentials.Invite)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "WARN: Failed to retrieve invite: %v\n", err)
|
||||
render(CreateAccountResponse{
|
||||
|
@ -270,7 +268,7 @@ func createAccountHandler(db *sqlx.DB) http.Handler {
|
|||
}
|
||||
if invite == nil || time.Now().After(invite.ExpiresAt) {
|
||||
if invite != nil {
|
||||
err := controller.DeleteInvite(db, invite.Code)
|
||||
err := controller.DeleteInvite(app.DB, invite.Code)
|
||||
if err != nil { fmt.Fprintf(os.Stderr, "WARN: Failed to delete expired invite: %v\n", err) }
|
||||
}
|
||||
render(CreateAccountResponse{
|
||||
|
@ -294,7 +292,7 @@ func createAccountHandler(db *sqlx.DB) http.Handler {
|
|||
Email: credentials.Email,
|
||||
AvatarURL: "/img/default-avatar.png",
|
||||
}
|
||||
err = controller.CreateAccount(db, &account)
|
||||
err = controller.CreateAccount(app.DB, &account)
|
||||
if err != nil {
|
||||
if strings.HasPrefix(err.Error(), "pq: duplicate key") {
|
||||
render(CreateAccountResponse{
|
||||
|
@ -309,11 +307,11 @@ func createAccountHandler(db *sqlx.DB) http.Handler {
|
|||
return
|
||||
}
|
||||
|
||||
err = controller.DeleteInvite(db, invite.Code)
|
||||
err = controller.DeleteInvite(app.DB, invite.Code)
|
||||
if err != nil { fmt.Fprintf(os.Stderr, "WARN: Failed to delete expired invite: %v\n", err) }
|
||||
|
||||
// registration success!
|
||||
token, err := controller.CreateToken(db, account.ID, r.UserAgent())
|
||||
token, err := controller.CreateToken(app.DB, account.ID, r.UserAgent())
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "WARN: Failed to create token: %v\n", err)
|
||||
// gracefully redirect user to login page
|
||||
|
@ -322,10 +320,10 @@ func createAccountHandler(db *sqlx.DB) http.Handler {
|
|||
}
|
||||
|
||||
cookie := http.Cookie{}
|
||||
cookie.Name = global.COOKIE_TOKEN
|
||||
cookie.Name = model.COOKIE_TOKEN
|
||||
cookie.Value = token.Token
|
||||
cookie.Expires = token.ExpiresAt
|
||||
if strings.HasPrefix(global.Config.BaseUrl, "https") {
|
||||
if strings.HasPrefix(app.Config.BaseUrl, "https") {
|
||||
cookie.Secure = true
|
||||
}
|
||||
cookie.HttpOnly = true
|
||||
|
|
|
@ -5,16 +5,15 @@ import (
|
|||
"net/http"
|
||||
"strings"
|
||||
|
||||
"arimelody-web/global"
|
||||
"arimelody-web/model"
|
||||
"arimelody-web/controller"
|
||||
)
|
||||
|
||||
func serveArtist() http.Handler {
|
||||
func serveArtist(app *model.AppState) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
slices := strings.Split(r.URL.Path[1:], "/")
|
||||
id := slices[0]
|
||||
artist, err := controller.GetArtist(global.DB, id)
|
||||
artist, err := controller.GetArtist(app.DB, id)
|
||||
if err != nil {
|
||||
if artist == nil {
|
||||
http.NotFound(w, r)
|
||||
|
@ -25,7 +24,7 @@ func serveArtist() http.Handler {
|
|||
return
|
||||
}
|
||||
|
||||
credits, err := controller.GetArtistCredits(global.DB, artist.ID, true)
|
||||
credits, err := controller.GetArtistCredits(app.DB, artist.ID, true)
|
||||
if err != nil {
|
||||
fmt.Printf("Error rendering admin track page for %s: %s\n", id, err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
|
|
|
@ -9,28 +9,26 @@ import (
|
|||
|
||||
"arimelody-web/controller"
|
||||
"arimelody-web/model"
|
||||
|
||||
"github.com/jmoiron/sqlx"
|
||||
)
|
||||
|
||||
func Handler(db *sqlx.DB) http.Handler {
|
||||
func Handler(app *model.AppState) http.Handler {
|
||||
mux := http.NewServeMux()
|
||||
|
||||
mux.Handle("/login", LoginHandler(db))
|
||||
mux.Handle("/register", createAccountHandler(db))
|
||||
mux.Handle("/logout", RequireAccount(db, LogoutHandler(db)))
|
||||
mux.Handle("/account", RequireAccount(db, AccountHandler(db)))
|
||||
mux.Handle("/login", LoginHandler(app))
|
||||
mux.Handle("/register", createAccountHandler(app))
|
||||
mux.Handle("/logout", RequireAccount(app, LogoutHandler(app)))
|
||||
mux.Handle("/account", RequireAccount(app, AccountHandler(app)))
|
||||
mux.Handle("/static/", http.StripPrefix("/static", staticHandler()))
|
||||
mux.Handle("/release/", RequireAccount(db, http.StripPrefix("/release", serveRelease())))
|
||||
mux.Handle("/artist/", RequireAccount(db, http.StripPrefix("/artist", serveArtist())))
|
||||
mux.Handle("/track/", RequireAccount(db, http.StripPrefix("/track", serveTrack())))
|
||||
mux.Handle("/release/", RequireAccount(app, http.StripPrefix("/release", serveRelease(app))))
|
||||
mux.Handle("/artist/", RequireAccount(app, http.StripPrefix("/artist", serveArtist(app))))
|
||||
mux.Handle("/track/", RequireAccount(app, http.StripPrefix("/track", serveTrack(app))))
|
||||
mux.Handle("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path != "/" {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
account, err := controller.GetAccountByRequest(db, r)
|
||||
account, err := controller.GetAccountByRequest(app.DB, r)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "WARN: Failed to fetch account: %s\n", err)
|
||||
}
|
||||
|
@ -39,21 +37,21 @@ func Handler(db *sqlx.DB) http.Handler {
|
|||
return
|
||||
}
|
||||
|
||||
releases, err := controller.GetAllReleases(db, false, 0, true)
|
||||
releases, err := controller.GetAllReleases(app.DB, false, 0, true)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "WARN: Failed to pull releases: %s\n", err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
artists, err := controller.GetAllArtists(db)
|
||||
artists, err := controller.GetAllArtists(app.DB)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "WARN: Failed to pull artists: %s\n", err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
tracks, err := controller.GetOrphanTracks(db)
|
||||
tracks, err := controller.GetOrphanTracks(app.DB)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "WARN: Failed to pull orphan tracks: %s\n", err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
|
@ -83,9 +81,9 @@ func Handler(db *sqlx.DB) http.Handler {
|
|||
return mux
|
||||
}
|
||||
|
||||
func RequireAccount(db *sqlx.DB, next http.Handler) http.HandlerFunc {
|
||||
func RequireAccount(app *model.AppState, next http.Handler) http.HandlerFunc {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
account, err := controller.GetAccountByRequest(db, r)
|
||||
account, err := controller.GetAccountByRequest(app.DB, r)
|
||||
if err != nil {
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
fmt.Fprintf(os.Stderr, "WARN: Failed to fetch account: %v\n", err)
|
||||
|
|
|
@ -5,19 +5,18 @@ import (
|
|||
"net/http"
|
||||
"strings"
|
||||
|
||||
"arimelody-web/global"
|
||||
"arimelody-web/controller"
|
||||
"arimelody-web/model"
|
||||
)
|
||||
|
||||
func serveRelease() http.Handler {
|
||||
func serveRelease(app *model.AppState) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
slices := strings.Split(r.URL.Path[1:], "/")
|
||||
releaseID := slices[0]
|
||||
|
||||
account := r.Context().Value("account").(*model.Account)
|
||||
|
||||
release, err := controller.GetRelease(global.DB, releaseID, true)
|
||||
release, err := controller.GetRelease(app.DB, releaseID, true)
|
||||
if err != nil {
|
||||
if strings.Contains(err.Error(), "no rows") {
|
||||
http.NotFound(w, r)
|
||||
|
@ -34,10 +33,10 @@ func serveRelease() http.Handler {
|
|||
serveEditCredits(release).ServeHTTP(w, r)
|
||||
return
|
||||
case "addcredit":
|
||||
serveAddCredit(release).ServeHTTP(w, r)
|
||||
serveAddCredit(app, release).ServeHTTP(w, r)
|
||||
return
|
||||
case "newcredit":
|
||||
serveNewCredit().ServeHTTP(w, r)
|
||||
serveNewCredit(app).ServeHTTP(w, r)
|
||||
return
|
||||
case "editlinks":
|
||||
serveEditLinks(release).ServeHTTP(w, r)
|
||||
|
@ -46,10 +45,10 @@ func serveRelease() http.Handler {
|
|||
serveEditTracks(release).ServeHTTP(w, r)
|
||||
return
|
||||
case "addtrack":
|
||||
serveAddTrack(release).ServeHTTP(w, r)
|
||||
serveAddTrack(app, release).ServeHTTP(w, r)
|
||||
return
|
||||
case "newtrack":
|
||||
serveNewTrack().ServeHTTP(w, r)
|
||||
serveNewTrack(app).ServeHTTP(w, r)
|
||||
return
|
||||
}
|
||||
http.NotFound(w, r)
|
||||
|
@ -83,9 +82,9 @@ func serveEditCredits(release *model.Release) http.Handler {
|
|||
})
|
||||
}
|
||||
|
||||
func serveAddCredit(release *model.Release) http.Handler {
|
||||
func serveAddCredit(app *model.AppState, release *model.Release) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
artists, err := controller.GetArtistsNotOnRelease(global.DB, release.ID)
|
||||
artists, err := controller.GetArtistsNotOnRelease(app.DB, release.ID)
|
||||
if err != nil {
|
||||
fmt.Printf("FATAL: Failed to pull artists not on %s: %s\n", release.ID, err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
|
@ -109,10 +108,10 @@ func serveAddCredit(release *model.Release) http.Handler {
|
|||
})
|
||||
}
|
||||
|
||||
func serveNewCredit() http.Handler {
|
||||
func serveNewCredit(app *model.AppState) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
artistID := strings.Split(r.URL.Path, "/")[3]
|
||||
artist, err := controller.GetArtist(global.DB, artistID)
|
||||
artist, err := controller.GetArtist(app.DB, artistID)
|
||||
if err != nil {
|
||||
fmt.Printf("FATAL: Failed to pull artists %s: %s\n", artistID, err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
|
@ -154,9 +153,9 @@ func serveEditTracks(release *model.Release) http.Handler {
|
|||
})
|
||||
}
|
||||
|
||||
func serveAddTrack(release *model.Release) http.Handler {
|
||||
func serveAddTrack(app *model.AppState, release *model.Release) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
tracks, err := controller.GetTracksNotOnRelease(global.DB, release.ID)
|
||||
tracks, err := controller.GetTracksNotOnRelease(app.DB, release.ID)
|
||||
if err != nil {
|
||||
fmt.Printf("FATAL: Failed to pull tracks not on %s: %s\n", release.ID, err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
|
@ -181,10 +180,10 @@ func serveAddTrack(release *model.Release) http.Handler {
|
|||
})
|
||||
}
|
||||
|
||||
func serveNewTrack() http.Handler {
|
||||
func serveNewTrack(app *model.AppState) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
trackID := strings.Split(r.URL.Path, "/")[3]
|
||||
track, err := controller.GetTrack(global.DB, trackID)
|
||||
track, err := controller.GetTrack(app.DB, trackID)
|
||||
if err != nil {
|
||||
fmt.Printf("Error rendering new track component for %s: %s\n", trackID, err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
|
|
|
@ -5,16 +5,15 @@ import (
|
|||
"net/http"
|
||||
"strings"
|
||||
|
||||
"arimelody-web/global"
|
||||
"arimelody-web/model"
|
||||
"arimelody-web/controller"
|
||||
)
|
||||
|
||||
func serveTrack() http.Handler {
|
||||
func serveTrack(app *model.AppState) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
slices := strings.Split(r.URL.Path[1:], "/")
|
||||
id := slices[0]
|
||||
track, err := controller.GetTrack(global.DB, id)
|
||||
track, err := controller.GetTrack(app.DB, id)
|
||||
if err != nil {
|
||||
fmt.Printf("Error rendering admin track page for %s: %s\n", id, err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
|
@ -25,7 +24,7 @@ func serveTrack() http.Handler {
|
|||
return
|
||||
}
|
||||
|
||||
releases, err := controller.GetTrackReleases(global.DB, track.ID, true)
|
||||
releases, err := controller.GetTrackReleases(app.DB, track.ID, true)
|
||||
if err != nil {
|
||||
fmt.Printf("FATAL: Failed to pull releases for %s: %s\n", id, err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue