@@ -7,31 +7,48 @@ import (
77 "os"
88 "path/filepath"
99 "time"
10+
11+ "labix.org/v2/mgo"
12+ )
13+
14+ var (
15+ Configuration Config
16+ MgoSession * mgo.Session
1017)
1118
1219type Config struct {
1320 Address string `json:"address"`
1421 ReadTimeout time.Duration `json:"read_timeout"`
1522 WriteTimeout time.Duration `json:"write_timeout"`
23+ DBHost string `json:"db_host"`
24+ DBName string `json:"db_name"`
25+ Collection string `json:"collection"`
1626}
1727
1828func main () {
1929 // read config file
2030 configFile , err := os .Open (filepath .Join (
2131 os .Getenv ("GOPATH" ), "src" , "github.com" , "arkxu" , "imgongo" , "config.json" ))
2232 if err != nil {
23- log .Fatal (err )
33+ log .Panicln (err )
2434 }
2535
26- var configuration Config
27- json .NewDecoder (configFile ).Decode (& configuration )
36+ json .NewDecoder (configFile ).Decode (& Configuration )
37+
38+ // Initialize mongo connection
39+ log .Println (Configuration .DBHost )
40+ MgoSession , err = mgo .Dial (Configuration .DBHost )
41+ if err != nil {
42+ log .Panicln (err )
43+ }
2844
2945 // start the server
3046 s := & http.Server {
31- Addr : configuration .Address ,
47+ Addr : Configuration .Address ,
3248 Handler : new (ImgHandler ),
33- ReadTimeout : configuration .ReadTimeout * time .Second ,
34- WriteTimeout : configuration .WriteTimeout * time .Second ,
49+ ReadTimeout : Configuration .ReadTimeout * time .Second ,
50+ WriteTimeout : Configuration .WriteTimeout * time .Second ,
3551 }
36- log .Fatal (s .ListenAndServe ())
52+ log .Panicln (s .ListenAndServe ())
53+
3754}
0 commit comments