-
Notifications
You must be signed in to change notification settings - Fork 0
/
user_handler.go
49 lines (40 loc) · 1.29 KB
/
user_handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// Copyright 2024 Bill Nixon. All rights reserved.
// Use of this source code is governed by the license found in the LICENSE file.
package webauth
import (
"net/http"
"github.com/bnixon67/webapp/webhandler"
"github.com/bnixon67/webapp/webutil"
)
// UserPageData contains data passed to the HTML template.
type UserPageData struct {
Title string
Message string
User User
}
// UserGetHandler shows user information.
func (app *AuthApp) UserGetHandler(w http.ResponseWriter, r *http.Request) {
// Get logger with request info and function name.
logger := webhandler.NewRequestLoggerWithFuncName(r)
// Check if the HTTP method is valid.
if r.Method != http.MethodGet {
webutil.RespondWithError(w, http.StatusMethodNotAllowed)
logger.Error("invalid method")
return
}
// Attempt to get the user from the request.
user, err := app.DB.UserFromRequest(w, r)
if err != nil {
webutil.RespondWithError(w, http.StatusInternalServerError)
logger.Error("failed to get user from request", "err", err)
return
}
// Render the template with the data.
err = webutil.RenderTemplateOrError(app.Tmpl, w, "user.html",
UserPageData{Message: "", User: user, Title: app.Cfg.App.Name})
if err != nil {
logger.Error("failed to render template", "err", err)
return
}
logger.Info("done", "user", user)
}