Skip to content

Commit

Permalink
initialize
Browse files Browse the repository at this point in the history
  • Loading branch information
sunfmin committed Mar 9, 2012
0 parents commit 9550e34
Show file tree
Hide file tree
Showing 14 changed files with 205 additions and 0 deletions.
8 changes: 8 additions & 0 deletions accounts/account.go
@@ -0,0 +1,8 @@
package accounts

type Account struct {
}

func NewAccount() (a *Account) {
return &Account{}
}
14 changes: 14 additions & 0 deletions 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)
}
10 changes: 10 additions & 0 deletions 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)
}
48 changes: 48 additions & 0 deletions 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)
}

}
9 changes: 9 additions & 0 deletions main.go
@@ -0,0 +1,9 @@
package gowebapp

import (
"net/http"
)

func main() {
http.ListenAndServe("localhost:5000", NewMux())
}
19 changes: 19 additions & 0 deletions 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
}
56 changes: 56 additions & 0 deletions 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)
}
}
3 changes: 3 additions & 0 deletions templates/accounts/_account.html
@@ -0,0 +1,3 @@
{{define "accounts/account"}}
_account
{{end}}
3 changes: 3 additions & 0 deletions templates/accounts/edit.html
@@ -0,0 +1,3 @@
{{define "accounts/edit"}}
accounts edit
{{end}}
3 changes: 3 additions & 0 deletions templates/accounts/index.html
@@ -0,0 +1,3 @@
{{define "accounts/index"}}
accounts index
{{end}}
3 changes: 3 additions & 0 deletions templates/home/_product.html
@@ -0,0 +1,3 @@
{{define "home/product"}}
home product
{{end}}
3 changes: 3 additions & 0 deletions templates/home/index.html
@@ -0,0 +1,3 @@
{{define "home/index"}}
home index
{{end}}
13 changes: 13 additions & 0 deletions templates/layout/main.html
@@ -0,0 +1,13 @@
{{define "mainHeader"}}
<html>
<head>
<title>Main Layout</title>
</head>
<body>
{{end}}


{{define "mainFooter"}}
</body>
</html>
{{end}}
13 changes: 13 additions & 0 deletions templates/layout/simple.html
@@ -0,0 +1,13 @@
{{define "simpleHeader"}}
<html>
<head>
<title>Popup Layout</title>
</head>
<body>
{{end}}


{{define "simpleFooter"}}
</body>
</html>
{{end}}

0 comments on commit 9550e34

Please sign in to comment.