/
user_controller.go
111 lines (98 loc) · 2.76 KB
/
user_controller.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package main
import (
"github.com/zenazn/goji/web"
"net/http"
"encoding/base64"
"strings"
"html/template"
"github.com/syo-sa1982/crudstudy/models"
"github.com/wcl48/valval"
"fmt"
"strconv"
"log"
)
var tpl *template.Template
const Password = "user:user"
type FormData struct{
User models.User
Mess string
}
func UserIndex(c web.C, w http.ResponseWriter, r *http.Request){
Users := [] models.User{}
db.Find(&Users)
log.Print("index")
tpl = template.Must(template.ParseFiles("view/user/index.html"))
tpl.Execute(w, Users)
}
func UserNew(c web.C, w http.ResponseWriter, r *http.Request){
tpl = template.Must(template.ParseFiles("view/user/new.html"))
tpl.Execute(w, FormData{models.User{}, ""})
}
func UserCreate(c web.C, w http.ResponseWriter, r *http.Request){
User := models.User{Name: r.FormValue("Name")}
if err := models.UserValidate(User); err != nil {
var Mess string
errs := valval.Errors(err)
for _, errInfo := range errs {
Mess += fmt.Sprint(errInfo.Error)
}
tpl = template.Must(template.ParseFiles("view/user/new.html"))
tpl.Execute(w, FormData{User, Mess})
} else {
db.Create(&User)
http.Redirect(w, r, "/user/index", 301)
}
}
func UserEdit(c web.C, w http.ResponseWriter, r *http.Request){
log.Print("UserEdit")
User := models.User{}
User.Id, _ = strconv.ParseInt(c.URLParams["id"], 10, 64)
db.Find(&User)
tpl = template.Must(template.ParseFiles("view/user/edit.html"))
tpl.Execute(w,FormData{User, ""})
}
func UserUpdate(c web.C, w http.ResponseWriter, r *http.Request){
User := models.User{}
User.Id, _ = strconv.ParseInt(c.URLParams["id"], 10, 64)
db.Find(&User)
User.Name = r.FormValue("Name")
if err := models.UserValidate(User); err != nil {
var Mess string
errs := valval.Errors(err)
for _, errInfo := range errs {
Mess += fmt.Sprint(errInfo.Error)
}
tpl = template.Must(template.ParseFiles("view/user/edit.html"))
tpl.Execute(w,FormData{User, Mess})
} else {
db.Save(&User)
http.Redirect(w, r, "/user/index", 301)
}
}
func UserDelete(c web.C, w http.ResponseWriter, r *http.Request){
User := models.User{}
User.Id, _ = strconv.ParseInt(c.URLParams["id"], 10, 64)
db.Delete(&User)
http.Redirect(w, r, "/user/index", 301)
}
func SuperSecure(c *web.C, h http.Handler) http.Handler {
fn := func (w http.ResponseWriter, r *http.Request) {
auth := r.Header.Get("Authorization")
if !strings.HasPrefix(auth, "Basic ") {
pleaseAuth(w)
return
}
password, err := base64.StdEncoding.DecodeString(auth[6:])
if err != nil || string(password) != Password {
pleaseAuth(w)
return
}
h.ServeHTTP(w, r)
}
return http.HandlerFunc(fn)
}
func pleaseAuth(w http.ResponseWriter) {
w.Header().Set("WWW-Authenticate", `Basic realm="Gritter"`)
w.WriteHeader(http.StatusUnauthorized)
w.Write([]byte("Go away!\n"))
}