diff --git a/accounts/account.go b/accounts/account.go new file mode 100644 index 0000000..11a6272 --- /dev/null +++ b/accounts/account.go @@ -0,0 +1,8 @@ +package accounts + +type Account struct { +} + +func NewAccount() (a *Account) { + return &Account{} +} diff --git a/accounts/handlers.go b/accounts/handlers.go new file mode 100644 index 0000000..972ce62 --- /dev/null +++ b/accounts/handlers.go @@ -0,0 +1,14 @@ +package accounts + +import ( + "github.com/sunfmin/gowebapp/layout" + "net/http" +) + +func Index(w http.ResponseWriter, r *http.Request) { + layout.Templates.ExecuteTemplate(w, "accounts/index", nil) +} + +func Edit(w http.ResponseWriter, r *http.Request) { + layout.Templates.ExecuteTemplate(w, "accounts/edit", nil) +} diff --git a/home/handlers.go b/home/handlers.go new file mode 100644 index 0000000..efc562d --- /dev/null +++ b/home/handlers.go @@ -0,0 +1,10 @@ +package home + +import ( + "github.com/sunfmin/gowebapp/layout" + "net/http" +) + +func Index(w http.ResponseWriter, r *http.Request) { + layout.Templates.ExecuteTemplate(w, "home/index", nil) +} diff --git a/layout/templates.go b/layout/templates.go new file mode 100644 index 0000000..61d705e --- /dev/null +++ b/layout/templates.go @@ -0,0 +1,48 @@ +package layout + +import ( + "net/http" + "text/template" +) + +var Templates *template.Template + +func init() { + var err error + Templates, err = template.ParseGlob("templates/*/*.html") + if err != nil { + panic(err) + } +} + +type Header struct { +} + +type Footer struct { +} + +func MainLayout(handler http.HandlerFunc) (r http.HandlerFunc) { + + return func(w http.ResponseWriter, r *http.Request) { + header := &Header{} + footer := &Footer{} + Templates.ExecuteTemplate(w, "mainHeader", header) + handler(w, r) + Templates.ExecuteTemplate(w, "mainFooter", footer) + } + +} + +type SimpleHeader struct { +} + +func SimpleLayout(handler http.HandlerFunc) (r http.HandlerFunc) { + return func(w http.ResponseWriter, r *http.Request) { + header := &SimpleHeader{} + footer := &Footer{} + Templates.ExecuteTemplate(w, "simpleHeader", header) + handler(w, r) + Templates.ExecuteTemplate(w, "simpleFooter", footer) + } + +} diff --git a/main.go b/main.go new file mode 100644 index 0000000..33d4f25 --- /dev/null +++ b/main.go @@ -0,0 +1,9 @@ +package gowebapp + +import ( + "net/http" +) + +func main() { + http.ListenAndServe("localhost:5000", NewMux()) +} diff --git a/routes.go b/routes.go new file mode 100644 index 0000000..ebbda2e --- /dev/null +++ b/routes.go @@ -0,0 +1,19 @@ +package gowebapp + +import ( + "github.com/bmizerany/pat" + "github.com/sunfmin/gowebapp/accounts" + "github.com/sunfmin/gowebapp/home" + "github.com/sunfmin/gowebapp/layout" + "net/http" +) + +func NewMux() (r *pat.PatternServeMux) { + m := pat.New() + m.Get("/", http.HandlerFunc(layout.MainLayout(home.Index))) + m.Get("/accounts", http.HandlerFunc(layout.MainLayout(accounts.Index))) + m.Get("/accounts/:id/edit", http.HandlerFunc(layout.MainLayout(accounts.Edit))) + m.Get("/accounts/:id/popup_edit", http.HandlerFunc(layout.SimpleLayout(accounts.Edit))) + + return m +} diff --git a/routes_test.go b/routes_test.go new file mode 100644 index 0000000..3380ff6 --- /dev/null +++ b/routes_test.go @@ -0,0 +1,56 @@ +package gowebapp + +import ( + "io/ioutil" + "net/http" + "net/http/httptest" + "strings" + "testing" +) + +func stringbody(res *http.Response, t *testing.T) string { + got, err := ioutil.ReadAll(res.Body) + if err != nil { + t.Fatal(err) + } + return string(got) +} + +func TestHome(t *testing.T) { + ts := httptest.NewServer(NewMux()) + defer ts.Close() + + res, _ := http.Get(ts.URL + "/") + b := stringbody(res, t) + + if strings.Index(b, "home index") == -1 { + t.Errorf("wrong template %+v", b) + } +} + +func TestAccountIndex(t *testing.T) { + ts := httptest.NewServer(NewMux()) + defer ts.Close() + + res, _ := http.Get(ts.URL + "/accounts") + b := stringbody(res, t) + + if strings.Index(b, "accounts index") == -1 { + t.Errorf("wrong template %+v", b) + } + + if strings.Index(b, "Main Layout") == -1 { + t.Errorf("wrong layout %+v", b) + } +} + +func TestPopupEdit(t *testing.T) { + ts := httptest.NewServer(NewMux()) + defer ts.Close() + + res, _ := http.Get(ts.URL + "/accounts/1/popup_edit") + b := stringbody(res, t) + if strings.Index(b, "Popup Layout") == -1 { + t.Errorf("wrong layout %+v", b) + } +} diff --git a/templates/accounts/_account.html b/templates/accounts/_account.html new file mode 100644 index 0000000..6180ca1 --- /dev/null +++ b/templates/accounts/_account.html @@ -0,0 +1,3 @@ +{{define "accounts/account"}} + _account +{{end}} diff --git a/templates/accounts/edit.html b/templates/accounts/edit.html new file mode 100644 index 0000000..5df405c --- /dev/null +++ b/templates/accounts/edit.html @@ -0,0 +1,3 @@ +{{define "accounts/edit"}} + accounts edit +{{end}} diff --git a/templates/accounts/index.html b/templates/accounts/index.html new file mode 100644 index 0000000..10584fc --- /dev/null +++ b/templates/accounts/index.html @@ -0,0 +1,3 @@ +{{define "accounts/index"}} + accounts index +{{end}} diff --git a/templates/home/_product.html b/templates/home/_product.html new file mode 100644 index 0000000..c1d2551 --- /dev/null +++ b/templates/home/_product.html @@ -0,0 +1,3 @@ +{{define "home/product"}} + home product +{{end}} diff --git a/templates/home/index.html b/templates/home/index.html new file mode 100644 index 0000000..a980fb6 --- /dev/null +++ b/templates/home/index.html @@ -0,0 +1,3 @@ +{{define "home/index"}} + home index +{{end}} diff --git a/templates/layout/main.html b/templates/layout/main.html new file mode 100644 index 0000000..bb40c96 --- /dev/null +++ b/templates/layout/main.html @@ -0,0 +1,13 @@ +{{define "mainHeader"}} + + + Main Layout + + +{{end}} + + +{{define "mainFooter"}} + + +{{end}} diff --git a/templates/layout/simple.html b/templates/layout/simple.html new file mode 100644 index 0000000..b5d3f48 --- /dev/null +++ b/templates/layout/simple.html @@ -0,0 +1,13 @@ +{{define "simpleHeader"}} + + + Popup Layout + + +{{end}} + + +{{define "simpleFooter"}} + + +{{end}}