Skip to content

Commit

Permalink
add login form handler and template
Browse files Browse the repository at this point in the history
  • Loading branch information
zeebo committed Sep 5, 2012
1 parent 51c7664 commit 50d85a7
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 47 deletions.
20 changes: 3 additions & 17 deletions handlers.go
Expand Up @@ -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.

Copy link
@zeebo

zeebo Sep 5, 2012

Author 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) {
Expand All @@ -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.

Copy link
@zeebo

zeebo Sep 5, 2012

Author 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 changes: 3 additions & 30 deletions main.go
Expand Up @@ -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 {
Expand All @@ -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.

Copy link
@zeebo

zeebo Sep 5, 2012

Author 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")

Expand Down
28 changes: 28 additions & 0 deletions template.go
@@ -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.

Copy link
@zeebo

zeebo Sep 5, 2012

Author 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.

2 changes: 2 additions & 0 deletions templates/index.html
Expand Up @@ -14,6 +14,8 @@ <h1>Guestbook</h1>
{{ end }}
</ul>

<hr>
<a href="{{ reverse "login" }}">login</a>

This comment has been minimized.

Copy link
@zeebo

zeebo Sep 5, 2012

Author Owner

I never said I was a good web designer :)

<hr>

<form action="{{ reverse "sign" }}" method="POST">
Expand Down
10 changes: 10 additions & 0 deletions templates/login.html
@@ -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.