From ed49bb8644f4aeb3da4d7b7c8ea504ed22d9a653 Mon Sep 17 00:00:00 2001 From: bonfy Date: Mon, 10 Sep 2018 10:03:49 +0800 Subject: [PATCH] 04-Web-Form better structure --- controller/g.go | 17 +++++++++++ controller/home.go | 19 ++++++++++++ controller/utils.go | 42 ++++++++++++++++++++++++++ main.go | 73 ++------------------------------------------- model/post.go | 7 +++++ model/user.go | 6 ++++ vm/g.go | 11 +++++++ vm/index.go | 27 +++++++++++++++++ 8 files changed, 132 insertions(+), 70 deletions(-) create mode 100644 controller/g.go create mode 100644 controller/home.go create mode 100644 controller/utils.go create mode 100644 model/post.go create mode 100644 model/user.go create mode 100644 vm/g.go create mode 100644 vm/index.go diff --git a/controller/g.go b/controller/g.go new file mode 100644 index 0000000..dd98145 --- /dev/null +++ b/controller/g.go @@ -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() +} diff --git a/controller/home.go b/controller/home.go new file mode 100644 index 0000000..e1ccdef --- /dev/null +++ b/controller/home.go @@ -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) +} diff --git a/controller/utils.go b/controller/utils.go new file mode 100644 index 0000000..4e9242a --- /dev/null +++ b/controller/utils.go @@ -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 +} diff --git a/main.go b/main.go index be2411b..19e75d2 100644 --- a/main.go +++ b/main.go @@ -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) } diff --git a/model/post.go b/model/post.go new file mode 100644 index 0000000..3cb49d3 --- /dev/null +++ b/model/post.go @@ -0,0 +1,7 @@ +package model + +// Post struct +type Post struct { + User + Body string +} diff --git a/model/user.go b/model/user.go new file mode 100644 index 0000000..50b394a --- /dev/null +++ b/model/user.go @@ -0,0 +1,6 @@ +package model + +// User struct +type User struct { + Username string +} diff --git a/vm/g.go b/vm/g.go new file mode 100644 index 0000000..d5e9a69 --- /dev/null +++ b/vm/g.go @@ -0,0 +1,11 @@ +package vm + +// BaseViewModel struct +type BaseViewModel struct { + Title string +} + +// SetTitle func +func (v *BaseViewModel) SetTitle(title string) { + v.Title = title +} diff --git a/vm/index.go b/vm/index.go new file mode 100644 index 0000000..41925bf --- /dev/null +++ b/vm/index.go @@ -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 +}