Code cleanup

This commit is contained in:
Maximilian 2022-12-04 18:04:24 -06:00
parent ed2d0063f0
commit e8e42f3724
5 changed files with 25 additions and 21 deletions

View File

@ -13,7 +13,7 @@ type GetController struct {
App *app.App
}
func (getController *GetController) ShowHome(w http.ResponseWriter, r *http.Request) {
func (getController *GetController) ShowHome(w http.ResponseWriter, _ *http.Request) {
type dataStruct struct {
Test string
}
@ -27,17 +27,17 @@ func (getController *GetController) ShowHome(w http.ResponseWriter, r *http.Requ
func (getController *GetController) ShowRegister(w http.ResponseWriter, r *http.Request) {
type dataStruct struct {
csrf_token string
csrfToken string
}
// Create csrf token
csrf_token, err := security.GenerateCsrfToken(w, r)
csrfToken, err := security.GenerateCsrfToken(w, r)
if err != nil {
return
}
data := dataStruct{
csrf_token: csrf_token,
csrfToken: csrfToken,
}
templating.RenderTemplate(getController.App, w, "templates/pages/register.html", data)
@ -45,17 +45,17 @@ func (getController *GetController) ShowRegister(w http.ResponseWriter, r *http.
func (getController *GetController) ShowLogin(w http.ResponseWriter, r *http.Request) {
type dataStruct struct {
csrf_token string
csrfToken string
}
// Create csrf token
csrf_token, err := security.GenerateCsrfToken(w, r)
csrfToken, err := security.GenerateCsrfToken(w, r)
if err != nil {
return
}
data := dataStruct{
csrf_token: csrf_token,
csrfToken: csrfToken,
}
templating.RenderTemplate(getController.App, w, "templates/pages/login.html", data)

24
main.go
View File

@ -8,34 +8,38 @@ import (
"log"
"net/http"
"os"
"time"
)
func main() {
// Create instance of App
app := app.App{}
appLoaded := app.App{}
// Load config file to application
app.Config = config.LoadConfig()
appLoaded.Config = config.LoadConfig()
// Create logs directory if it doesn't exist
if _, err := os.Stat("logs"); os.IsNotExist(err) {
os.Mkdir("logs", 0755)
err := os.Mkdir("logs", 0755)
if err != nil {
panic("Failed to create log directory")
}
}
// Create log file and set output
//file, _ := os.Create("logs/log-" + time.Now().String() + ".log")
//log.SetOutput(file)
file, _ := os.Create("logs/log-" + time.Now().String() + ".log")
log.SetOutput(file)
// Connect to database
app.Db = database.ConnectDB(&app)
appLoaded.Db = database.ConnectDB(&appLoaded)
// Define Routes
routes.GetRoutes(&app)
routes.PostRoutes(&app)
routes.GetRoutes(&appLoaded)
routes.PostRoutes(&appLoaded)
// Start server
log.Println("Starting server and listening on " + app.Config.Listen.Ip + ":" + app.Config.Listen.Port)
err := http.ListenAndServe(app.Config.Listen.Ip+":"+app.Config.Listen.Port, nil)
log.Println("Starting server and listening on " + appLoaded.Config.Listen.Ip + ":" + appLoaded.Config.Listen.Port)
err := http.ListenAndServe(appLoaded.Config.Listen.Ip+":"+appLoaded.Config.Listen.Port, nil)
if err != nil {
log.Println(err)
return

View File

@ -9,7 +9,7 @@ import (
)
// GenerateCsrfToken generates a csrf token and assigns it to a cookie for double submit cookie csrf protection
func GenerateCsrfToken(w http.ResponseWriter, r *http.Request) (string, error) {
func GenerateCsrfToken(w http.ResponseWriter, _ *http.Request) (string, error) {
// Generate random 64 character string (alpha-numeric)
buff := make([]byte, int(math.Ceil(float64(64)/2)))
_, err := rand.Read(buff)

View File

@ -2,7 +2,7 @@
{{ define "content" }}
<form action="/login-handle" method="post">
<input type="hidden" name="csrf_token" value="{{ .csrfToken }}">
<input name="csrf_token" type="hidden" value="{{ .csrfToken }}">
<label for="username">Username:</label><br>
<input id="username" name="username" type="text" value="John"><br><br>

View File

@ -2,7 +2,7 @@
{{ define "content" }}
<form action="/register-handle" method="post">
<input type="hidden" name="csrf_token" value="{{ .csrfToken }}">
<input name="csrf_token" type="hidden" value="{{ .csrfToken }}">
<label for="username">Username:</label><br>
<input id="username" name="username" type="text" value="John"><br><br>