/
server.go
231 lines (190 loc) · 5.13 KB
/
server.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package main
import (
"crypto/md5"
"database/sql"
"encoding/hex"
"errors"
"io/ioutil"
"log"
"net/http"
"regexp"
"strings"
"time"
"github.com/coopernurse/gorp"
"github.com/go-martini/martini"
_ "github.com/lib/pq"
"github.com/martini-contrib/render"
"github.com/martini-contrib/sessions"
)
const (
CookieSecret = "secretedesse"
DBName = "bunkai"
BowerComponentPath = "./client/src/components/"
)
func SetupDB() *gorp.DbMap {
db, err := sql.Open("postgres", "dbname="+DBName+" sslmode=disable")
PanicIf(err)
dbmap := &gorp.DbMap{Db: db, Dialect: gorp.PostgresDialect{}}
dbmap.AddTableWithName(Sentence{}, "sentences").SetKeys(true, "Id")
dbmap.AddTableWithName(User{}, "users").SetKeys(true, "Id")
err = dbmap.CreateTablesIfNotExists()
PanicIf(err)
return dbmap
}
func PanicIf(err error) {
if err != nil {
panic(err)
}
}
func Md5(org string) string {
hasher := md5.New()
hasher.Write([]byte(org))
return hex.EncodeToString(hasher.Sum(nil))
}
type Sentence struct {
Id int64
UserId int64
Text string
Url string
CreatedAt int64
}
func newSentence(usr User, text, url string) Sentence {
return Sentence{
UserId: usr.Id,
Text: text,
Url: url,
CreatedAt: time.Now().UnixNano(),
}
}
type User struct {
Id int64
Email string
Password string
CreatedAt int64
UpdatedAt int64
}
func newUser(email, password string) User {
return User{
Email: email,
Password: Md5(password),
CreatedAt: time.Now().UnixNano(),
}
}
func (sen *Sentence) Validate() (bool, error) {
matchText, _ := regexp.MatchString(`.+`, sen.Text)
if !matchText {
err := errors.New("Text is invalid")
return false, err
}
matchUrl, _ := regexp.MatchString(`http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?`, sen.Url)
if !matchUrl {
err := errors.New("Url is invalid")
return false, err
}
return true, nil
}
func main() {
m := martini.Classic()
m.Map(SetupDB())
m.Use(martini.Logger())
m.Use(render.Renderer(render.Options{
Layout: "layout",
}))
store := sessions.NewCookieStore([]byte(CookieSecret))
m.Use(sessions.Sessions("bunkaisession", store))
log.Println("env is", martini.Env)
m.Map(martini.Env)
m.Group("/", func(m martini.Router) {
m.Get("", Home)
}, AssetMap)
m.Group("/api", func(m martini.Router) {
m.Post("/login", PostLogin)
m.Post("/users", UserCreate)
m.Group("/sentences", func(m martini.Router) {
m.Post("", SentenceCreate)
m.Get("", SentenceList)
m.Delete("/:id", SentenceDelete)
}, RequireLogin)
m.Group("/users", func(m martini.Router) {
m.Post("/logout", Logout)
m.Get("/me", UserGet)
}, RequireLogin)
})
m.Run()
}
func AssetMap(c martini.Context) {
c.Map(JsComponent())
}
func JsComponent() []string {
jsdirs, err := ioutil.ReadDir(BowerComponentPath)
PanicIf(err)
var jslibNamePath []string
for _, value := range jsdirs {
s := []string{BowerComponentPath, value.Name(), "/", value.Name(), ".js"}
jslibNamePath = append(jslibNamePath, strings.Join(s, ""))
}
return jslibNamePath
}
func RequireLogin(ren render.Render, req *http.Request, s sessions.Session, dbmap *gorp.DbMap, c martini.Context) {
var usr User
err := dbmap.SelectOne(&usr, "SELECT * from users WHERE id = $1", s.Get("userId"))
if err != nil {
ren.JSON(http.StatusForbidden, nil)
return
}
c.Map(usr)
}
func Logout(ren render.Render, req *http.Request, s sessions.Session) {
s.Delete("userId")
ren.JSON(http.StatusAccepted, nil)
}
func PostLogin(req *http.Request, dbmap *gorp.DbMap, s sessions.Session) (int, string) {
var userId string
email, password := req.FormValue("email"), req.FormValue("password")
err := dbmap.SelectOne(&userId, "select id from users where email=$1 and password=$2", email, Md5(password))
if err != nil {
return 401, "Unauthorized"
}
s.Set("userId", userId)
return 200, "User id is " + userId
}
func Home(ren render.Render) {
ren.HTML(200, "home", nil)
}
func SentenceCreate(ren render.Render, req *http.Request, dbmap *gorp.DbMap, usr User) {
sentence := newSentence(usr, req.FormValue("text"), req.FormValue("url"))
_, err := sentence.Validate()
if err != nil {
msg := make(map[string]string)
msg["error"] = err.Error()
ren.JSON(400, msg)
} else {
err = dbmap.Insert(&sentence)
PanicIf(err)
ren.JSON(200, sentence)
}
}
func SentenceList(ren render.Render, req *http.Request, dbmap *gorp.DbMap, usr User) {
var sens []Sentence
_, err := dbmap.Select(&sens, "select * from sentences WHERE userid = $1 order by id", usr.Id)
PanicIf(err)
ren.JSON(200, sens)
}
func SentenceDelete(ren render.Render, params martini.Params, dbmap *gorp.DbMap) {
_, err := dbmap.Exec("DELETE FROM sentences WHERE id= $1", params["id"])
PanicIf(err)
ren.JSON(200, nil)
}
func UserGet(ren render.Render, params martini.Params, dbmap *gorp.DbMap, s sessions.Session) {
var usr User
log.Println(params)
err := dbmap.SelectOne(&usr, "SELECT * from users WHERE id = $1", s.Get("userId"))
PanicIf(err)
ren.JSON(200, usr)
}
func UserCreate(ren render.Render, req *http.Request, dbmap *gorp.DbMap) {
usr := newUser(req.FormValue("email"), req.FormValue("password"))
err := dbmap.Insert(&usr)
PanicIf(err)
ren.JSON(200, usr)
}