-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
98 lines (86 loc) · 2.42 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
package main
import (
"net/http"
"time"
"amdl/config"
"amdl/db"
"amdl/models"
"amdl/routers"
"github.com/iris-contrib/middleware/cors"
"github.com/kataras/iris"
"github.com/kataras/iris/cache"
"github.com/kataras/iris/middleware/logger"
"github.com/kataras/iris/middleware/recover"
)
type User1 struct {
Name string `json:"name"`
Age string `json:"age"`
}
const refreshEvery = 10 * time.Second
func main() {
db.ConnectAndInit(
config.Conf,
new(models.Article),
new(models.Comments),
new(models.Photos),
new(models.RecomPictures),
new(models.User),
new(models.Follow),
)
// defer db.DB.Close()
iris.RegisterOnInterrupt(func() {
db.DB.Close()
})
app := iris.New()
app.Use(logger.New())
app.Use(recover.New())
app.Use(iris.Cache304(refreshEvery))
app.Get("*", cache.Handler(10*time.Second), func(ctx iris.Context) {})
crs := cors.New(cors.Options{
AllowedOrigins: []string{"*"}, // allows everything, use that to change the hosts.
AllowCredentials: true,
AllowedMethods: []string{"PUT", "PATCH", "GET", "POST", "OPTIONS"},
AllowedHeaders: []string{"*", "Content-Type", "Content-Length", "Accept", "X-Requested-With", "Origin", "Authorization"},
ExposedHeaders: []string{"Accept", "Content-Type", "Content-Length", "Accept-Encoding", "X-CSRF-Token", "Authorization"},
})
// crs := cors.New(cors.Options{
// AllowedOrigins: []string{"*"}, // allows everything, use that to change the hosts.
// AllowedHeaders: []string{"*"},
// AllowCredentials: true,
// })
v1 := app.Party("/", crs).AllowMethods(iris.MethodOptions) // <- 对于预检很重要。
{
v1.Get("/abcd", func(ctx iris.Context) {
ctx.JSON(iris.Map{
"aaa": "成功111",
})
}) //登录
}
// crs := cors.New(cors.Options{
// // AllowedOrigins: []string{"http://foo.com"}, //允许通过的主机名称
// AllowedOrigins: []string{"*"}, //允许通过的主机名称
// AllowCredentials: true,
// })
app.Post("/user", func(ctx iris.Context) {
c := &User1{}
if err := ctx.ReadForm(c); err != nil {
ctx.JSON(iris.Map{
"AAA": "AAA",
})
} else {
ctx.JSON(c)
}
})
router := routers.InitRouters(app)
h := app.NewHost(&http.Server{
Addr: ":8080",
Handler: router,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
})
h.RegisterOnShutdown(func() {
println("server terminated")
})
app.Run(iris.Raw(h.ListenAndServe))
}