Skip to content
Permalink
Browse files

04-Web-Form better structure

  • Loading branch information...
bonfy committed Sep 10, 2018
1 parent e5fd3ef commit ed49bb8644f4aeb3da4d7b7c8ea504ed22d9a653
Showing with 132 additions and 70 deletions.
  1. +17 −0 controller/g.go
  2. +19 −0 controller/home.go
  3. +42 −0 controller/utils.go
  4. +3 −70 main.go
  5. +7 −0 model/post.go
  6. +6 −0 model/user.go
  7. +11 −0 vm/g.go
  8. +27 −0 vm/index.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()
}
@@ -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)
}
@@ -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 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)
}
@@ -0,0 +1,7 @@
package model

// Post struct
type Post struct {
User
Body string
}
@@ -0,0 +1,6 @@
package model

// User struct
type User struct {
Username string
}
11 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
}
@@ -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 sign in to comment.
You can’t perform that action at this time.