-
Notifications
You must be signed in to change notification settings - Fork 11
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
Showing
8 changed files
with
132 additions
and
70 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,17 @@ | ||
package controller | ||
|
||
import "html/template" | ||
|
||
var ( | ||
homeController home | ||
templates map[string]*template.Template | ||
) | ||
|
||
func init() { | ||
templates = PopulateTemplates() | ||
} | ||
|
||
// Startup func | ||
func Startup() { | ||
homeController.registerRoutes() | ||
} |
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 controller | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/bonfy/go-mega-code/vm" | ||
) | ||
|
||
type home struct{} | ||
|
||
func (h home) registerRoutes() { | ||
http.HandleFunc("/", indexHandler) | ||
} | ||
|
||
func indexHandler(w http.ResponseWriter, r *http.Request) { | ||
vop := vm.IndexViewModelOp{} | ||
v := vop.GetVM() | ||
templates["index.html"].Execute(w, &v) | ||
} |
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,42 @@ | ||
package controller | ||
|
||
import ( | ||
"html/template" | ||
"io/ioutil" | ||
"os" | ||
) | ||
|
||
// PopulateTemplates func | ||
// Create map template name to template.Template | ||
func PopulateTemplates() map[string]*template.Template { | ||
const basePath = "templates" | ||
result := make(map[string]*template.Template) | ||
|
||
layout := template.Must(template.ParseFiles(basePath + "/_base.html")) | ||
dir, err := os.Open(basePath + "/content") | ||
if err != nil { | ||
panic("Failed to open template blocks directory: " + err.Error()) | ||
} | ||
fis, err := dir.Readdir(-1) | ||
if err != nil { | ||
panic("Failed to read contents of content directory: " + err.Error()) | ||
} | ||
for _, fi := range fis { | ||
f, err := os.Open(basePath + "/content/" + fi.Name()) | ||
if err != nil { | ||
panic("Failed to open template '" + fi.Name() + "'") | ||
} | ||
content, err := ioutil.ReadAll(f) | ||
if err != nil { | ||
panic("Failed to read content from file '" + fi.Name() + "'") | ||
} | ||
f.Close() | ||
tmpl := template.Must(layout.Clone()) | ||
_, err = tmpl.Parse(string(content)) | ||
if err != nil { | ||
panic("Failed to parse contents of '" + fi.Name() + "' as template") | ||
} | ||
result[fi.Name()] = tmpl | ||
} | ||
return result | ||
} |
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 |
---|---|---|
@@ -1,79 +1,12 @@ | ||
package main | ||
|
||
import ( | ||
"html/template" | ||
"io/ioutil" | ||
"net/http" | ||
"os" | ||
) | ||
|
||
// User struct | ||
type User struct { | ||
Username string | ||
} | ||
|
||
// Post struct | ||
type Post struct { | ||
User | ||
Body string | ||
} | ||
|
||
// IndexViewModel struct | ||
type IndexViewModel struct { | ||
Title string | ||
User | ||
Posts []Post | ||
} | ||
|
||
// PopulateTemplates func | ||
// Create map template name to template.Template | ||
func PopulateTemplates() map[string]*template.Template { | ||
const basePath = "templates" | ||
result := make(map[string]*template.Template) | ||
|
||
layout := template.Must(template.ParseFiles(basePath + "/_base.html")) | ||
dir, err := os.Open(basePath + "/content") | ||
if err != nil { | ||
panic("Failed to open template blocks directory: " + err.Error()) | ||
} | ||
fis, err := dir.Readdir(-1) | ||
if err != nil { | ||
panic("Failed to read contents of content directory: " + err.Error()) | ||
} | ||
for _, fi := range fis { | ||
f, err := os.Open(basePath + "/content/" + fi.Name()) | ||
if err != nil { | ||
panic("Failed to open template '" + fi.Name() + "'") | ||
} | ||
content, err := ioutil.ReadAll(f) | ||
if err != nil { | ||
panic("Failed to read content from file '" + fi.Name() + "'") | ||
} | ||
f.Close() | ||
tmpl := template.Must(layout.Clone()) | ||
_, err = tmpl.Parse(string(content)) | ||
if err != nil { | ||
panic("Failed to parse contents of '" + fi.Name() + "' as template") | ||
} | ||
result[fi.Name()] = tmpl | ||
} | ||
return result | ||
} | ||
"github.com/bonfy/go-mega-code/controller" | ||
) | ||
|
||
func main() { | ||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { | ||
u1 := User{Username: "bonfy"} | ||
u2 := User{Username: "rene"} | ||
|
||
posts := []Post{ | ||
Post{User: u1, Body: "Beautiful day in Portland!"}, | ||
Post{User: u2, Body: "The Avengers movie was so cool!"}, | ||
} | ||
|
||
v := IndexViewModel{Title: "Homepage", User: u1, Posts: posts} | ||
|
||
templates := PopulateTemplates() | ||
templates["index.html"].Execute(w, &v) | ||
}) | ||
controller.Startup() | ||
http.ListenAndServe(":8888", 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,7 @@ | ||
package model | ||
|
||
// Post struct | ||
type Post struct { | ||
User | ||
Body string | ||
} |
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,6 @@ | ||
package model | ||
|
||
// User struct | ||
type User struct { | ||
Username string | ||
} |
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,11 @@ | ||
package vm | ||
|
||
// BaseViewModel struct | ||
type BaseViewModel struct { | ||
Title string | ||
} | ||
|
||
// SetTitle func | ||
func (v *BaseViewModel) SetTitle(title string) { | ||
v.Title = title | ||
} |
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,27 @@ | ||
package vm | ||
|
||
import "github.com/bonfy/go-mega-code/model" | ||
|
||
// IndexViewModel struct | ||
type IndexViewModel struct { | ||
BaseViewModel | ||
model.User | ||
Posts []model.Post | ||
} | ||
|
||
// IndexViewModelOp struct | ||
type IndexViewModelOp struct{} | ||
|
||
// GetVM func | ||
func (IndexViewModelOp) GetVM() IndexViewModel { | ||
u1 := model.User{Username: "bonfy"} | ||
u2 := model.User{Username: "rene"} | ||
|
||
posts := []model.Post{ | ||
model.Post{User: u1, Body: "Beautiful day in Portland!"}, | ||
model.Post{User: u2, Body: "The Avengers movie was so cool!"}, | ||
} | ||
|
||
vm := IndexViewModel{BaseViewModel{Title: "Homepage"}, u1, posts} | ||
return vm | ||
} |