-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
239 lines (182 loc) · 5.48 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package main
import (
"context"
"database/sql"
"fmt"
"log"
"os"
"github.com/ServiceWeaver/weaver"
"github.com/gofiber/fiber/v2"
_ "github.com/mattn/go-sqlite3"
)
func init() {
log.SetFlags(log.Ldate | log.Ltime | log.Lmsgprefix)
log.SetPrefix(fmt.Sprintf("[%d] [weaver] ", os.Getpid()))
}
func main() {
if err := weaver.Run(context.Background(), serve); err != nil {
log.Fatal(err)
}
}
func serve(ctx context.Context, app *App) error {
server := fiber.New(fiber.Config{
DisableStartupMessage: true,
})
server.Get("/health", func(c *fiber.Ctx) error {
return c.SendString("OK")
})
server.Get("/users", func(c *fiber.Ctx) error {
users, err := app.userRepository.Get().FindAll(c.Context())
if err != nil {
return c.Status(fiber.StatusInternalServerError).SendString(err.Error())
}
return c.JSON(users)
})
server.Get("/users/:id", func(c *fiber.Ctx) error {
id, err := c.ParamsInt("id")
if err != nil {
return c.Status(fiber.StatusBadRequest).SendString(err.Error())
}
user, err := app.userRepository.Get().FindById(c.Context(), uint64(id))
if err != nil {
return c.Status(fiber.StatusInternalServerError).SendString(err.Error())
}
return c.JSON(user)
})
server.Post("/users", func(c *fiber.Ctx) error {
var body struct {
Name string `json:"name"`
Email string `json:"email"`
}
if err := c.BodyParser(&body); err != nil {
return c.Status(fiber.StatusBadRequest).SendString(err.Error())
}
user, err := app.userRepository.Get().Create(c.Context(), User{
Name: body.Name,
Email: body.Email,
})
if err != nil {
return c.Status(fiber.StatusInternalServerError).SendString(err.Error())
}
return c.JSON(user)
})
server.Put("/users/:id", func(c *fiber.Ctx) error {
id, err := c.ParamsInt("id")
if err != nil {
return c.Status(fiber.StatusBadRequest).SendString(err.Error())
}
var body struct {
Name string `json:"name"`
Email string `json:"email"`
}
if err := c.BodyParser(&body); err != nil {
return c.Status(fiber.StatusBadRequest).SendString(err.Error())
}
user, err := app.userRepository.Get().Update(c.Context(), User{
Id: uint64(id),
Name: body.Name,
Email: body.Email,
})
if err != nil {
return c.Status(fiber.StatusInternalServerError).SendString(err.Error())
}
return c.JSON(user)
})
server.Delete("/users/:id", func(c *fiber.Ctx) error {
id, err := c.ParamsInt("id")
if err != nil {
return c.Status(fiber.StatusBadRequest).SendString(err.Error())
}
if err := app.userRepository.Get().Delete(c.Context(), uint64(id)); err != nil {
return c.Status(fiber.StatusInternalServerError).SendString(err.Error())
}
return c.SendStatus(fiber.StatusNoContent)
})
log.Printf("Server is listening on: http://%s\n", app.listener.Addr())
log.Printf("Check health status at: http://%s/health\n", app.listener.Addr())
return server.Listener(app.listener)
}
type App struct {
weaver.Implements[weaver.Main]
listener weaver.Listener
userRepository weaver.Ref[UserRepository]
}
type User struct {
weaver.AutoMarshal
Id uint64 `json:"id"`
Name string `json:"name"`
Email string `json:"email"`
CreatedAt string `json:"createdAt"`
UpdatedAt string `json:"updatedAt"`
}
type UserRepository interface {
FindAll(context.Context) ([]User, error)
FindById(context.Context, uint64) (User, error)
Create(context.Context, User) (User, error)
Update(context.Context, User) (User, error)
Delete(context.Context, uint64) error
}
type userRepositoryImpl struct {
weaver.Implements[UserRepository]
db *sql.DB
}
func (r *userRepositoryImpl) Init(context.Context) error {
db, err := sql.Open("sqlite3", "./dev.db?_journal_mode=WAL&mode=rwc&cache=shared")
if err != nil {
return err
}
r.db = db
return nil
}
func (r *userRepositoryImpl) FindAll(ctx context.Context) ([]User, error) {
log.Println("UserRepository.FindAll()")
rows, err := r.db.QueryContext(ctx, "SELECT * FROM users")
if err != nil {
return nil, err
}
defer rows.Close()
var users []User
for rows.Next() {
var user User
if err := rows.Scan(&user.Id, &user.Name, &user.Email, &user.CreatedAt, &user.UpdatedAt); err != nil {
return nil, err
}
users = append(users, user)
}
return users, nil
}
func (r *userRepositoryImpl) FindById(ctx context.Context, id uint64) (User, error) {
log.Printf("UserRepository.FindById(%d)\n", id)
var user User
if err := r.db.QueryRowContext(ctx, "SELECT * FROM users WHERE id = ?", id).Scan(&user.Id, &user.Name, &user.Email, &user.CreatedAt, &user.UpdatedAt); err != nil {
return User{}, err
}
return user, nil
}
func (r *userRepositoryImpl) Create(ctx context.Context, user User) (User, error) {
result, err := r.db.ExecContext(ctx, "INSERT INTO users(name, email) VALUES(?, ?)", user.Name, user.Email)
if err != nil {
return User{}, err
}
id, err := result.LastInsertId()
if err != nil {
return User{}, err
}
return r.FindById(ctx, uint64(id))
}
func (r *userRepositoryImpl) Update(ctx context.Context, user User) (User, error) {
log.Println("UserRepository.Update()")
_, err := r.db.ExecContext(ctx, "UPDATE users SET name = ?, email = ?, updated_at = CURRENT_TIMESTAMP WHERE id = ?", user.Name, user.Email, user.Id)
if err != nil {
return User{}, err
}
return r.FindById(ctx, user.Id)
}
func (r *userRepositoryImpl) Delete(ctx context.Context, id uint64) error {
log.Printf("UserRepository.Delete(%d)\n", id)
_, err := r.db.ExecContext(ctx, "DELETE FROM users WHERE id = ?", id)
if err != nil {
return err
}
return nil
}