-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.go
72 lines (64 loc) · 1.52 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
package main
import (
"flag"
"fmt"
"github.com/idalin/govel/models"
"log"
"os"
"os/signal"
"strings"
"syscall"
"time"
"github.com/idalin/govel/controllers"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/spf13/viper"
)
func main() {
cfgTmp := flag.String("c", "config.json", "configuration file")
version := flag.Bool("v", false, "show version")
help := flag.Bool("h", false, "help")
flag.Parse()
cfg := *cfgTmp
if *version {
fmt.Println("0.1")
os.Exit(0)
}
if *help {
flag.Usage()
os.Exit(0)
}
viper.AddConfigPath(".")
viper.AddConfigPath("/")
viper.AddConfigPath("./config")
viper.AddConfigPath("./api/config")
cfg = strings.Replace(cfg, ".json", "", 1)
viper.SetConfigName(cfg)
err := viper.ReadInConfig()
if err != nil {
log.Fatal(err)
}
corsConfig := cors.New(cors.Config{
AllowOrigins: []string{"*"},
AllowMethods: []string{"PUT", "PATCH", "POST", "GET", "OPTIONS"},
AllowHeaders: []string{"Origin", "X-Requested-With", "UserToken", "Content-Type", "Authorization"},
ExposeHeaders: []string{"Content-Length"},
AllowCredentials: true,
AllowOriginFunc: func(origin string) bool {
return origin == "*"
},
MaxAge: 12 * time.Hour,
})
routes := gin.Default()
routes.Use(corsConfig)
models.InitBS(viper.GetString("source_file"))
go controllers.StartGin(viper.GetString("http.port"), routes)
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-sigs
fmt.Println()
os.Exit(0)
}()
select {}
}