-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
100 lines (88 loc) · 1.93 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
99
100
package main
import (
"flag"
"fmt"
"github.com/HongJungWan/commerce-system/docs"
"github.com/HongJungWan/commerce-system/internal/helper"
configs "github.com/HongJungWan/commerce-system/internal/infrastructure/configs"
"github.com/HongJungWan/commerce-system/internal/infrastructure/router"
"github.com/spf13/viper"
"gorm.io/gorm"
"net/http"
"os"
)
var (
conf = configs.Config{}
file string
)
// @title commerce-system API
// @version 1.0
// @securityDefinitions.apikey Bearer
// @in header
// @name Authorization
// @description A commerce-system service API in Go using Gin framework
// @host localhost:3031
// @BasePath /api
func main() {
if !parseConfig() {
helper.ShowHelp()
os.Exit(-1)
}
initializeSwaggerHost(&conf)
db := configs.ConnectionDB(&conf)
startServer(db)
}
func loadConfig() bool {
_, err := os.Stat(file)
if err != nil {
return false
}
viper.SetConfigFile(file)
viper.SetConfigType("toml")
err = viper.ReadInConfig()
if err != nil {
fmt.Println(err)
return false
}
fmt.Println(conf.DBHost)
err = viper.GetViper().Unmarshal(&conf)
if err != nil {
fmt.Println(err)
return false
}
return true
}
func parseConfig() bool {
flag.StringVar(&file, "c", "config.toml", "config file")
flag.Parse()
if !loadConfig() {
return false
}
return true
}
func startServer(db *gorm.DB) {
routers := router.NewRouter(conf, db)
server := &http.Server{
Addr: "0.0.0.0:3031",
Handler: routers,
}
err := server.ListenAndServe()
if err != nil {
helper.ErrorPanic(err)
}
}
func initializeSwaggerHost(conf *configs.Config) {
docs.SwaggerInfo.Host = conf.Host
docs.SwaggerInfo.Schemes = conf.Scheme
docs.SwaggerInfo.Version = conf.Version
docs.SwaggerInfo.BasePath = conf.BasePath
docs.SwaggerInfo.Title = conf.Title
fmt.Printf(
"설정된 Swagger 정보:\nHost: %s\nSchemes: %v\nVersion: %s\nBasePath: %s\nTitle: %s\n",
conf.Host,
conf.Scheme,
conf.Version,
conf.BasePath,
conf.Title,
)
}