-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathapp.go
More file actions
53 lines (43 loc) · 980 Bytes
/
app.go
File metadata and controls
53 lines (43 loc) · 980 Bytes
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
package main
import (
"fmt"
"time"
"github.com/daqing/airway/config"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
)
type App struct {
r *gin.Engine
name string // Application name
port string
}
func NewApp(name, port string) *App {
router := gin.New()
router.Use(gin.Logger())
router.Use(gin.Recovery())
router.Use(CORS())
config.Routes(router)
return &App{
r: router,
name: name,
port: port,
}
}
func (a *App) Router() *gin.Engine {
return a.r
}
func (a *App) Run() {
fmt.Printf("%s running at: http://127.0.0.1:%s\n", a.name, a.port)
a.r.Run(":" + a.port)
}
// Default CORS middleware
func CORS() gin.HandlerFunc {
return cors.New(cors.Config{
AllowAllOrigins: true,
AllowMethods: []string{"GET", "POST", "OPTIONS"},
AllowHeaders: []string{"Origin", "Content-Type", "Accept", "Authorization"},
ExposeHeaders: []string{"Content-Length"},
MaxAge: 12 * time.Hour,
AllowCredentials: true,
})
}