Skip to content

Commit

Permalink
04-Web-Form better structure
Browse files Browse the repository at this point in the history
  • Loading branch information
bonfy committed Sep 10, 2018
1 parent e5fd3ef commit ed49bb8
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 70 deletions.
17 changes: 17 additions & 0 deletions controller/g.go
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()
}
19 changes: 19 additions & 0 deletions controller/home.go
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)
}
42 changes: 42 additions & 0 deletions controller/utils.go
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
}
73 changes: 3 additions & 70 deletions main.go
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)
}
7 changes: 7 additions & 0 deletions model/post.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package model

// Post struct
type Post struct {
User
Body string
}
6 changes: 6 additions & 0 deletions model/user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package model

// User struct
type User struct {
Username string
}
11 changes: 11 additions & 0 deletions vm/g.go
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
}
27 changes: 27 additions & 0 deletions vm/index.go
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
}

0 comments on commit ed49bb8

Please # to comment.