-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
195 lines (169 loc) · 3.67 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
package main
import (
"encoding/json"
"errors"
"flag"
"fmt"
"log"
"net/http"
"os"
"sync"
"time"
"github.com/gorilla/mux"
)
var (
name string
version string
gitSHA string
)
const usage = `version: %s - git: %s
Usage: %s [-h] [-v]
Options:
-h this help
-v show version and exit
Examples:
%[3]s -t localhost:8888 run the server
`
func main() {
var vers bool
flag.Usage = func() {
w := os.Stderr
for _, arg := range os.Args {
if arg == "-h" {
w = os.Stdout
break
}
}
fmt.Fprintf(w, usage, version, gitSHA, name)
}
flag.BoolVar(&vers, "v", false, "")
flag.Parse()
if vers {
fmt.Fprintf(os.Stdout, "version: %s\n", version)
return
}
router := mux.NewRouter()
data := newStore()
data.addTodo(todo{ID: uint32(1), Tasks: []task{task{Name: "task 1"}}})
c := newController(data)
ba := &BasicAuth{
Username: "go",
Password: "go",
}
router.HandleFunc(todoURL, ba.Authenticate(c.retrieveTodosHandler())).Methods(http.MethodGet)
router.HandleFunc(todoURL, ba.Authenticate(c.addTodoHandler())).Methods(http.MethodPost)
if err := http.ListenAndServe(":8888", router); err != nil {
log.Println(err)
os.Exit(1)
}
}
type controller struct {
store *store
}
func newController(s *store) *controller {
return &controller{
store: s,
}
}
const (
baseAPI = "/api/v1"
todoURL = baseAPI + "/todo"
)
type todosRequest struct {
Data struct {
Todos []todo `json:"todos"`
} `json:"data"`
}
type request struct{}
type task struct {
Name string `json:"name"`
CreatedAt time.Time `json:"created_at"`
CompletedAt time.Time `json:"completed_at"`
Completed bool `json:"completed"`
}
type todo struct {
ID uint32 `json:"id"`
Tasks []task `json:"tasks"`
Category string `json:"category"`
}
func (c *controller) retrieveTodosHandler() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
todos := c.store.todos()
b, err := json.Marshal(todos)
if err != nil {
log.Println(err)
http.Error(w, "please try again later", http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
w.Write(b)
}
}
func (c *controller) addTodoHandler() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var t todo
if err := json.NewDecoder(r.Body).Decode(&t); err != nil {
log.Println(err)
http.Error(w, "please try again later", http.StatusInternalServerError)
return
}
c.store.addTodo(t)
w.WriteHeader(http.StatusOK)
}
}
type store struct {
mu *sync.Mutex
data map[uint32]todo
}
func newStore() *store {
return &store{
mu: &sync.Mutex{},
data: make(map[uint32]todo),
}
}
func (s *store) todos() []todo {
s.mu.Lock()
defer s.mu.Unlock()
todos := make([]todo, len(s.data))
for _, v := range s.data {
todos = append(todos, v)
}
return todos
}
func (s *store) addTodo(t todo) {
s.mu.Lock()
defer s.mu.Unlock()
if _, ok := s.data[t.ID]; !ok {
s.data[t.ID] = t
return
}
s.data[t.ID] = t
return
}
type Authenticator interface {
Authenticate(handler http.HandlerFunc) http.HandlerFunc
}
// BasicAuth contains a username and password combination
type BasicAuth struct {
Username string
Password string
}
func (b *BasicAuth) Authenticate(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if err := b.authenticate(r); err != nil {
w.WriteHeader(http.StatusUnauthorized)
return
}
next.ServeHTTP(w, r)
}
}
func (b *BasicAuth) authenticate(r *http.Request) error {
user, pass, ok := r.BasicAuth()
if !ok {
return errors.New("invalid authorization header")
}
if user != b.Username || pass != b.Password {
return errors.New("invalid credentials")
}
return nil
}