allowances/main.go
2013-02-20 21:11:44 -08:00

69 lines
1.7 KiB
Go

package main
import (
"code.google.com/p/go.crypto/bcrypt"
"flag"
"fmt"
"github.com/gorilla/sessions"
"github.com/kuroneko/gosqlite3"
"html/template"
"log"
"net/http"
"path/filepath"
)
var addr = flag.String("addr", ":8000", "address I'll listen on.")
var static_files = flag.String("static", "./static", "location of static files")
var db_file = flag.String("db", "./db.sqlite", "the database")
var template_dir = flag.String("templates", "templates", "template dir")
var add_pw = flag.String("passwd", "", "add this pass to the db")
var store = sessions.NewCookieStore([]byte("hello world"))
var templates *template.Template
func homeHandler(c http.ResponseWriter, req *http.Request) {
log.Printf("%v\n", req.URL)
t := T("index.html")
if t != nil {
t.Execute(c, req.Host)
} else {
log.Fatal("template index.html not found")
}
}
func main() {
flag.Parse()
if *add_pw != "" {
hpass, err := bcrypt.GenerateFromPassword([]byte(*add_pw), bcrypt.DefaultCost)
if err != nil {
log.Fatal(err)
}
db, err := sqlite3.Open(*db_file)
defer db.Close()
if err != nil {
log.Fatal(err)
}
cmd := fmt.Sprintf("INSERT INTO passes ('id', 'hash') VALUES (null, '%v')",
string(hpass))
_, err = db.Execute(cmd)
if err != nil {
log.Fatal(err)
}
} else {
pattern := filepath.Join(*template_dir, "*.html")
var err error
templates, err = template.ParseGlob(pattern)
if err != nil {
println(*template_dir)
println(pattern)
log.Fatal("problem parsing template dir:", *template_dir)
}
http.HandleFunc("/", homeHandler)
http.Handle("/s/", http.StripPrefix("/s/",
http.FileServer(http.Dir(*static_files))))
if err := http.ListenAndServe(*addr, nil); err != nil {
log.Fatal("ListenAndServe:", err)
}
}
}