-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
71 lines (56 loc) · 1.43 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package main
import (
"fmt"
"html/template"
"log"
"net/http"
"os"
"labix.org/v2/mgo"
)
const (
url = "mongodb://localhost"
database = "blog"
sessions = "sessions"
users = "users"
tmplDir = "tmpl/"
)
var (
trace = log.New(os.Stdout, "trace:", log.Lshortfile)
session *mgo.Session
templates = template.Must(template.ParseFiles(tmplDir+"signup.html", tmplDir+"login.html"))
)
func renderTemplate(w http.ResponseWriter, tmpl string, u *User) {
if err := templates.ExecuteTemplate(w, tmpl, u); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func root(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "This is a place holder for the blog")
trace.Println(r, "\n")
}
func signup(w http.ResponseWriter, r *http.Request) {
var user = new(User)
user.Id = r.FormValue("username")
user.Password = []byte(r.FormValue("password"))
user.Email = r.FormValue("email")
renderTemplate(w, "signup.html", user)
trace.Println(r, "\n")
}
func login(w http.ResponseWriter, r *http.Request) {
var user = new(User)
user.Id = r.FormValue("username")
user.Password = []byte(r.FormValue("password"))
renderTemplate(w, "login.html", user)
trace.Println(r, "\n")
}
func main() {
session, err := mgo.Dial(url)
if err != nil {
log.Fatalln(err)
}
defer session.Close()
http.HandleFunc("/", root)
http.HandleFunc("/signup", signup)
http.HandleFunc("/login", login)
http.ListenAndServe(":8080", nil)
}