This repository has been archived by the owner on Apr 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
85 lines (71 loc) · 2.2 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
package main
import (
"fmt"
"github.com/kataras/iris/v12"
"github.com/iris-contrib/middleware/cors"
"github.com/kataras/iris/v12/middleware/logger"
"github.com/kataras/iris/v12/middleware/recover"
)
func setCORSAlowAll(ctx iris.Context) iris.Context {
ctx.Header("Access-Control-Allow-Headers", "Accept, Authorization, Content-Type, Origin")
ctx.Header("Access-Control-Allow-Headers", "Accept, Authorization, Content-Type, Origin")
ctx.Header("Access-Control-Allow-Methods", "GET, OPTIONS")
ctx.Header("Access-Control-Allow-Origin", "*")
ctx.Header("Access-Control-Expose-Headers", "Date")
return ctx
}
func newApp() *iris.Application {
app := iris.New()
app.Use(recover.New())
app.Use(logger.New())
app.OnErrorCode(iris.StatusNotFound, func(ctx iris.Context) {
app.Logger().Info(fmt.Sprintf("%v", ctx.GetStatusCode()), " : ", ctx.Path())
// ctx.Exec("GET", "/web")
})
app.RegisterView(iris.HTML("./web", ".html").Binary(Asset, AssetNames))
app.Get("/web", func(ctx iris.Context) {
ctx.View("index.html")
})
app.HandleDir("/", "./web", iris.DirOptions{Asset: Asset, AssetNames: AssetNames})
app.Get(".well-known", func(ctx iris.Context) {
ctx.WriteString("OK")
})
app.Get(".well-known/ready", func(ctx iris.Context) {
ctx.WriteString("OK")
})
app.Get(".well-known/live", func(ctx iris.Context) {
ctx.WriteString("OK")
})
app.Get(".well-known/metrics", func(ctx iris.Context) {
ctx.StatusCode(200)
})
v1 := app.Party("/api/v1")
{
v1.Get("/home", func(ctx iris.Context) {
result := `[{ "label": "foo", "href": "bar" }, { "label": "bar", "href": "foo" }]`
ctx = setCORSAlowAll(ctx)
ctx.Header("Age", "0")
ctx.ContentType("application/json")
ctx.WriteString(string(result))
})
}
crs := cors.New(cors.Options{
AllowedOrigins: []string{"*"}, // allows everything, use that to change the hosts.
AllowCredentials: true,
})
v2 := app.Party("/api/v2")
v2.Use(crs)
{
v2.Get("/home", func(ctx iris.Context) {
result := `[{ "label": "foo", "href": "bar" }, { "label": "bar", "href": "foo" }]`
ctx.Header("Age", "0")
ctx.ContentType("application/json")
ctx.WriteString(result)
})
}
return app
}
func main() {
app := newApp()
app.Run(iris.Addr(":8080"))
}