2013-01-06 15:20:54 -08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2013-02-19 22:56:01 -08:00
|
|
|
"code.google.com/p/go.crypto/bcrypt"
|
2013-01-06 15:20:54 -08:00
|
|
|
"flag"
|
2013-02-19 22:56:01 -08:00
|
|
|
"fmt"
|
2013-02-20 21:11:44 -08:00
|
|
|
"github.com/gorilla/sessions"
|
2013-02-19 22:56:01 -08:00
|
|
|
"github.com/kuroneko/gosqlite3"
|
2013-02-20 21:11:44 -08:00
|
|
|
"html/template"
|
2013-01-06 15:20:54 -08:00
|
|
|
"log"
|
|
|
|
"net/http"
|
2013-02-20 21:11:44 -08:00
|
|
|
"path/filepath"
|
2013-01-06 15:20:54 -08:00
|
|
|
)
|
|
|
|
|
2013-02-19 22:56:01 -08:00
|
|
|
var addr = flag.String("addr", ":8000", "address I'll listen on.")
|
2013-01-06 15:29:31 -08:00
|
|
|
var static_files = flag.String("static", "./static", "location of static files")
|
2013-02-19 22:56:01 -08:00
|
|
|
var db_file = flag.String("db", "./db.sqlite", "the database")
|
2013-02-20 21:11:44 -08:00
|
|
|
var template_dir = flag.String("templates", "templates", "template dir")
|
2013-02-19 22:56:01 -08:00
|
|
|
var add_pw = flag.String("passwd", "", "add this pass to the db")
|
2013-01-06 15:20:54 -08:00
|
|
|
|
2013-02-20 21:11:44 -08:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-06 15:20:54 -08:00
|
|
|
func main() {
|
|
|
|
flag.Parse()
|
2013-02-19 22:56:01 -08:00
|
|
|
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 {
|
2013-02-20 21:11:44 -08:00
|
|
|
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)
|
2013-02-19 22:56:01 -08:00
|
|
|
http.Handle("/s/", http.StripPrefix("/s/",
|
|
|
|
http.FileServer(http.Dir(*static_files))))
|
|
|
|
if err := http.ListenAndServe(*addr, nil); err != nil {
|
|
|
|
log.Fatal("ListenAndServe:", err)
|
|
|
|
}
|
2013-01-06 15:20:54 -08:00
|
|
|
}
|
|
|
|
}
|