This repository has been archived by the owner on Mar 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
88 lines (79 loc) · 2.5 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package main
import (
"html/template"
"net/http"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/sqlite"
)
// User is a person on the site.
type User struct {
gorm.Model
Confirmed bool `gorm:"default:'true'"` // reverse when confirmation flow
FirstName string `gorm:"size:64;index"`
LastName string `gorm:"size:64;index"`
Email string `gorm:"size:64;unique;index"`
PasswordHash string `gorm:"size:128"`
}
var templates = template.Must(template.ParseFiles("templates/index.html", "templates/about.html", "templates/register.html", "templates/login.html"))
func renderTemplate(w http.ResponseWriter, tmpl string, data interface{}) {
err := templates.ExecuteTemplate(w, tmpl+".html", data)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func main() {
db, err := gorm.Open("sqlite3", "/tmp/gorm.db")
if err != nil {
panic("failed to connect database")
}
defer db.Close()
db.AutoMigrate(&User{})
type Data struct {
LoggedIn bool
}
data := Data{false}
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
renderTemplate(w, "index", data)
})
http.HandleFunc("/about", func(w http.ResponseWriter, r *http.Request) {
tmpl := template.Must(template.ParseFiles("templates/about.html", "templates/base.html"))
tmpl.Execute(w, data)
})
http.HandleFunc("/account/register", func(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
renderTemplate(w, "register", data)
} else {
r.ParseForm()
FirstName := r.Form["first_name"][0]
LastName := r.Form["last_name"][0]
Email := r.Form["email"][0]
Password := r.Form["password"][0]
user := User{FirstName: FirstName, LastName: LastName, Email: Email, PasswordHash: Password}
db.Create(&user)
http.Redirect(w, r, "/", http.StatusFound)
}
})
http.HandleFunc("/account/login", func(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
renderTemplate(w, "login", data)
} else {
r.ParseForm()
Email := r.Form["email"][0]
FormPassword := r.Form["password"][0]
var user User
db.First(&user, "Email = ?", Email)
if user.PasswordHash == FormPassword {
data.LoggedIn = true
http.Redirect(w, r, "/", http.StatusFound)
} else {
renderTemplate(w, "login", data)
}
}
})
http.HandleFunc("/account/logout", func(w http.ResponseWriter, r *http.Request) {
data.LoggedIn = false
http.Redirect(w, r, "/", http.StatusFound)
})
http.Handle("/static/", http.FileServer(http.Dir(".")))
http.ListenAndServe(":8080", nil)
}