Add Go rewrite with Postgres 17 and Dokploy Docker Compose
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"amnezia-share/internal/web"
|
||||
)
|
||||
|
||||
func registerInstall(a *web.App, mux *http.ServeMux) {
|
||||
mux.HandleFunc("GET /install", InstallForm(a))
|
||||
mux.HandleFunc("POST /install", a.CSRFProtect(InstallSubmit(a)))
|
||||
}
|
||||
|
||||
type installView struct {
|
||||
Error string
|
||||
Installed bool
|
||||
DBError string
|
||||
}
|
||||
|
||||
// InstallForm renders the single-admin registration form (install.php).
|
||||
func InstallForm(a *web.App) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
if a.CurrentAdmin(r) != nil {
|
||||
redirectTo(w, r, a, "admin")
|
||||
return
|
||||
}
|
||||
count, err := a.Auth.AdminCount(r.Context())
|
||||
view := installView{}
|
||||
if err != nil {
|
||||
view.DBError = "Нет связи с базой данных. Проверьте DATABASE_URL и миграции."
|
||||
} else if count > 0 {
|
||||
view.Installed = true
|
||||
}
|
||||
a.Render(w, r, "page_install", view)
|
||||
}
|
||||
}
|
||||
|
||||
// InstallSubmit creates the sole administrator account (install.php POST).
|
||||
func InstallSubmit(a *web.App) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
count, err := a.Auth.AdminCount(ctx)
|
||||
if err != nil {
|
||||
a.Render(w, r, "page_install", installView{DBError: "Нет связи с базой данных."})
|
||||
return
|
||||
}
|
||||
if count > 0 {
|
||||
a.Render(w, r, "page_install", installView{Installed: true})
|
||||
return
|
||||
}
|
||||
|
||||
username := strings.TrimSpace(r.FormValue("username"))
|
||||
p1 := r.FormValue("password")
|
||||
p2 := r.FormValue("password2")
|
||||
if p1 != p2 {
|
||||
a.Render(w, r, "page_install", installView{Error: "Пароли не совпадают."})
|
||||
return
|
||||
}
|
||||
|
||||
admin, err := a.Auth.RegisterFirstAdmin(ctx, username, p1)
|
||||
if err != nil {
|
||||
a.Render(w, r, "page_install", installView{Error: err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
_ = a.Sessions.RenewToken(ctx)
|
||||
a.Sessions.Put(ctx, web.SessionAdminID, admin.ID)
|
||||
redirectTo(w, r, a, "admin")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user