-
-
Notifications
You must be signed in to change notification settings - Fork 178
/
main.go
121 lines (96 loc) · 3.34 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
package main
import (
"fmt"
"github.com/kataras/iris/v12"
"github.com/kataras/iris/v12/middleware/accesslog"
"github.com/kataras/iris/v12/middleware/recover"
"github.com/kataras/iris/v12/sessions"
"github.com/kataras/iris/v12/mvc"
)
func main() {
app := iris.New()
app.Logger().SetLevel("debug")
basic := app.Party("/basic")
{
// Register middlewares to run under the /basic path prefix.
ac := accesslog.File("./basic_access.log")
defer ac.Close()
basic.UseRouter(ac.Handler)
basic.UseRouter(recover.New())
mvc.Configure(basic, basicMVC)
}
app.Listen(":8080")
}
func basicMVC(app *mvc.Application) {
// Disable verbose logging of controllers for this and its children mvc apps
// when the log level is "debug":
app.SetControllersNoLog(true)
// You can still register middlewares at MVC apps of course.
// The app.Router returns the Party that this MVC
// was registered on.
// app.Router.UseRouter/Use/....
// Register dependencies which will be binding to the controller(s),
// can be either a function which accepts an iris.Context and returns a single value (dynamic binding)
// or a static struct value (service).
app.Register(
sessions.New(sessions.Config{}).Start,
&prefixedLogger{prefix: "DEV"},
accesslog.GetFields, // Set custom fields through a controller or controller's methods.
)
// GET: http://localhost:8080/basic
// GET: http://localhost:8080/basic/custom
// GET: http://localhost:8080/basic/custom2
app.Handle(new(basicController))
// All dependencies of the parent *mvc.Application
// are cloned to this new child,
// thefore it has access to the same session as well.
// GET: http://localhost:8080/basic/sub
app.Party("/sub").
Handle(new(basicSubController))
}
// If controller's fields (or even its functions) expecting an interface
// but a struct value is binded then it will check
// if that struct value implements
// the interface and if true then it will add this to the
// available bindings, as expected, before the server ran of course,
// remember? Iris always uses the best possible way to reduce load
// on serving web resources.
type LoggerService interface {
Log(string)
}
type prefixedLogger struct {
prefix string
}
func (s *prefixedLogger) Log(msg string) {
fmt.Printf("%s: %s\n", s.prefix, msg)
}
type basicController struct {
Logger LoggerService // the static logger service attached to this app.
Session *sessions.Session // current HTTP session.
LogFields *accesslog.Fields // accesslog middleware custom fields.
}
func (c *basicController) BeforeActivation(b mvc.BeforeActivation) {
b.HandleMany("GET", "/custom /custom2", "Custom")
}
func (c *basicController) AfterActivation(a mvc.AfterActivation) {
if a.Singleton() {
panic("basicController should be stateless, a request-scoped, we have a 'Session' which depends on the context.")
}
}
func (c *basicController) Get() string {
count := c.Session.Increment("count", 1)
c.LogFields.Set("count", count)
body := fmt.Sprintf("Hello from basicController\nTotal visits from you: %d", count)
c.Logger.Log(body)
return body
}
func (c *basicController) Custom() string {
return "custom"
}
type basicSubController struct {
Session *sessions.Session
}
func (c *basicSubController) Get() string {
count := c.Session.GetIntDefault("count", 1)
return fmt.Sprintf("Hello from basicSubController.\nRead-only visits count: %d", count)
}