knowyt/server/src/application/setupApp.go

38 lines
864 B
Go
Raw Normal View History

package application
import (
"fmt"
"net/http"
"regexp"
"sirlab.de/go/knowyt/log"
"sirlab.de/go/knowyt/user"
)
func (app *Application) SetupApp(w http.ResponseWriter, r *http.Request) {
authcode := r.URL.Query().Get("authcode")
if match, err := regexp.MatchString("^\\d{6}$", authcode); err != nil || !match {
w.WriteHeader(http.StatusInternalServerError)
log.Error("setupApp failed: invalid authcode does not consist of exactly 6 digits")
fmt.Fprintf(w, "server error")
return
}
_, err := app.createUser(nil, "Admin", user.ROLE_ADMIN, authcode)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
log.ErrorLog(err)
fmt.Fprintf(w, "server error")
return
}
// if err != nil {
// w.WriteHeader(http.StatusNotFound)
// fmt.Fprintf(w, "...")
// return
// }
log.Debug("setup app successful\n")
fmt.Fprintf(w, "ok")
}