Compare commits

...

3 Commits

Author SHA1 Message Date
b7bd99e465 underscore -> go spellings 2015-05-25 16:56:20 -07:00
5f4b24ba35 no need to use templates 2015-05-25 16:54:54 -07:00
96e4a101d8 generate working static file 2015-04-20 23:18:40 -07:00
7 changed files with 443 additions and 58 deletions

View File

@ -5,6 +5,4 @@ import (
) )
var addr = flag.String("addr", ":8000", "http service address") var addr = flag.String("addr", ":8000", "http service address")
var static_files = flag.String("static", "./static", "location of static files") var dbFile = flag.String("db", "db.json", "output database")
var db_file = flag.String("db", "db.json", "output database")
var template_dir = flag.String("templates", "templates", "template dir")

6
gen.go Normal file
View File

@ -0,0 +1,6 @@
package main
//go:generate go get github.com/jteeuwen/go-bindata/...
//go:generate go get github.com/elazarl/go-bindata-assetfs/...
//go:generate rm -f static.go
//go:generate go-bindata -o static.go -pkg=main static/...

View File

@ -7,21 +7,6 @@ import (
"net/http" "net/http"
) )
func homeHandler(c http.ResponseWriter, req *http.Request) {
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) { func passHandler(resp http.ResponseWriter, req *http.Request) {
un := req.FormValue("username") un := req.FormValue("username")
pw := req.FormValue("password") pw := req.FormValue("password")
@ -33,7 +18,7 @@ func passHandler(resp http.ResponseWriter, req *http.Request) {
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
err = ioutil.WriteFile(*db_file, b, 0644) err = ioutil.WriteFile(*dbFile, b, 0644)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }

40
main.go
View File

@ -7,8 +7,10 @@ import (
"io/ioutil" "io/ioutil"
"log" "log"
"net/http" "net/http"
"path/filepath" "os"
"sync" "sync"
"github.com/elazarl/go-bindata-assetfs"
) )
type Cred struct { type Cred struct {
@ -20,8 +22,8 @@ var creds = make([]Cred, 0)
var m = sync.Mutex{} var m = sync.Mutex{}
var templates *template.Template var templates *template.Template
func init_db() { func initDb() {
b, err := ioutil.ReadFile(*db_file) b, err := ioutil.ReadFile(*dbFile)
if err != nil { if err != nil {
log.Println("Problem opening db file ... ignoring ...", err) log.Println("Problem opening db file ... ignoring ...", err)
} }
@ -34,21 +36,27 @@ func init_db() {
func main() { func main() {
flag.Parse() flag.Parse()
init_db() initDb()
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("/pass", passHandler)
http.HandleFunc("/api/1.0/creds/", credHandler) http.HandleFunc("/api/1.0/creds/", credHandler)
http.Handle("/s/", http.StripPrefix("/s/",
http.FileServer(http.Dir(*static_files)))) var fs http.FileSystem
if os.Getenv("DEV") == "" {
log.Printf("static: prod mode")
fs = &assetfs.AssetFS{
Asset: Asset,
AssetDir: AssetDir,
Prefix: "static",
}
} else {
log.Printf("static: dev mode")
fs = http.Dir(os.Getenv("DEV"))
}
http.Handle("/", http.RedirectHandler("/s/", http.StatusMovedPermanently))
http.Handle("/s/", http.StripPrefix("/s/", http.FileServer(fs)))
if err := http.ListenAndServe(*addr, nil); err != nil { if err := http.ListenAndServe(*addr, nil); err != nil {
log.Fatal("ListenAndServe:", err) log.Fatal("ListenAndServe:", err)
} }

411
static.go Normal file

File diff suppressed because one or more lines are too long

View File

@ -1,23 +0,0 @@
<!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>