commit 1a523ae54c644999278773e5b542b02d2077f9fd
parent fa77144d20b25c259ef9ffed10f206998f72709f
Author: Peter Kosyh <p.kosyh@gmail.com>
Date: Thu, 3 Sep 2020 23:24:46 +0300
error template
Diffstat:
3 files changed, 12 insertions(+), 4 deletions(-)
diff --git a/ii-node/main.go b/ii-node/main.go
@@ -57,6 +57,11 @@ type WWW struct {
db *ii.DB
}
+func handleErr(www WWW, w http.ResponseWriter, err error) {
+ ctx := WebContext{ Error: err.Error() }
+ www.tpl.ExecuteTemplate(w, "error.tpl", ctx)
+}
+
func main() {
var www WWW
ii.OpenLog(ioutil.Discard, os.Stdout, os.Stderr)
@@ -75,7 +80,7 @@ func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
err := handleWWW(www, w, r)
if err != nil {
- fmt.Fprintf(w, "%s\n", err)
+ handleErr(www, w, err)
}
})
http.HandleFunc("/list.txt", func(w http.ResponseWriter, r *http.Request) {
diff --git a/ii-node/tpl/error.tpl b/ii-node/tpl/error.tpl
@@ -0,0 +1,3 @@
+{{template "header.tpl" $}}
+{{.Error}}
+{{template "footer.tpl"}}
diff --git a/ii-node/web.go b/ii-node/web.go
@@ -15,6 +15,7 @@ type WebContext struct {
Echoes []*ii.Echo
Topics []*Topic
Msg []*ii.Msg
+ Error string
Echo string
Page int
Pages int
@@ -70,17 +71,16 @@ func www_login(user *ii.User, www WWW, w http.ResponseWriter, r *http.Request) e
udb := ii.LoadUsers(*users_opt)
if udb == nil || !udb.Auth(user, password) {
ii.Info.Printf("Access denied for user: %s", user)
- return nil
+ return errors.New("Access denied")
}
exp := time.Now().Add(10 * 365 * 24 * time.Hour)
cookie := http.Cookie{Name: "pauth", Value: udb.Secret(user), Expires: exp}
http.SetCookie(w, &cookie)
ii.Info.Printf("User logged in: %s\n", user)
http.Redirect(w, r, "/", http.StatusSeeOther)
- default:
return nil
}
- return nil
+ return errors.New("Wrong method")
}
func www_index(user *ii.User, www WWW, w http.ResponseWriter, r *http.Request) error {