allowances/main.go

55 lines
1.5 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"
)
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 check_pw = flag.String("checkpw", "", "check if this pw is in db")
var store = sessions.NewCookieStore([]byte("hello world"))
var templates *template.Template
var db *sqlite3.Database
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 if *check_pw != "" {
fmt.Printf("valid password: %v\n", check_password(*check_pw))
} else {
http.HandleFunc("/", homeHandler)
http.HandleFunc("/login", loginHandler)
http.Handle("/s/", http.StripPrefix("/s/",
http.FileServer(http.Dir(*static_files))))
if err := http.ListenAndServe(*addr, nil); err != nil {
log.Fatal("ListenAndServe:", err)
}
}
}