-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.go
201 lines (178 loc) · 5.02 KB
/
main.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
package main
import (
"database/sql"
"errors"
"log"
"net/http"
"os"
"reflect"
"strings"
"time"
"github.com/go-playground/locales/ja_JP"
ut "github.com/go-playground/universal-translator"
"github.com/labstack/echo"
_ "github.com/lib/pq"
"gopkg.in/go-playground/validator.v9"
ja "gopkg.in/go-playground/validator.v9/translations/ja"
"gopkg.in/gorp.v2"
)
var dbDriver = "postgres"
// Validator is implementation of validation of rquest values.
type Validator struct {
trans ut.Translator
validator *validator.Validate
}
// Validate do validation for request value.
func (v *Validator) Validate(i interface{}) error {
err := v.validator.Struct(i)
if err == nil {
return nil
}
errs := err.(validator.ValidationErrors)
msg := ""
for _, v := range errs.Translate(v.trans) {
if msg != "" {
msg += ", "
}
msg += v
}
return errors.New(msg)
}
// Error indicate response erorr
type Error struct {
Error string `json:"error"`
}
// Comment is a struct to hold unit of request and response.
type Comment struct {
Id int64 `json:"id" db:"id,primarykey,autoincrement"`
Name string `json:"name" form:"name" db:"name,notnull,size:200"`
Text string `json:"text" form:"text" validate:"required,max=20" db:"text,notnull,size:399"`
Created time.Time `json:"created" db:"created,notnull"`
Updated time.Time `json:"updated" db:"updated,notnull"`
}
// PreInsert update fields Created and Updated.
func (c *Comment) PreInsert(s gorp.SqlExecutor) error {
if c.Name == "" {
c.Name = "名無し"
}
c.Created = time.Now()
c.Updated = c.Created
return nil
}
// PreUpdate update field Updated.
func (c *Comment) PreUpdate(s gorp.SqlExecutor) error {
c.Updated = time.Now()
return nil
}
func setupDB() (*gorp.DbMap, error) {
db, err := sql.Open(dbDriver, os.Getenv("DSN"))
if err != nil {
return nil, err
}
var diarect gorp.Dialect = gorp.PostgresDialect{}
// for testing
if dbDriver == "sqlite3" {
diarect = gorp.SqliteDialect{}
}
dbmap := &gorp.DbMap{Db: db, Dialect: diarect}
dbmap.AddTableWithName(Comment{}, "comments").SetKeys(true, "id")
err = dbmap.CreateTablesIfNotExists()
if err != nil {
return nil, err
}
return dbmap, nil
}
func setupEcho() *echo.Echo {
e := echo.New()
e.Debug = true
e.Logger.SetOutput(os.Stderr)
// setup japanese translation
japanese := ja_JP.New()
uni := ut.New(japanese, japanese)
trans, _ := uni.GetTranslator("ja")
validate := validator.New()
err := ja.RegisterDefaultTranslations(validate, trans)
if err != nil {
log.Fatal(err)
}
// register japanese translation for input field
validate.RegisterTagNameFunc(func(fld reflect.StructField) string {
name := strings.SplitN(fld.Tag.Get("json"), ",", 2)[0]
switch name {
case "name":
return "お名前"
case "text":
return "コメント"
case "-":
return ""
}
return name
})
e.Validator = &Validator{validator: validate, trans: trans}
return e
}
// Controller is a controller for this application.
type Controller struct {
dbmap *gorp.DbMap
}
// GetComment is GET handler to return record.
func (controller *Controller) GetComment(c echo.Context) error {
var comment Comment
// fetch record specified by parameter id
err := controller.dbmap.SelectOne(&comment,
"SELECT * FROM comments WHERE id = $1", c.Param("id"))
if err != nil {
if err != sql.ErrNoRows {
c.Logger().Error("SelectOne: ", err)
return c.String(http.StatusBadRequest, "SelectOne: "+err.Error())
}
return c.String(http.StatusNotFound, "Not Found")
}
return c.JSON(http.StatusOK, comment)
}
// ListComments is GET handler to return records.
func (controller *Controller) ListComments(c echo.Context) error {
var comments []Comment
// fetch last 10 records
_, err := controller.dbmap.Select(&comments,
"SELECT * FROM comments ORDER BY created desc LIMIT 10")
if err != nil {
c.Logger().Error("Select: ", err)
return c.String(http.StatusBadRequest, "Select: "+err.Error())
}
return c.JSON(http.StatusOK, comments)
}
// InsertComment is POST handler to insert record.
func (controller *Controller) InsertComment(c echo.Context) error {
var comment Comment
// bind request to comment struct
if err := c.Bind(&comment); err != nil {
c.Logger().Error("Bind: ", err)
return c.String(http.StatusBadRequest, "Bind: "+err.Error())
}
// validate request
if err := c.Validate(&comment); err != nil {
c.Logger().Error("Validate: ", err)
return c.JSON(http.StatusBadRequest, &Error{Error: err.Error()})
}
// insert record
if err := controller.dbmap.Insert(&comment); err != nil {
c.Logger().Error("Insert: ", err)
return c.String(http.StatusBadRequest, "Insert: "+err.Error())
}
c.Logger().Infof("inserted comment: %v", comment.Id)
return c.NoContent(http.StatusCreated)
}
func main() {
dbmap, err := setupDB()
if err != nil {
log.Fatal(err)
}
controller := &Controller{dbmap: dbmap}
e := setupEcho()
e.GET("/api/comments/:id", controller.GetComment)
e.GET("/api/comments", controller.ListComments)
e.POST("/api/comments", controller.InsertComment)
e.Static("/", "static/")
e.Logger.Fatal(e.Start(":8989"))
}