-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
145 lines (124 loc) · 3.22 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
/*
* @Descripttion: Do not edit
* @version: v0.1.0
* @Author: TSZ201
* @Date: 2021-02-27 23:17:19
* @LastEditors: TSZ201
* @LastEditTime: 2021-02-27 23:17:20
*/
package main
import (
stdContext "context"
"irisdemo/control"
"os"
"os/signal"
"syscall"
"time"
"github.com/iris-contrib/middleware/cors"
"github.com/kataras/iris/v12"
"github.com/kataras/iris/v12/middleware/rate"
"github.com/kataras/iris/v12/middleware/recover"
"github.com/kataras/iris/v12/mvc"
)
func newLogFile(name string) *os.File {
today := time.Now().Format("Jan 02 2006")
filename := name + "-" + today + ".txt"
f, err := os.OpenFile(filename, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {
panic(err)
}
return f
}
// func allowFunc(ctx iris.Context) {
// if auth, _ := sessions.Get(ctx).GetBoolean("authenticated"); !auth {
// ctx.StatusCode(iris.StatusForbidden)
// return
// }
// }
func newApp() *iris.Application {
app := iris.New()
// config app
app.Configure(iris.WithConfiguration(
iris.Configuration{
LogLevel: "debug",
EnableOptimizations: true,
TimeFormat: "Mon, 02 Jan 2006 15:04:05 GMT",
Charset: "utf-8",
},
))
// use compression
app.Use(iris.Compression)
// use recover
app.Use(recover.New())
// use cors
crs := cors.New(cors.Options{
AllowedOrigins: []string{"*"},
AllowCredentials: true,
AllowedMethods: []string{"GET", "POST", "DELETE", "PUT"},
MaxAge: 10,
Debug: true,
})
app.Use(crs)
// user auto set-cookies
// sess := sessions.New(
// sessions.Config{
// Cookie: "session_id",
// AllowReclaim: true,
// Expires: -1,
// DisableSubdomainPersistence: false,
// SessionIDGenerator: sessionId,
// },
// )
// app.Use(sess.Handler())
// use auth
// opts := basicauth.Options{
// Realm: basicauth.DefaultRealm,
// ErrorHandler: basicauth.DefaultErrorHandler,
// Allow: allowFunc(iris.Context),
// }
// auth := basicauth.New(opts)
// app.Use(auth)
// user rate
api := app.Party("/api")
{
apiLimit := rate.Limit(1, 5, rate.PurgeEvery(time.Minute, 5*time.Minute))
api.Use(apiLimit)
}
// use mvc
mvc.New(api.Party("/teachers")).Handle(control.NewTeacherController())
mvc.New(api.Party("/users")).Handle(control.NewUserController())
mvc.New(api.Party("/classes")).Handle(control.NewClassController())
mvc.New(api.Party("/students")).Handle(control.NewStudentController())
return app
}
func main() {
app := newApp()
f1 := newLogFile("debug")
f2 := newLogFile("error")
app.Logger().SetLevelOutput("debug", f1).SetLevelOutput("error", f2)
defer f1.Close()
defer f2.Close()
go func() {
ch := make(chan os.Signal, 1)
signal.Notify(ch,
os.Interrupt,
syscall.SIGINT,
os.Kill,
syscall.SIGKILL,
syscall.SIGTERM,
)
select {
case <-ch:
println("shutdown...")
timeout := 5 * time.Second
ctx, cancel := stdContext.WithTimeout(stdContext.Background(), timeout)
defer cancel()
app.Shutdown(ctx)
}
}()
if err := app.Run(iris.Addr(":8080"), iris.WithoutInterruptHandler); err != nil {
app.Logger().Errorf("Shutdonw with error:%v\n", err)
} else {
app.Logger().Info("Server is running")
}
}