got storage of hashed passwords working

This commit is contained in:
Stephen McQuay 2013-02-19 22:56:01 -08:00
parent 5f35227f21
commit 562b0311c1
3 changed files with 35 additions and 5 deletions

View File

@ -34,5 +34,9 @@ Dependencies
- place in ./static/bootstrap
Bootstrap DB
============
$ sqlite3 db.sqlite < init_db.sql
.. _bootstrap: http://twitter.github.com/bootstrap/assets/bootstrap.zip

1
init_db.sql Normal file
View File

@ -0,0 +1 @@
CREATE TABLE passes (id INTEGER PRIMARY KEY AUTOINCREMENT, hash STRING);

35
main.go
View File

@ -1,19 +1,44 @@
package main
import (
"code.google.com/p/go.crypto/bcrypt"
"flag"
"fmt"
"github.com/kuroneko/gosqlite3"
"log"
"net/http"
)
var addr = flag.String("addr", ":8000", "http service address")
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 add_pw = flag.String("passwd", "", "add this pass to the db")
func main() {
flag.Parse()
http.Handle("/s/", http.StripPrefix("/s/",
http.FileServer(http.Dir(*static_files))))
if err := http.ListenAndServe(*addr, nil); err != nil {
log.Fatal("ListenAndServe:", err)
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 {
fmt.Printf("%v\n", *add_pw)
http.Handle("/s/", http.StripPrefix("/s/",
http.FileServer(http.Dir(*static_files))))
if err := http.ListenAndServe(*addr, nil); err != nil {
log.Fatal("ListenAndServe:", err)
}
}
}