update moon api

master
Jordan Orelli 10 years ago
parent f14741d7fa
commit b1cf12269c

@ -24,12 +24,7 @@ func requestsHandler(w http.ResponseWriter, r *http.Request) {
} }
func appServer() { 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 := http.NewServeMux()
m.HandleFunc("/requests", requestsHandler) m.HandleFunc("/requests", requestsHandler)
http.ListenAndServe(addr, m) http.ListenAndServe(config.AppAddr, m)
} }

@ -6,4 +6,4 @@ proxy_addr: ":8080"
# prox history. # prox history.
app_addr: ":9000" app_addr: ":9000"
dbpath: "history.db" dbpath: "fart_history.db"

@ -28,16 +28,10 @@ func freezeRequest(r *http.Request) error {
} }
func openDB() error { func openDB() error {
var dbpath string
var err error var err error
err = conf.Get("dbpath", &dbpath) log.Printf("opening sqlite file at %s", config.DbPath)
if err != nil { db, err = sql.Open("sqlite3", config.DbPath)
return err
}
log.Printf("opening sqlite file at %s", dbpath)
db, err = sql.Open("sqlite3", dbpath)
if err != nil { if err != nil {
return err return err
} }

@ -1,11 +1,9 @@
package main package main
import ( import (
"flag"
"fmt" "fmt"
"github.com/jordanorelli/moon/lib" "github.com/jordanorelli/moon/lib"
"io" "io"
"log"
"net/http" "net/http"
"net/http/httputil" "net/http/httputil"
"os" "os"
@ -16,9 +14,28 @@ import (
var ( var (
client = new(http.Client) 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) { func httpHandler(w http.ResponseWriter, r *http.Request) {
id := newRequestId() id := newRequestId()
fmt.Printf("%s >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n", id.String()) fmt.Printf("%s >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n", id.String())
@ -82,27 +99,13 @@ func bail(status int, t string, args ...interface{}) {
} }
func proxyListener() { 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 := http.NewServeMux()
m.HandleFunc("/", httpHandler) m.HandleFunc("/", httpHandler)
http.ListenAndServe(addr, m) http.ListenAndServe(config.ProxyAddr, m)
} }
func main() { func main() {
var configPath string moon.Parse(&config)
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)
}
if err := openDB(); err != nil { if err := openDB(); err != nil {
bail(1, "unable to open db: %s", err) bail(1, "unable to open db: %s", err)

Loading…
Cancel
Save