-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
55 lines (47 loc) · 1.74 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
package main
import (
"github.com/iris-contrib/mvc-template/controllers"
"github.com/iris-contrib/mvc-template/services"
"github.com/kataras/iris/v12"
"github.com/kataras/iris/v12/mvc"
"github.com/kataras/iris/v12/middleware/accesslog"
"github.com/kataras/iris/v12/middleware/recover"
)
func main() {
app := iris.New()
// Serve our front-end and its assets.
app.HandleDir("/", iris.Dir("./app/public"))
// Note, it's buffered, so make sure it's closed so it can flush any buffered contents.
ac := accesslog.File("./access.log")
defer ac.Close()
app.UseRouter(ac.Handler)
app.UseRouter(recover.New())
// Group routes and mvc apps based on /api path prefix.
api := app.Party("/api")
{
// Group based on /api/counter path prefix.
counterAPI := api.Party("/counter")
// Optionally, a <trick> to keep the `m` local variable
// unaccessible outside of this block's scope. That
// way you can register many mvc apps for different Parties
// with a "m" variable.
// Alternatively you can use the mvc.Configure function :)
// Register a new MVC Application to the counterAPI Party.
m := mvc.New(counterAPI)
m.Register(
// Register a static dependency (static because it doesn't accept an iris.Context,
// only one instance of that it's used). Helps us to keep a global counter across
// clients requests.
services.NewGlobalCounter(),
// Register a dynamic dependency (GetFields accepts an iris.Context,
// it binds a new instance on every request). Helps us to
// set custom fields based on the request handler.
accesslog.GetFields,
)
// Register our controller.
m.Handle(new(controllers.Counter))
}
// GET http://localhost:8080/api/counter
// POST http://localhost:8080/api/counter/increment
app.Listen(":8080")
}