package main import ( "database/sql" "encoding/hex" "fmt" "html/template" "log" "net/http" "net/http/httputil" "github.com/gorilla/securecookie" ) type handler func(DataStore, http.ResponseWriter, *http.Request) type App struct { DataStore DB *sql.DB Config *Config CookieHandler *securecookie.SecureCookie Template *template.Template } func New() *App { app := new(App) config := NewConfig() app.Config = config db, err := sql.Open("postgres", config.Database) if err = db.Ping(); err != nil { log.Fatal(err) } app.DB = db app.DataStore = &SqlStore{db} hashKey, err := hex.DecodeString(config.HashKey) blockKey, err := hex.DecodeString(config.HashKey) s := securecookie.New(hashKey, blockKey) app.CookieHandler = s templates := template.Must(template.ParseGlob("templates/*.tmpl")) app.Template = templates return app } func logg(fn http.HandlerFunc) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { d, _ := httputil.DumpRequest(r, true) fmt.Printf("--------------------------\n%s\n", d) r.ParseForm() fn(w, r) } } func main() { app := New() go func() { sm := http.NewServeMux() sm.HandleFunc("/", logg(app.mainHandler)) sm.HandleFunc("/np", logg(app.nowPlayingHandler)) sm.HandleFunc("/scrobble", logg(app.scrobbleHandler)) //http.HandleFunc("/2.0/", logg(app.ApiHandler)) http.ListenAndServe(":3001", sm) }() app.TrackInfo("believe", "cher") sm := http.NewServeMux() fs := http.FileServer(http.Dir("static")) sm.Handle("/static/", http.StripPrefix("/static/", fs)) sm.HandleFunc("/", app.root) sm.HandleFunc("/callback", app.callback) sm.HandleFunc("/login", app.login) sm.HandleFunc("/settings", app.settings) http.ListenAndServe(":8000", sm) }