alfheim-website/handlers.go

202 lines
5.2 KiB
Go

package main
import "fmt"
import "log"
import "net/http"
import "html/template"
//import "strconv"
import "strings"
import "unicode/utf8"
import "alfheimgame/models"
import "errors"
import "runtime/debug"
type templatedata struct {
Formerrors map[string]string
}
func favicon(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "favicon.ico")
}
func home(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
http.NotFound(w, r);
return
}
text, err := template.ParseFiles("base.html", "index.html")
if err != nil {
http.Error(w, "Internal Server Error", 500)
log.Fatal(err)
}
switch r.Method {
case http.MethodGet:
err = text.Execute(w, true)
if err != nil {
log.Fatal(err)
http.Error(w, "Internal Server Error", 500)
}
case http.MethodPost:
data := LoginData{username: r.FormValue("username"), password: r.FormValue("password")}
fmt.Println(data)
err = text.Execute(w, false)
if err != nil {
log.Fatal(err)
http.Error(w, "Internal Server Error", 500)
}
}
}
func login(w http.ResponseWriter, r *http.Request) {
text, err := template.ParseFiles("base.html", "login/index.html")
if err != nil {
http.Error(w, "Internal Server Error", 500)
log.Fatal(err)
}
switch r.Method {
case http.MethodGet:
text.Execute(w, templatedata{})
if err != nil {
log.Fatal(err)
http.Error(w, "Internal Server Error", 500)
}
case http.MethodPost:
session, _ := store.Get(r, "id");
logindata := LoginData{username: r.FormValue("username"), password: r.FormValue("password")}
errors := make(map[string]string)
if strings.TrimSpace(logindata.username) == "" {
errors["username"] = "This field cannot be blank"
} else if utf8.RuneCountInString(logindata.username) > 20 {
errors["username"] = "This field is too long (the maximum is 20 characters)"
}
if strings.TrimSpace(logindata.password) == "" {
errors["password"] = "This field cannot be blank"
} else if utf8.RuneCountInString(logindata.password) < 8 {
errors["password"] = "This field is too short (the minimum is 8 characters)"
}
if len(errors) > 0 {
text.Execute(w, templatedata{errors})
if err != nil {
log.Fatal(err)
http.Error(w, "Internal Server Error", 500)
}
}
id, _ := users.Authenticate(logindata.username, logindata.password)
if id > 0 {
session.Values["id"] = id
session.Save(r, w)
http.Redirect(w, r, "/account", http.StatusSeeOther)
}
}
}
func register(w http.ResponseWriter, r *http.Request) {
text, err := template.ParseFiles("base.html", "register/index.html")
if err != nil {
http.Error(w, "Internal Server Error", 500)
log.Fatal(err)
}
switch r.Method {
case http.MethodGet:
text.Execute(w, templatedata{})
if err != nil {
log.Fatal(err)
http.Error(w, "Internal Server Error", 500)
}
case http.MethodPost:
account := models.Account{Username: r.FormValue("username"), Password: []byte(r.FormValue("password")), Firstname: r.FormValue("firstname"), Lastname: r.FormValue("lastname"), Email: r.FormValue("email")}
errors := make(map[string]string)
if strings.TrimSpace(account.Username) == "" {
errors["username"] = "This field cannot be blank"
} else if utf8.RuneCountInString(account.Username) > 20 {
errors["username"] = "This field is too long (the maximum is 20 characters)"
}
if strings.TrimSpace(string(account.Password)) == "" {
errors["password"] = "This field cannot be blank"
} else if utf8.RuneCountInString(string(account.Password)) < 8 {
errors["password"] = "This field is too short (the minimum is 8 characters)"
}
if len(errors) > 0 {
text.Execute(w, templatedata{errors})
if err != nil {
log.Fatal(err)
http.Error(w, "Internal Server Error", 500)
}
}
fmt.Println(account)
users.Insert(account.Username, string(account.Password), account.Firstname, account.Lastname, account.Email)
http.Redirect(w, r, "/login", http.StatusSeeOther)
}
}
func account(w http.ResponseWriter, r *http.Request) {
session, _ := store.Get(r, "id")
//id, err := strconv.Atoi(r.URL.Query().Get("id"))
//if err != nil || id < 1 {
// http.NotFound(w, r)
// return
//}
//account, err := users.Get_account(int32(id));
//if err != nil {
// log.Fatal(err);
//}
id, ok := session.Values["id"].(int32)
if !ok {
trace := fmt.Sprintf("%s\n%s", errors.New("type assertion to int32 failed").Error(), debug.Stack())
log.Println(trace)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
} else {
account, err := users.Get_account(id)
fmt.Println(account)
text, err := template.ParseFiles("base.html", "account/index.html")
if err != nil {
http.Error(w, "Internal Server Error", 500)
log.Fatal(err)
}
switch r.Method {
case http.MethodGet:
text.Execute(w, account)
if err != nil {
log.Fatal(err)
http.Error(w, "Internal Server Error", 500)
}
}
//case http.MethodPost:
// data := LoginData{username: r.FormValue("username"), password: r.FormValue("password")}
// fmt.Println(data)
// text.Execute(w, false)
// if err != nil {
// log.Fatal(err)
// http.Error(w, "Internal Server Error", 500)
// }
}
}