6 Commits

Author SHA1 Message Date
Maximilian
a2077131a7 Update experimental crypto library 2023-02-08 19:55:10 -06:00
Maximilian
edccb95be3 Remove unnecessary assignment 2023-02-08 19:47:08 -06:00
Maximilian
9e4216301d Move models to its own package 2023-02-08 19:39:53 -06:00
Maximilian
f2f2187872 Fix issue with logging to a file. Only log by day. 2023-02-05 13:01:38 -06:00
Maximilian
112a549162 Update to go 1.20 2023-02-05 12:59:37 -06:00
Maximilian
1360f93ac3 Embed static and templates folder when compiling, RenderTemplate now renders and serves from the embedded filesystem 2023-02-05 12:46:47 -06:00
8 changed files with 50 additions and 14 deletions

View File

@@ -3,10 +3,12 @@ package app
import (
"GoWeb/config"
"database/sql"
"embed"
)
// App contains and supplies available configurations and connections
type App struct {
Config config.Configuration
Db *sql.DB
Config config.Configuration // Configuration file
Db *sql.DB // Database connection
Res *embed.FS // Resources from the embedded filesystem
}

View File

@@ -2,7 +2,7 @@ package controllers
import (
"GoWeb/app"
"GoWeb/database/models"
"GoWeb/models"
"GoWeb/security"
"GoWeb/templating"
"net/http"

View File

@@ -2,7 +2,7 @@ package controllers
import (
"GoWeb/app"
"GoWeb/database/models"
"GoWeb/models"
"GoWeb/security"
"log"
"net/http"

4
go.mod
View File

@@ -1,8 +1,8 @@
module GoWeb
go 1.19
go 1.20
require (
github.com/lib/pq v1.10.7
golang.org/x/crypto v0.1.0
golang.org/x/crypto v0.6.0
)

4
go.sum
View File

@@ -1,4 +1,4 @@
github.com/lib/pq v1.10.7 h1:p7ZhMD+KsSRozJr34udlUrhboJwWAgCg34+/ZZNvZZw=
github.com/lib/pq v1.10.7/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
golang.org/x/crypto v0.1.0 h1:MDRAIl0xIo9Io2xV565hzXHw3zVseKrJKodhohM5CjU=
golang.org/x/crypto v0.1.0/go.mod h1:RecgLatLF4+eUMCP1PoPZQb+cVrJcOPbHkTkbkB9sbw=
golang.org/x/crypto v0.6.0 h1:qfktjS5LUO+fFKeJXZ+ikTRijMmljikvG68fpMMruSc=
golang.org/x/crypto v0.6.0/go.mod h1:OFC/31mSvZgRz0V1QTNCzfAI1aIRzbiufJtkMIlEp58=

11
main.go
View File

@@ -5,12 +5,16 @@ import (
"GoWeb/config"
"GoWeb/database"
"GoWeb/routes"
"embed"
"log"
"net/http"
"os"
"time"
)
//go:embed templates static
var res embed.FS
func main() {
// Create instance of App
appLoaded := app.App{}
@@ -18,6 +22,9 @@ func main() {
// Load config file to application
appLoaded.Config = config.LoadConfig()
// Load templates
appLoaded.Res = &res
// Create logs directory if it doesn't exist
if _, err := os.Stat("logs"); os.IsNotExist(err) {
err := os.Mkdir("logs", 0755)
@@ -27,7 +34,7 @@ func main() {
}
// Create log file and set output
file, _ := os.Create("logs/log-" + time.Now().String() + ".log")
file, err := os.OpenFile("logs/"+time.Now().Format("2006-01-02")+".log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
log.SetOutput(file)
// Connect to database
@@ -39,7 +46,7 @@ func main() {
// Start server
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)
err = http.ListenAndServe(appLoaded.Config.Listen.Ip+":"+appLoaded.Config.Listen.Port, nil)
if err != nil {
log.Println(err)
return

View File

@@ -148,8 +148,7 @@ func createSessionCookie(app *app.App, w http.ResponseWriter, username string) (
}
// Store token in auth_token column of the users table
sqlStatement := "UPDATE users SET auth_token = $1 WHERE username = $2"
_, err = app.Db.Exec(sqlStatement, token, username)
_, err = app.Db.Exec("UPDATE users SET auth_token = $1 WHERE username = $2", token, username)
if err != nil {
log.Println("Error setting auth_token column in users table")
log.Println(err)

View File

@@ -7,11 +7,39 @@ import (
"net/http"
)
// RenderTemplate renders and serves a template from the embedded filesystem optionally with given data
func RenderTemplate(app *app.App, w http.ResponseWriter, contentPath string, data any) {
templatePath := app.Config.Template.BaseName
t, _ := template.ParseFiles(templatePath, contentPath)
err := t.Execute(w, data)
templateContent, err := app.Res.ReadFile(templatePath)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), 500)
return
}
t, err := template.New(templatePath).Parse(string(templateContent))
if err != nil {
log.Println(err)
http.Error(w, err.Error(), 500)
return
}
content, err := app.Res.ReadFile(contentPath)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), 500)
return
}
t, err = t.Parse(string(content))
if err != nil {
log.Println(err)
http.Error(w, err.Error(), 500)
return
}
err = t.Execute(w, data)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), 500)