-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
118 lines (82 loc) · 2.29 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package main
import (
"errors"
"flag"
"os"
"strings"
"github.com/gloryof/go-crud-practice/crud/config"
"github.com/gloryof/go-crud-practice/crud/config/externals"
"github.com/gloryof/go-crud-practice/crud/config/externals/echo"
"github.com/gloryof/go-crud-practice/crud/config/registry"
"github.com/labstack/gommon/log"
)
var (
paramC = flag.String("c", "./config/development/", "Config base directory")
paramS = flag.String("s", "./static/", "Config base directory")
paramP = flag.Int("p", 8000, "Booting port")
)
// main 起動処理
// -c 設定ファイルのディレクトリパス。デフォルトは実行ファイルがあるディレクトリ内のconfigディレクトリ。
// -s 静的ファイルのルートディレクトリパス。デフォルトはカレントディレクトリ。
func main() {
flag.Parse()
p, pe := loadParameter()
if pe != nil {
handleError(pe)
}
lc, le := config.LoadLogConfig(p)
if le != nil {
handleError(le)
}
// TODO DBの設定は入れたけどトランザクション制御のテストをしていないので後ほど確認する
dc, de := config.LoadDBConfig(p)
if de != nil {
handleError(de)
}
ctx, err := externals.CreateContext(dc)
if err != nil {
handleError(err)
}
defer ctx.Close()
rr, re := registry.Register(lc, ctx)
if re != nil {
handleError(re)
}
e, ee := echo.CreateEcho(p, lc, &rr)
if ee != nil {
handleError(ee)
}
echo.Start(e, *paramP)
}
func loadParameter() (config.CrudParameter, error) {
p := loadBasePath()
if f, err := os.Stat(p); os.IsNotExist(err) || !f.IsDir() {
return config.CrudParameter{}, errors.New("設定ファイルのディレクトリが存在しません[" + p + "]")
}
r := loadStaticRoot()
if f, err := os.Stat(r); os.IsNotExist(err) || !f.IsDir() {
return config.CrudParameter{}, errors.New("静的ファイルのルートディレクトリが存在しません[" + r + "]")
}
return config.CrudParameter{
BasePath: p,
StaticRootDirectory: r,
}, nil
}
func loadBasePath() string {
v := *paramC
if strings.HasSuffix(v, "/") {
return v
}
return v + "/"
}
func loadStaticRoot() string {
v := *paramS
if strings.HasSuffix(v, "/") {
return v
}
return v + "/"
}
func handleError(err error) {
log.Errorf("Error!: %+v\n", err)
os.Exit(1)
}