Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 9550e34
Showing
14 changed files
with
205 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package accounts | ||
|
||
type Account struct { | ||
} | ||
|
||
func NewAccount() (a *Account) { | ||
return &Account{} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package gowebapp | ||
|
||
import ( | ||
"net/http" | ||
) | ||
|
||
func main() { | ||
http.ListenAndServe("localhost:5000", NewMux()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{{define "accounts/account"}} | ||
_account | ||
{{end}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{{define "accounts/edit"}} | ||
accounts edit | ||
{{end}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{{define "accounts/index"}} | ||
accounts index | ||
{{end}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{{define "home/product"}} | ||
home product | ||
{{end}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{{define "home/index"}} | ||
home index | ||
{{end}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{{define "mainHeader"}} | ||
<html> | ||
<head> | ||
<title>Main Layout</title> | ||
</head> | ||
<body> | ||
{{end}} | ||
|
||
|
||
{{define "mainFooter"}} | ||
</body> | ||
</html> | ||
{{end}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{{define "simpleHeader"}} | ||
<html> | ||
<head> | ||
<title>Popup Layout</title> | ||
</head> | ||
<body> | ||
{{end}} | ||
|
||
|
||
{{define "simpleFooter"}} | ||
</body> | ||
</html> | ||
{{end}} |