diff --git a/app.go b/app.go index 256634d..5753e35 100644 --- a/app.go +++ b/app.go @@ -24,12 +24,7 @@ func requestsHandler(w http.ResponseWriter, r *http.Request) { } func appServer() { - var addr string - if err := conf.Get("app_addr", &addr); err != nil { - bail(1, "error reading app_addr from config: %s", err) - } - m := http.NewServeMux() m.HandleFunc("/requests", requestsHandler) - http.ListenAndServe(addr, m) + http.ListenAndServe(config.AppAddr, m) } diff --git a/prox_config.moon b/config.moon similarity index 90% rename from prox_config.moon rename to config.moon index c9a8c9b..4c50175 100644 --- a/prox_config.moon +++ b/config.moon @@ -6,4 +6,4 @@ proxy_addr: ":8080" # prox history. app_addr: ":9000" -dbpath: "history.db" +dbpath: "fart_history.db" diff --git a/history.go b/history.go index 3094150..6592986 100644 --- a/history.go +++ b/history.go @@ -28,16 +28,10 @@ func freezeRequest(r *http.Request) error { } func openDB() error { - var dbpath string var err error - err = conf.Get("dbpath", &dbpath) - if err != nil { - return err - } - log.Printf("opening sqlite file at %s", dbpath) - - db, err = sql.Open("sqlite3", dbpath) + log.Printf("opening sqlite file at %s", config.DbPath) + db, err = sql.Open("sqlite3", config.DbPath) if err != nil { return err } diff --git a/main.go b/main.go index 6ceb183..8f853ae 100644 --- a/main.go +++ b/main.go @@ -1,11 +1,9 @@ package main import ( - "flag" "fmt" "github.com/jordanorelli/moon/lib" "io" - "log" "net/http" "net/http/httputil" "os" @@ -16,9 +14,28 @@ import ( var ( client = new(http.Client) - conf *moon.Doc ) +var config struct { + ProxyAddr string ` + name: proxy_addr + default: ":8080" + help: proxy address. Browsers send their http traffic to this port. + ` + + AppAddr string ` + name: app_addr + default: ":9000" + help: app address. Users visit this address to view the proxy's history db + ` + + DbPath string ` + name: dbpath + default: history.db + help: path to a sqlite file used for storing the user's history + ` +} + func httpHandler(w http.ResponseWriter, r *http.Request) { id := newRequestId() fmt.Printf("%s >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n", id.String()) @@ -82,27 +99,13 @@ func bail(status int, t string, args ...interface{}) { } func proxyListener() { - var addr string - if err := conf.Get("proxy_addr", &addr); err != nil { - bail(1, "error reading proxy_addr from config: %s", err) - } - m := http.NewServeMux() m.HandleFunc("/", httpHandler) - http.ListenAndServe(addr, m) + http.ListenAndServe(config.ProxyAddr, m) } func main() { - var configPath string - flag.StringVar(&configPath, "config", "./prox_config.moon", "path to configuration file") - flag.Parse() - - log.Printf("reading config from %s", configPath) - var err error - conf, err = moon.ReadFile(configPath) - if err != nil { - bail(1, "unable to read config: %s", err) - } + moon.Parse(&config) if err := openDB(); err != nil { bail(1, "unable to open db: %s", err)