OLD | NEW |
1 package main | 1 package main |
2 | 2 |
3 import ( | 3 import ( |
4 "fmt" | 4 "fmt" |
| 5 "io/ioutil" |
| 6 "launchpad.net/goyaml" |
5 "launchpad.net/juju/go/log" | 7 "launchpad.net/juju/go/log" |
6 "launchpad.net/juju/go/store" | 8 "launchpad.net/juju/go/store" |
7 stdlog "log" | 9 stdlog "log" |
8 "net/http" | 10 "net/http" |
9 "os" | 11 "os" |
10 "path/filepath" | 12 "path/filepath" |
11 ) | 13 ) |
12 | 14 |
13 func main() { | 15 func main() { |
14 log.Target = stdlog.New(os.Stdout, "", stdlog.LstdFlags) | 16 log.Target = stdlog.New(os.Stdout, "", stdlog.LstdFlags) |
15 err := serve() | 17 err := serve() |
16 if err != nil { | 18 if err != nil { |
17 fmt.Fprintf(os.Stderr, "%v\n", err) | 19 fmt.Fprintf(os.Stderr, "%v\n", err) |
18 os.Exit(1) | 20 os.Exit(1) |
19 } | 21 } |
20 } | 22 } |
21 | 23 |
22 func goodArg(arg string) bool { | 24 type config struct { |
23 » return len(arg) > 0 && arg[0] != '-' | 25 » MongoURL string `yaml:"mongo-url"` |
| 26 » APIAddr string `yaml:"api-addr"` |
| 27 } |
| 28 |
| 29 func readConfig(path string, conf interface{}) error { |
| 30 » f, err := os.Open(path) |
| 31 » if err != nil { |
| 32 » » return fmt.Errorf("opening config file: %v", err) |
| 33 » } |
| 34 » data, err := ioutil.ReadAll(f) |
| 35 » f.Close() |
| 36 » if err != nil { |
| 37 » » return fmt.Errorf("reading config file: %v", err) |
| 38 » } |
| 39 » err = goyaml.Unmarshal(data, conf) |
| 40 » if err != nil { |
| 41 » » return fmt.Errorf("processing config file: %v", err) |
| 42 » } |
| 43 » return nil |
24 } | 44 } |
25 | 45 |
26 func serve() error { | 46 func serve() error { |
27 » if len(os.Args) != 3 || !goodArg(os.Args[1]) || !goodArg(os.Args[2]) { | 47 » var confPath string |
28 » » return fmt.Errorf("usage: %s <mongo addr> <http addr>", filepath
.Base(os.Args[0])) | 48 » if len(os.Args) == 2 { |
| 49 » » if _, err := os.Stat(os.Args[1]); err == nil { |
| 50 » » » confPath = os.Args[1] |
| 51 » » } |
29 } | 52 } |
30 » s, err := store.Open(os.Args[1]) | 53 » if confPath == "" { |
| 54 » » return fmt.Errorf("usage: %s <config path>", filepath.Base(os.Ar
gs[0])) |
| 55 » } |
| 56 » var conf config |
| 57 » err := readConfig(confPath, &conf) |
31 if err != nil { | 58 if err != nil { |
32 return err | 59 return err |
33 } | 60 } |
| 61 if conf.MongoURL == "" || conf.APIAddr == "" { |
| 62 return fmt.Errorf("missing mongo-url or api-addr in config file"
) |
| 63 } |
| 64 s, err := store.Open(conf.MongoURL) |
| 65 if err != nil { |
| 66 return err |
| 67 } |
34 defer s.Close() | 68 defer s.Close() |
35 server, err := store.NewServer(s) | 69 server, err := store.NewServer(s) |
36 if err != nil { | 70 if err != nil { |
37 return err | 71 return err |
38 } | 72 } |
39 » return http.ListenAndServe(os.Args[2], server) | 73 » return http.ListenAndServe(conf.APIAddr, server) |
40 } | 74 } |
OLD | NEW |