diff --git a/cmd/numerus/main.go b/cmd/numerus/main.go index c9cc57f..de988db 100644 --- a/cmd/numerus/main.go +++ b/cmd/numerus/main.go @@ -20,9 +20,12 @@ func main() { } defer db.Close() + var demo bool + _ = db.QueryRow(context.Background(), "select database_is_numerus_demo()").Scan(&demo) + srv := http.Server{ Addr: ":8080", - Handler: numerus.NewRouter(db), + Handler: numerus.NewRouter(db, demo), ReadTimeout: 5 * time.Second, WriteTimeout: 10 * time.Second, IdleTimeout: 2 * time.Minute, diff --git a/demo/demo.sql b/demo/demo.sql index a5a2ea8..17ed672 100644 --- a/demo/demo.sql +++ b/demo/demo.sql @@ -2,6 +2,8 @@ begin; set search_path to auth, numerus, public; +create or replace function public.database_is_numerus_demo() returns bool as $$ select true $$ language sql; + alter sequence user_user_id_seq restart with 123; insert into auth."user" (email, name, password, role) values ('demo@numerus', 'Demo User', 'demo', 'invoicer') diff --git a/pkg/login.go b/pkg/login.go index 658d85a..95ed5dc 100644 --- a/pkg/login.go +++ b/pkg/login.go @@ -29,8 +29,8 @@ type loginForm struct { Password *InputField } -func newLoginForm(locale *Locale) *loginForm { - return &loginForm{ +func newLoginForm(demo bool, locale *Locale) *loginForm { + form := &loginForm{ locale: locale, Email: &InputField{ Name: "email", @@ -53,6 +53,11 @@ func newLoginForm(locale *Locale) *loginForm { }, }, } + if demo { + form.Email.Val = "admin@numerus" + form.Password.Val = "admin" + } + return form } func (form *loginForm) Parse(r *http.Request) error { @@ -74,26 +79,26 @@ func (form *loginForm) Validate() bool { return validator.AllOK() } -func GetLoginForm(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { +func GetLoginForm(w http.ResponseWriter, r *http.Request, demo bool) { user := getUser(r) if user.LoggedIn { http.Redirect(w, r, "/", http.StatusSeeOther) return } locale := getLocale(r) - form := newLoginForm(locale) + form := newLoginForm(demo, locale) w.WriteHeader(http.StatusOK) mustRenderLoginForm(w, r, form) } -func HandleLoginForm(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { +func HandleLoginForm(w http.ResponseWriter, r *http.Request, demo bool) { user := getUser(r) if user.LoggedIn { http.Redirect(w, r, "/", http.StatusSeeOther) return } locale := getLocale(r) - form := newLoginForm(locale) + form := newLoginForm(demo, locale) if err := form.Parse(r); err != nil { http.Error(w, err.Error(), http.StatusBadRequest) return diff --git a/pkg/router.go b/pkg/router.go index e5c698d..191b6a6 100644 --- a/pkg/router.go +++ b/pkg/router.go @@ -6,7 +6,7 @@ import ( "net/http" ) -func NewRouter(db *Db) http.Handler { +func NewRouter(db *Db, demo bool) http.Handler { companyRouter := httprouter.New() companyRouter.GET("/profile", GetProfileForm) companyRouter.POST("/profile", HandleProfileForm) @@ -62,8 +62,12 @@ func NewRouter(db *Db) http.Handler { router := httprouter.New() router.ServeFiles("/static/*filepath", http.Dir("web/static")) - router.GET("/login", GetLoginForm) - router.POST("/login", HandleLoginForm) + router.GET("/login", func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { + GetLoginForm(w, r, demo) + }) + router.POST("/login", func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { + HandleLoginForm(w, r, demo) + }) router.POST("/logout", Authenticated(HandleLogout)) companyHandler := Authenticated(CompanyHandler(companyRouter))