11package config
22
33import (
4+ "flag"
5+ "github.com/GuoYuefei/DOStorage1/distributed/utils"
6+ "gopkg.in/yaml.v2"
7+ "io/ioutil"
8+ "log"
49 "os"
10+ "os/exec"
11+ "path"
12+ "path/filepath"
513)
614
15+ /**
16+ 如果是相对位置,则是相对执行文件的位置
17+ */
18+ var ConfigFile string
19+
720var Pub * SPub
821var ServerInf * SInf
922var ServerData * SData
1023
24+ const usage string = "-c configFilePath"
25+
26+ type ServerType = int
27+ const (
28+ TypeSInf = iota
29+ TypeSData
30+ )
31+
32+ var ObjectRoot string
33+ var TempRoot string
34+
35+ func Init () {
36+ ObjectRoot = filepath .Join (ServerData .STORAGE_ROOT , "objects" )
37+ TempRoot = filepath .Join (ServerData .STORAGE_ROOT , "temp" )
38+ // make dir for ./data/objects
39+ _ , e := os .Stat (ObjectRoot )
40+ if e != nil {
41+ e := os .MkdirAll (ObjectRoot , os .ModePerm )
42+ if e != nil {
43+ log .Fatal (e )
44+ }
45+ }
46+
47+ _ , e = os .Stat (TempRoot )
48+ if e != nil {
49+ e := os .MkdirAll (TempRoot , os .ModePerm )
50+ if e != nil {
51+ log .Fatal (e )
52+ }
53+ }
54+ }
55+
1156func init () {
1257
1358 Pub = & SPub {
@@ -17,44 +62,93 @@ func init() {
1762 ServerInf = & SInf {
1863 LISTEN_ADDRESS : "localhost:23333" ,
1964 }
65+ ServerInf .SPub = Pub
2066 ServerData = & SData {
2167 LISTEN_ADDRESS : "localhost:23334" ,
2268 STORAGE_ROOT : "./data" ,
2369 }
70+ ServerData .SPub = Pub
71+
72+ }
73+
74+ // serverType
75+ // TypeSInf interface server
76+ // TypeSData data server
77+ func ConfigParse (serverType ServerType ) {
78+
79+ switch serverType {
80+ case TypeSInf :
81+ interfaceConfigFile , err := ioutil .ReadFile (path .Join (ConfigFile ))
82+ if err == nil {
83+ yaml .Unmarshal (interfaceConfigFile , ServerInf )
84+ }
85+ utils .Log .Printf (utils .Debug , "after read config file, %v\n " , ServerInf , Pub )
86+ case TypeSData :
87+ dataConfigFile , err := ioutil .ReadFile (path .Join (ConfigFile ))
88+ if err == nil {
89+ yaml .Unmarshal (dataConfigFile , ServerData )
90+ }
91+ utils .Log .Printf (utils .Debug , "after read config file, %v\n " , ServerData , Pub )
92+ default :
93+ utils .Log .Println (utils .Warning , "Server Type No MATCH!" )
94+ }
2495
2596 if os .Getenv ("RABBITMQ_SERVER" ) != "" {
2697 Pub .RABBITMQ_SERVER = os .Getenv ("RABBITMQ_SERVER" )
2798 }
2899 if os .Getenv ("ES_SERVER" ) != "" {
29100 Pub .ES_SERVER = os .Getenv ("ES_SERVER" )
30101 }
31-
32102 if os .Getenv ("LISTEN_ADDRESS" ) != "" {
33103 ServerData .LISTEN_ADDRESS = os .Getenv ("LISTEN_ADDRESS" )
34104 ServerInf .LISTEN_ADDRESS = ServerData .LISTEN_ADDRESS
35105 }
36-
37106 if os .Getenv ("STORAGE_ROOT" ) != "" {
38107 ServerData .STORAGE_ROOT = os .Getenv ("STORAGE_ROOT" )
39108 }
109+ // 最后如果STORAGE_ROOT是相对位置的话,转成绝对路径
110+ if ! filepath .IsAbs (ServerData .STORAGE_ROOT ) {
111+ exePath , _ := exec .LookPath (os .Args [0 ])
112+ path , _ := filepath .Abs (filepath .Dir (exePath ))
113+ ServerData .STORAGE_ROOT = filepath .Join (path , ServerData .STORAGE_ROOT )
114+ }
40115
41- // todo config
42-
116+ // 配置结束后初始化
117+ Init ()
118+ }
43119
120+ func Flags (serverType ServerType ) {
121+ switch serverType {
122+ case TypeSInf :
123+ flag .StringVar (& ConfigFile , "c" , "./config/interface.yml" , usage )
124+ flag .StringVar (& ConfigFile , "-config" , "./config/interface.yml" , usage )
125+ case TypeSData :
126+ flag .StringVar (& ConfigFile , "c" , "./config/data.yml" , usage )
127+ flag .StringVar (& ConfigFile , "-config" , "./config/data.yml" , usage )
128+ default :
129+ utils .Log .Println (utils .Warning , "Server Type No MATCH!" )
130+ }
131+ // 相对位置转换成绝对位置
132+ if ! filepath .IsAbs (ConfigFile ) {
133+ exePath , _ := exec .LookPath (os .Args [0 ])
134+ path , _ := filepath .Abs (filepath .Dir (exePath ))
135+ ConfigFile = filepath .Join (path , ConfigFile )
136+ }
137+ utils .Log .Println (utils .Info , "Use config file: " , ConfigFile )
44138}
45139
46140type SPub struct {
47- ES_SERVER string
48- RABBITMQ_SERVER string
141+ ES_SERVER string `yaml:"es_server"`
142+ RABBITMQ_SERVER string `yaml:"rabbitmq_server"`
49143}
50144
51145type SInf struct {
52- * SPub
53- LISTEN_ADDRESS string
146+ * SPub `yaml:"public"`
147+ LISTEN_ADDRESS string `yaml:"listen_address"`
54148}
55149
56150type SData struct {
57- * SPub
58- LISTEN_ADDRESS string
59- STORAGE_ROOT string
151+ * SPub `yaml:"public"`
152+ LISTEN_ADDRESS string `yaml:"listen_address"`
153+ STORAGE_ROOT string `yaml:"storage_root"`
60154}
0 commit comments