-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
77 lines (63 loc) · 1.39 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
package main
import (
"fmt"
"github.com/deweppro/goppy"
"github.com/deweppro/goppy/plugins"
"github.com/deweppro/goppy/plugins/database"
"github.com/deweppro/goppy/plugins/web"
)
func main() {
app := goppy.New()
app.WithConfig("./config.yaml")
app.Plugins(
web.WithHTTPDebug(),
web.WithHTTP(),
database.WithMySQL(),
database.WithSQLite(),
)
app.Plugins(
plugins.Plugin{
Inject: NewController,
Resolve: func(routes web.RouterPool, c *Controller) {
router := routes.Main()
router.Use(web.ThrottlingMiddleware(100))
router.Get("/users", c.Users)
api := router.Collection("/api/v1", web.ThrottlingMiddleware(100))
api.Get("/user/{id}", c.User)
},
},
)
app.Run()
}
type Controller struct {
mdb database.MySQL
sdb database.SQLite
}
func NewController(
m database.MySQL,
s database.SQLite,
) *Controller {
return &Controller{
mdb: m,
sdb: s,
}
}
func (v *Controller) Users(ctx web.Context) {
data := []int64{1, 2, 3, 4}
ctx.JSON(200, data)
}
func (v *Controller) User(ctx web.Context) {
id, _ := ctx.Param("id").Int()
err := v.mdb.Pool("main").Ping()
if err != nil {
ctx.ErrorJSON(500, err, web.ErrCtx{"id": id})
return
}
err = v.sdb.Pool("main").Ping()
if err != nil {
ctx.ErrorJSON(500, err, web.ErrCtx{"id": id})
return
}
ctx.ErrorJSON(400, fmt.Errorf("user not found"), web.ErrCtx{"id": id})
ctx.Log().Infof("user - %d", id)
}