credgrabber/main.go

56 lines
1.1 KiB
Go
Raw Normal View History

2013-01-06 22:12:18 -08:00
package main
import (
"encoding/json"
"flag"
"html/template"
"io/ioutil"
2013-01-06 22:12:18 -08:00
"log"
"net/http"
"path/filepath"
2013-01-06 22:12:18 -08:00
"sync"
)
type Cred struct {
Name string
Password string
}
var creds = make([]Cred, 0)
var m = sync.Mutex{}
var templates *template.Template
2013-01-06 22:12:18 -08:00
func init_db() {
b, err := ioutil.ReadFile(*db_file)
if err != nil {
log.Println("Problem opening db file ... ignoring ...", err)
}
err = json.Unmarshal(b, &creds)
if err != nil {
log.Println("Problem parsing db file ... ignoring ...", err)
creds = make([]Cred, 0)
}
}
2013-01-06 22:12:18 -08:00
func main() {
flag.Parse()
init_db()
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)
}
2013-01-06 22:12:18 -08:00
http.HandleFunc("/", homeHandler)
http.HandleFunc("/hello/", helloHandler)
2013-01-06 22:12:18 -08:00
http.HandleFunc("/pass", passHandler)
2013-01-07 07:55:07 -08:00
http.HandleFunc("/api/1.0/creds/", credHandler)
2013-01-06 22:12:18 -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)
}
}