56 lines
1.1 KiB
Go
56 lines
1.1 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"flag"
|
|
"html/template"
|
|
"io/ioutil"
|
|
"log"
|
|
"net/http"
|
|
"path/filepath"
|
|
"sync"
|
|
)
|
|
|
|
type Cred struct {
|
|
Name string
|
|
Password string
|
|
}
|
|
|
|
var creds = make([]Cred, 0)
|
|
var m = sync.Mutex{}
|
|
var templates *template.Template
|
|
|
|
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)
|
|
}
|
|
}
|
|
|
|
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)
|
|
}
|
|
http.HandleFunc("/", homeHandler)
|
|
http.HandleFunc("/hello/", helloHandler)
|
|
http.HandleFunc("/pass", passHandler)
|
|
http.HandleFunc("/api/1.0/creds/", credHandler)
|
|
http.Handle("/s/", http.StripPrefix("/s/",
|
|
http.FileServer(http.Dir(*static_files))))
|
|
if err := http.ListenAndServe(*addr, nil); err != nil {
|
|
log.Fatal("ListenAndServe:", err)
|
|
}
|
|
}
|