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 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 { type dataStruct struct {
Test string 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) { func (getController *GetController) ShowRegister(w http.ResponseWriter, r *http.Request) {
type dataStruct struct { type dataStruct struct {
csrf_token string csrfToken string
} }
// Create csrf token // Create csrf token
csrf_token, err := security.GenerateCsrfToken(w, r) csrfToken, err := security.GenerateCsrfToken(w, r)
if err != nil { if err != nil {
return return
} }
data := dataStruct{ data := dataStruct{
csrf_token: csrf_token, csrfToken: csrfToken,
} }
templating.RenderTemplate(getController.App, w, "templates/pages/register.html", data) 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) { func (getController *GetController) ShowLogin(w http.ResponseWriter, r *http.Request) {
type dataStruct struct { type dataStruct struct {
csrf_token string csrfToken string
} }
// Create csrf token // Create csrf token
csrf_token, err := security.GenerateCsrfToken(w, r) csrfToken, err := security.GenerateCsrfToken(w, r)
if err != nil { if err != nil {
return return
} }
data := dataStruct{ data := dataStruct{
csrf_token: csrf_token, csrfToken: csrfToken,
} }
templating.RenderTemplate(getController.App, w, "templates/pages/login.html", data) templating.RenderTemplate(getController.App, w, "templates/pages/login.html", data)

24
main.go
View File

@ -8,34 +8,38 @@ import (
"log" "log"
"net/http" "net/http"
"os" "os"
"time"
) )
func main() { func main() {
// Create instance of App // Create instance of App
app := app.App{} appLoaded := app.App{}
// Load config file to application // Load config file to application
app.Config = config.LoadConfig() appLoaded.Config = config.LoadConfig()
// Create logs directory if it doesn't exist // Create logs directory if it doesn't exist
if _, err := os.Stat("logs"); os.IsNotExist(err) { 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 // Create log file and set output
//file, _ := os.Create("logs/log-" + time.Now().String() + ".log") file, _ := os.Create("logs/log-" + time.Now().String() + ".log")
//log.SetOutput(file) log.SetOutput(file)
// Connect to database // Connect to database
app.Db = database.ConnectDB(&app) appLoaded.Db = database.ConnectDB(&appLoaded)
// Define Routes // Define Routes
routes.GetRoutes(&app) routes.GetRoutes(&appLoaded)
routes.PostRoutes(&app) routes.PostRoutes(&appLoaded)
// Start server // Start server
log.Println("Starting server and listening on " + app.Config.Listen.Ip + ":" + app.Config.Listen.Port) log.Println("Starting server and listening on " + appLoaded.Config.Listen.Ip + ":" + appLoaded.Config.Listen.Port)
err := http.ListenAndServe(app.Config.Listen.Ip+":"+app.Config.Listen.Port, nil) err := http.ListenAndServe(appLoaded.Config.Listen.Ip+":"+appLoaded.Config.Listen.Port, nil)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
return 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 // 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) // Generate random 64 character string (alpha-numeric)
buff := make([]byte, int(math.Ceil(float64(64)/2))) buff := make([]byte, int(math.Ceil(float64(64)/2)))
_, err := rand.Read(buff) _, err := rand.Read(buff)

View File

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

View File

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