Move setup -> settings
This commit is contained in:
parent
46bf383ad2
commit
c18537e82f
|
|
@ -52,8 +52,8 @@ func Routes() http.Handler {
|
|||
mux := http.NewServeMux()
|
||||
mux.Handle("GET /static/", ServeStatic())
|
||||
mux.HandleFunc("GET /{$}", HomeGet)
|
||||
mux.HandleFunc("GET /setup", SetupGet)
|
||||
mux.HandleFunc("POST /setup", SetupPost)
|
||||
mux.HandleFunc("GET /settings", SettingsGet)
|
||||
mux.HandleFunc("POST /settings", SettingsPost)
|
||||
mux.HandleFunc("GET /login", LoginGet)
|
||||
mux.HandleFunc("POST /login", LoginPost)
|
||||
mux.HandleFunc("DELETE /login", LoginDelete)
|
||||
|
|
|
|||
|
|
@ -13,9 +13,9 @@ import (
|
|||
"shelves/backend/templates"
|
||||
)
|
||||
|
||||
func setupRenderView(f setupForm, e setupFormErrors) template.HTML {
|
||||
func settingsRenderView(f settingsForm, e settingsFormErrors) template.HTML {
|
||||
body := templates.Form{
|
||||
Action: "/setup",
|
||||
Action: "/settings",
|
||||
Fields: []templates.Field{
|
||||
templates.Field{
|
||||
Label: "Display name",
|
||||
|
|
@ -43,8 +43,8 @@ func setupRenderView(f setupForm, e setupFormErrors) template.HTML {
|
|||
return templates.PageBase{Title: "Set up", Body: body.HTML()}.HTML()
|
||||
}
|
||||
|
||||
func queryGetOwnerSettings(db *sql.DB) (setupForm, error) {
|
||||
form := setupForm{}
|
||||
func queryGetOwnerSettings(db *sql.DB) (settingsForm, error) {
|
||||
form := settingsForm{}
|
||||
err := db.QueryRow(`select display_name from owner_settings`).Scan(&form.displayName)
|
||||
if err == sql.ErrNoRows {
|
||||
err = nil
|
||||
|
|
@ -53,7 +53,7 @@ func queryGetOwnerSettings(db *sql.DB) (setupForm, error) {
|
|||
return form, err
|
||||
}
|
||||
|
||||
func SetupGet(w http.ResponseWriter, r *http.Request) {
|
||||
func SettingsGet(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := httpx.GetCtx(r)
|
||||
if !ctx.Session.IsAdmin && !ctx.NeedsOwnerSetup {
|
||||
httpx.LoginRedirect(w, *r.URL)
|
||||
|
|
@ -66,20 +66,20 @@ func SetupGet(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
|
||||
html(w, setupRenderView(form, setupFormErrors{}))
|
||||
html(w, settingsRenderView(form, settingsFormErrors{}))
|
||||
}
|
||||
|
||||
type setupForm struct {
|
||||
type settingsForm struct {
|
||||
password string
|
||||
displayName string
|
||||
}
|
||||
|
||||
type setupFormErrors struct {
|
||||
type settingsFormErrors struct {
|
||||
password error
|
||||
displayName error
|
||||
}
|
||||
|
||||
func parseForm(f *setupForm, e *setupFormErrors, vs url.Values, v *forms.Validator) {
|
||||
func parseForm(f *settingsForm, e *settingsFormErrors, vs url.Values, v *forms.Validator) {
|
||||
f.password = vs.Get("password")
|
||||
passwordConfirmation := vs.Get("passwordConfirmation")
|
||||
f.displayName = vs.Get("displayName")
|
||||
|
|
@ -100,7 +100,7 @@ func queryUpdateOwnerSettings(db *sql.DB, display_name string, password_salt []b
|
|||
return err
|
||||
}
|
||||
|
||||
func updateOwnerSettings(db *sql.DB, f setupForm) error {
|
||||
func updateOwnerSettings(db *sql.DB, f settingsForm) error {
|
||||
salt := make([]byte, 32)
|
||||
_, err := rand.Read(salt)
|
||||
if err != nil {
|
||||
|
|
@ -112,7 +112,7 @@ func updateOwnerSettings(db *sql.DB, f setupForm) error {
|
|||
return queryUpdateOwnerSettings(db, f.displayName, salt, hash)
|
||||
}
|
||||
|
||||
func SetupPost(w http.ResponseWriter, r *http.Request) {
|
||||
func SettingsPost(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := httpx.GetCtx(r)
|
||||
|
||||
if !ctx.Session.IsAdmin && !ctx.NeedsOwnerSetup {
|
||||
|
|
@ -120,15 +120,15 @@ func SetupPost(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
|
||||
form := setupForm{}
|
||||
errs := setupFormErrors{}
|
||||
form := settingsForm{}
|
||||
errs := settingsFormErrors{}
|
||||
failed := forms.ParseFormData(r, func(vs url.Values, v *forms.Validator) {
|
||||
parseForm(&form, &errs, vs, v)
|
||||
})
|
||||
|
||||
if failed {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
html(w, setupRenderView(form, errs))
|
||||
html(w, settingsRenderView(form, errs))
|
||||
return
|
||||
}
|
||||
|
||||
Loading…
Reference in a new issue