-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
77 lines (61 loc) · 1.66 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
package main
import (
"github.com/iris-contrib/middleware/cors"
"github.com/kataras/iris"
"golang-AppDistribution/app/helper"
"golang-AppDistribution/routes"
"net/http"
"os"
"os/exec"
"path/filepath"
"strings"
"fmt"
)
func newApp() *iris.Application {
app := iris.New()
//获取当前执行文件的路径
file, _ := exec.LookPath(os.Args[0])
AppPath, _ := filepath.Abs(file)
losPath, _ := filepath.Split(AppPath)
dirPath := losPath +"apidoc"
app.Get("/apidoc", func(ctx iris.Context) {
ctx.ServeFile(dirPath+"/index.html", false)
})
fileServer := app.StaticHandler(dirPath, false, false)
app.WrapRouter(func(w http.ResponseWriter, r *http.Request, router http.HandlerFunc) {
path := r.URL.Path
if !strings.Contains(path, ".") {
router(w, r)
return
}
ctx := app.ContextPool.Acquire(w, r)
fileServer(ctx)
app.ContextPool.Release(ctx)
})
return app
}
func main() {
app := newApp()
app.WrapRouter(cors.WrapNext(cors.Options{
AllowedOrigins: []string{"*"},
AllowedMethods: []string{"*"},
AllowedHeaders: []string{"*"},
}))
// 访问日志处理
r, close := helper.NewRequestLogger()
defer close()
app.Use(r)
app.OnAnyErrorCode(r, func(ctx iris.Context) {
ctx.Writef("(Unexpected) internal server error")
})
// 记录程序启动日志
app.Use(func(this iris.Context) {
this.Application().Logger().Infof("Begin request for path %s", this.Path())
fmt.Println(this.Path())
this.Next()
})
//开启路由监听
routes.WebRoutes{}.StartRoute(app)
//监听端口,并且输出启动日志,设置输出编码
app.Run(iris.Addr(":8081"), iris.WithoutStartupLog, iris.WithCharset("UTF-8"), iris.WithoutVersionChecker)
}