-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
218 lines (173 loc) · 4.68 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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package main
import (
"encoding/json"
"errors"
"fmt"
"github.com/ToQoz/dou"
_ "github.com/ToQoz/dou/jsonapi"
"github.com/ToQoz/rome"
"github.com/lestrrat/go-apache-logformat"
"log"
"net"
"net/http"
"os"
"os/signal"
"time"
)
var (
// APIStatusOk define ok status code for X-API-Status
APIStatusOk = 1
// APIStatusValidationError define validation error status code for X-API-Status
APIStatusValidationError = 100
// APIStatusUnexpectedError define unexpected error status code for X-API-Status
APIStatusUnexpectedError = 900
logger = apachelog.CombinedLog
)
// --- API Error type ---
type apiError struct {
Message string `json:"message"`
}
func newAPIError(err error) *apiError {
return &apiError{Message: err.Error()}
}
type apiErrors struct {
Errors []*apiError `json:"errors"`
}
func newAPIErrors(errs []error) *apiErrors {
aErrs := &apiErrors{}
for _, err := range errs {
aErrs.Errors = append(aErrs.Errors, newAPIError(err))
}
return aErrs
}
// --- Example struct ---
var users = []*User{}
// User represents a user account.
type User struct {
Name string `json:"name"`
Email string `json:"email"`
}
// Validate validate fields.
func (u *User) Validate() []error {
var errs []error
if u.Name == "" {
errs = append(errs, errors.New("user: name is required"))
}
if u.Email == "" {
errs = append(errs, errors.New("user: email is required"))
}
return errs
}
// Save save to memory
func (u *User) Save() error {
users = append(users, u)
return nil
}
func main() {
defer teardown()
// --- Setup Router ---
// ! You can use router keeping interface `api.Router` instead of github.com/ToQoz/rome
router := rome.NewRouter()
router.NotFoundFunc(func(w http.ResponseWriter, r *http.Request) {
j, err := json.Marshal(map[string]string{
"message": http.StatusText(http.StatusNotFound),
"documentation_url": "http://toqoz.net",
})
if err != nil {
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
}
w.WriteHeader(http.StatusNotFound)
// skip wrote bytesyze
_, err = fmt.Fprintln(w, string(j))
if err != nil {
log.Printf("dou: fail to fmt.Fpintln(http.ResponseWriter, string)\n%v", err)
}
})
// --- Setup API ---
api, err := dou.NewAPI("jsonapi")
api.Handler = router
//api, err := dou.NewAPI("jsonapi")
if err != nil {
log.Fatal(err)
}
api.BeforeDispatch = func(w http.ResponseWriter, r *http.Request) (http.ResponseWriter, *http.Request) {
// Call default
w, r = api.Plugin.BeforeDispatch(w, r)
lw := apachelog.NewLoggingWriter(w, r, logger)
return lw, r
}
api.AfterDispatch = func(w http.ResponseWriter, r *http.Request) (http.ResponseWriter, *http.Request) {
// Call default
w, r = api.Plugin.AfterDispatch(w, r)
if lw, ok := w.(*apachelog.LoggingWriter); ok {
lw.EmitLog()
}
return w, r
}
api.ReadTimeout = 10 * time.Second
api.WriteTimeout = 10 * time.Second
api.MaxHeaderBytes = 1 << 20
// --- Map routes ---
router.GetFunc("/users", func(w http.ResponseWriter, r *http.Request) {
api.APIStatus(w, APIStatusOk)
api.Ok(w, users, http.StatusOK)
})
router.GetFunc("/error", func(w http.ResponseWriter, r *http.Request) {
api.APIStatus(w, APIStatusUnexpectedError)
api.Error(w, map[string]string{"message": "Internal server error"}, http.StatusInternalServerError)
})
// Try Ok $ curl -X POST -d 'name=ToQoz&email=toqoz403@gmail.com' -D - :8099/users
// Try Error $ curl -X POST -D - :8099/users
router.PostFunc("/users", func(w http.ResponseWriter, r *http.Request) {
u := &User{
Name: r.FormValue("name"),
Email: r.FormValue("email"),
}
errs := u.Validate()
if len(errs) > 0 {
api.APIStatus(w, APIStatusValidationError)
api.Error(w, newAPIErrors(errs), 422)
return
}
err := u.Save()
if err != nil {
api.APIStatus(w, APIStatusUnexpectedError)
api.Error(w, newAPIErrors(errs), http.StatusInternalServerError)
return
}
api.APIStatus(w, APIStatusOk)
api.Ok(w, u, http.StatusCreated)
})
// --- Create listener ---
// You can use utility, for example github.com/lestrrat/go-server-starter-listener etc.
l, err := net.Listen("tcp", ":8099")
if err != nil {
log.Printf("Could not listen: %s", ":8099")
teardown()
os.Exit(1)
}
log.Printf("Listen: %s", ":8099")
// --- Handle C-c ---
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
for sig := range c {
log.Print("Stopping the server...")
switch sig {
case os.Interrupt:
// --- Stop Server ---
api.Stop()
return
default:
log.Print("Receive unknown signal...")
}
}
}()
// --- Run Server ---
api.Run(l)
}
func teardown() {
log.Print("Tearing down...")
log.Print("Finished - bye bye. ;-)")
}