Skip to content

Commit

Permalink
04-Web-Form get web login form
Browse files Browse the repository at this point in the history
  • Loading branch information
bonfy committed Sep 10, 2018
1 parent ed49bb8 commit 3e42c84
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 4 deletions.
11 changes: 10 additions & 1 deletion controller/home.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,19 @@ type home struct{}

func (h home) registerRoutes() {
http.HandleFunc("/", indexHandler)
http.HandleFunc("/login", loginHandler)
}

func indexHandler(w http.ResponseWriter, r *http.Request) {
tpName := "index.html"
vop := vm.IndexViewModelOp{}
v := vop.GetVM()
templates["index.html"].Execute(w, &v)
templates[tpName].Execute(w, &v)
}

func loginHandler(w http.ResponseWriter, r *http.Request) {
tpName := "login.html"
vop := vm.LoginViewModelOp{}
v := vop.GetVM()
templates[tpName].Execute(w, &v)
}
7 changes: 6 additions & 1 deletion templates/_base.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@
{{end}}
</head>
<body>
<div>Blog: <a href="/">Home</a></div>
<div>
Blog:
<a href="/">Home</a>
<a href="/login">Login</a>
</div>

{{template "content" .}}
</body>
</html>
8 changes: 8 additions & 0 deletions templates/content/login.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{{define "content"}}
<h1>Login</h1>
<form action="/login" method="post" name="login">
<p><input type="text" name="username" value="" placeholder="Username or Email"></p>
<p><input type="password" name="password" value="" placeholder="Password"></p>
<p><input type="submit" name="submit" value="Login"></p>
</form>
{{end}}
4 changes: 2 additions & 2 deletions vm/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ func (IndexViewModelOp) GetVM() IndexViewModel {
model.Post{User: u2, Body: "The Avengers movie was so cool!"},
}

vm := IndexViewModel{BaseViewModel{Title: "Homepage"}, u1, posts}
return vm
v := IndexViewModel{BaseViewModel{Title: "Homepage"}, u1, posts}
return v
}
16 changes: 16 additions & 0 deletions vm/login.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package vm

// LoginViewModel struct
type LoginViewModel struct {
BaseViewModel
}

// LoginViewModelOp strutc
type LoginViewModelOp struct{}

// GetVM func
func (LoginViewModelOp) GetVM() LoginViewModel {
v := LoginViewModel{}
v.SetTitle("Login")
return v
}

0 comments on commit 3e42c84

Please sign in to comment.