Permalink
Browse files

add login form handler and template

  • Loading branch information...
zeebo committed Sep 5, 2012
1 parent 51c7664 commit 50d85a72495ac676db9301df45f84ca1cbf96702
Showing with 46 additions and 47 deletions.
  1. +3 −17 handlers.go
  2. +3 −30 main.go
  3. +28 −0 template.go
  4. +2 −0 templates/index.html
  5. +10 −0 templates/login.html
@@ -15,8 +15,7 @@ func hello(w http.ResponseWriter, req *http.Request, ctx *Context) (err error) {
}
//execute the template
err = index.Execute(w, entries)
return
return T("index.html").Execute(w, entries)

This comment has been minimized.

@zeebo

zeebo Sep 5, 2012

Owner

Here we use the new T function for compiling the index template on the fly.

}
func sign(w http.ResponseWriter, req *http.Request, ctx *Context) (err error) {
@@ -40,19 +39,6 @@ func sign(w http.ResponseWriter, req *http.Request, ctx *Context) (err error) {
return
}
func login(w http.ResponseWriter, req *http.Request, ctx *Context) (err error) {

This comment has been minimized.

@zeebo

zeebo Sep 5, 2012

Owner

Lets just remove our login function for now. It wasn't quite right!

//grab the username and password from the form
username, password := req.FormValue("username"), req.FormValue("password")
//log in the user
user, err := Login(ctx, username, password)
//what to do now? if there was an error we want to present the form again
//with some error message.
//where do we store the user if the login was valid?
//answer: sessions!
_ = user
return
func loginForm(w http.ResponseWriter, req *http.Request, ctx *Context) (err error) {
return T("login.html").Execute(w, nil)
}
33 main.go
@@ -3,32 +3,9 @@ package main
import (
"code.google.com/p/gorilla/pat"
"fmt"
"html/template"
"labix.org/v2/mgo"
"net/http"
"net/url"
"os"
"path/filepath"
)
var funcs = template.FuncMap{
"reverse": reverse,
}
func parseTemplate(files ...string) *template.Template {
//create a new template named after the first file in the list and add
//the function map to it
name := filepath.Base(files[0])
t := template.New(name).Funcs(funcs)
//parse the files into the template and panic on errors
t = template.Must(t.ParseFiles(files...))
return t
}
var index = parseTemplate(
"templates/_base.html",
"templates/index.html",
)
func reverse(name string, things ...interface{}) string {
@@ -51,18 +28,14 @@ var router *pat.Router
func main() {
var err error
u := os.Getenv("DATABASE_URL")
parsed, err := url.Parse(u)
if err != nil {
panic(err)
}
database = parsed.Path[1:]
session, err = mgo.Dial(u)
session, err = mgo.Dial(os.Getenv("DATABASE_URL"))
if err != nil {
panic(err)
}
database = session.DB("").Name

This comment has been minimized.

@zeebo

zeebo Sep 5, 2012

Owner

Support for this was added in a recent version of mgo. When passing an empty database name, mgo will use the database specified in the dial command. This lets use grab the name used without having to parse the url manually.

router = pat.New()
router.Add("GET", "/login", handler(loginForm)).Name("login")
router.Add("GET", "/", handler(hello)).Name("index")
router.Add("POST", "/sign", handler(sign)).Name("sign")
@@ -0,0 +1,28 @@
package main
import (
"html/template"
"path/filepath"
)
var cachedTemplates = map[string]*template.Template{}
var funcs = template.FuncMap{
"reverse": reverse,
}
func T(name string) *template.Template {
if t, ok := cachedTemplates[name]; ok {
return t
}
t := template.New("_base.html").Funcs(funcs)
t = template.Must(t.ParseFiles(
"templates/_base.html",
filepath.Join("templates", name),
))
cachedTemplates[name] = t
return t
}

This comment has been minimized.

@zeebo

zeebo Sep 5, 2012

Owner

Homework:

Make T always recompile the template if the DEBUG environment variable is set so we don't have to restart the website for template changes.

@@ -14,6 +14,8 @@ <h1>Guestbook</h1>
{{ end }}
</ul>
<hr>
<a href="{{ reverse "login" }}">login</a>

This comment has been minimized.

@zeebo

zeebo Sep 5, 2012

Owner

I never said I was a good web designer :)

<hr>
<form action="{{ reverse "sign" }}" method="POST">
@@ -0,0 +1,10 @@
{{ define "title" }}Guestbook - Login{{ end }}
{{ define "content" }}
<h1>Login</h1>
<form action="{{ reverse "login" }}" method="POST">
<p>Username: <input type="text" name="username"></p>
<p>Password: <input type="password" name="password"></p>
<p><button>Login</button></p>
</form>
{{ end }}

0 comments on commit 50d85a7

Please sign in to comment.