75 lines
1.6 KiB
Go
75 lines
1.6 KiB
Go
package controllers
|
|
|
|
import (
|
|
"GoWeb/app"
|
|
"GoWeb/models"
|
|
"GoWeb/security"
|
|
"GoWeb/templating"
|
|
"net/http"
|
|
)
|
|
|
|
// GetController is a wrapper struct for the App struct
|
|
type GetController struct {
|
|
App *app.App
|
|
}
|
|
|
|
func (getController *GetController) ShowHome(w http.ResponseWriter, _ *http.Request) {
|
|
type dataStruct struct {
|
|
Test string
|
|
}
|
|
|
|
data := dataStruct{
|
|
Test: "Hello World!",
|
|
}
|
|
|
|
templating.RenderTemplate(getController.App, w, "templates/pages/home.html", data)
|
|
}
|
|
|
|
func (getController *GetController) ShowRegister(w http.ResponseWriter, r *http.Request) {
|
|
type dataStruct struct {
|
|
CsrfToken string
|
|
}
|
|
|
|
// Create csrf token
|
|
CsrfToken, err := security.GenerateCsrfToken(w, r)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
data := dataStruct{
|
|
CsrfToken: CsrfToken,
|
|
}
|
|
|
|
templating.RenderTemplate(getController.App, w, "templates/pages/register.html", data)
|
|
}
|
|
|
|
func (getController *GetController) ShowLogin(w http.ResponseWriter, r *http.Request) {
|
|
type dataStruct struct {
|
|
CsrfToken string
|
|
}
|
|
|
|
// Create csrf token
|
|
CsrfToken, err := security.GenerateCsrfToken(w, r)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
data := dataStruct{
|
|
CsrfToken: CsrfToken,
|
|
}
|
|
|
|
templating.RenderTemplate(getController.App, w, "templates/pages/login.html", data)
|
|
}
|
|
|
|
func (getController *GetController) ShowFile(w http.ResponseWriter, r *http.Request) {
|
|
// GET /uploads?name=file.jpg
|
|
// will serve file.jpg
|
|
name := r.URL.Query().Get("name")
|
|
http.ServeFile(w, r, getController.App.Config.Upload.BaseName+name)
|
|
}
|
|
|
|
func (getController *GetController) Logout(w http.ResponseWriter, r *http.Request) {
|
|
models.LogoutUser(getController.App, w, r)
|
|
http.Redirect(w, r, "/", http.StatusFound)
|
|
}
|