Compare commits

...

2 Commits

Author SHA1 Message Date
Maximilian
ce03926ce6 Rename app->internal 2024-07-10 17:45:04 -05:00
Maximilian
ce85d6b77b Begin refactoring structure 2024-07-01 21:19:48 -05:00
23 changed files with 66 additions and 69 deletions

View File

@ -1,7 +1,7 @@
package database package database
import ( import (
"GoWeb/app" "GoWeb/internal"
"database/sql" "database/sql"
"fmt" "fmt"
_ "github.com/lib/pq" _ "github.com/lib/pq"
@ -9,7 +9,7 @@ import (
) )
// Connect returns a new database connection // Connect returns a new database connection
func Connect(app *app.App) *sql.DB { func Connect(app *app.Deps) *sql.DB {
postgresConfig := fmt.Sprintf("host=%s port=%s user=%s "+ postgresConfig := fmt.Sprintf("host=%s port=%s user=%s "+
"password=%s dbname=%s sslmode=disable", "password=%s dbname=%s sslmode=disable",
app.Config.Db.Ip, app.Config.Db.Port, app.Config.Db.User, app.Config.Db.Password, app.Config.Db.Name) app.Config.Db.Ip, app.Config.Db.Port, app.Config.Db.User, app.Config.Db.Password, app.Config.Db.Name)

View File

@ -1,7 +1,7 @@
package database package database
import ( import (
"GoWeb/app" "GoWeb/internal"
"errors" "errors"
"fmt" "fmt"
"github.com/lib/pq" "github.com/lib/pq"
@ -11,7 +11,7 @@ import (
// Migrate given a dummy object of any type, it will create a table with the same name // Migrate given a dummy object of any type, it will create a table with the same name
// as the type and create columns with the same name as the fields of the object // as the type and create columns with the same name as the fields of the object
func Migrate(app *app.App, anyStruct interface{}) error { func Migrate(app *app.Deps, anyStruct interface{}) error {
valueOfStruct := reflect.ValueOf(anyStruct) valueOfStruct := reflect.ValueOf(anyStruct)
typeOfStruct := valueOfStruct.Type() typeOfStruct := valueOfStruct.Type()
@ -41,7 +41,7 @@ func Migrate(app *app.App, anyStruct interface{}) error {
} }
// createTable creates a table with the given name if it doesn't exist, it is assumed that id will be the primary key // createTable creates a table with the given name if it doesn't exist, it is assumed that id will be the primary key
func createTable(app *app.App, tableName string) error { func createTable(app *app.Deps, tableName string) error {
var tableExists bool var tableExists bool
err := app.Db.QueryRow("SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace WHERE c.relname ~ $1 AND pg_catalog.pg_table_is_visible(c.oid))", "^"+tableName+"$").Scan(&tableExists) err := app.Db.QueryRow("SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace WHERE c.relname ~ $1 AND pg_catalog.pg_table_is_visible(c.oid))", "^"+tableName+"$").Scan(&tableExists)
if err != nil { if err != nil {
@ -67,7 +67,7 @@ func createTable(app *app.App, tableName string) error {
} }
// createColumn creates a column with the given name and type if it doesn't exist // createColumn creates a column with the given name and type if it doesn't exist
func createColumn(app *app.App, tableName, columnName, columnType string) error { func createColumn(app *app.Deps, tableName, columnName, columnType string) error {
var columnExists bool var columnExists bool
err := app.Db.QueryRow("SELECT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name = $1 AND column_name = $2)", tableName, columnName).Scan(&columnExists) err := app.Db.QueryRow("SELECT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name = $1 AND column_name = $2)", tableName, columnName).Scan(&columnExists)
if err != nil { if err != nil {

View File

@ -12,7 +12,7 @@
"HttpPort": "8090" "HttpPort": "8090"
}, },
"Template": { "Template": {
"BaseTemplateName": "templates/base.html", "BaseTemplateName": "internal/frontend/templates/base.html",
"ContentPath": "templates" "ContentPath": "internal/frontend/templates"
} }
} }

View File

@ -1,8 +1,8 @@
package controllers package controllers
import ( import (
"GoWeb/app" "GoWeb/internal"
"GoWeb/models" "GoWeb/internal/models"
"GoWeb/security" "GoWeb/security"
"GoWeb/templating" "GoWeb/templating"
"net/http" "net/http"
@ -10,7 +10,7 @@ import (
// Get is a wrapper struct for the App struct // Get is a wrapper struct for the App struct
type Get struct { type Get struct {
App *app.App App *app.Deps
} }
func (g *Get) ShowHome(w http.ResponseWriter, _ *http.Request) { func (g *Get) ShowHome(w http.ResponseWriter, _ *http.Request) {

View File

@ -1,8 +1,8 @@
package controllers package controllers
import ( import (
"GoWeb/app" "GoWeb/internal"
"GoWeb/models" "GoWeb/internal/models"
"log/slog" "log/slog"
"net/http" "net/http"
"time" "time"
@ -10,7 +10,7 @@ import (
// Post is a wrapper struct for the App struct // Post is a wrapper struct for the App struct
type Post struct { type Post struct {
App *app.App App *app.Deps
} }
func (p *Post) Login(w http.ResponseWriter, r *http.Request) { func (p *Post) Login(w http.ResponseWriter, r *http.Request) {

View File

@ -3,7 +3,7 @@
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<title>SiteName - {{ template "pageTitle" }}</title> <title>SiteName - {{ template "pageTitle" }}</title>
<link href="/static/css/style.css" rel="stylesheet"> <link href="/app/frontend/staticyle.css" rel="stylesheet">
</head> </head>
<body> <body>
{{ template "content" . }} {{ template "content" . }}

View File

@ -6,8 +6,8 @@ import (
"embed" "embed"
) )
// App contains and supplies available configurations and connections // Deps contains and supplies available configurations and connections
type App struct { type Deps struct {
Config config.Configuration // Configuration file Config config.Configuration // Configuration file
Db *sql.DB // Database connection Db *sql.DB // Database connection
Res *embed.FS // Resources from the embedded filesystem Res *embed.FS // Resources from the embedded filesystem

View File

@ -2,6 +2,8 @@ package middleware
import "net/http" import "net/http"
type MiddlewareFunc func(f func(w http.ResponseWriter, r *http.Request)) func(w http.ResponseWriter, r *http.Request)
// ProcessGroup is a wrapper function for the http.HandleFunc function // ProcessGroup is a wrapper function for the http.HandleFunc function
// that takes the function you want to execute (f) and the middleware you want // that takes the function you want to execute (f) and the middleware you want
// to execute (m) this should be used when processing multiple groups of middleware at a time // to execute (m) this should be used when processing multiple groups of middleware at a time

View File

@ -1,13 +1,13 @@
package models package models
import ( import (
"GoWeb/app"
"GoWeb/database" "GoWeb/database"
"GoWeb/internal"
"time" "time"
) )
// RunAllMigrations defines the structs that should be represented in the database // RunAllMigrations defines the structs that should be represented in the database
func RunAllMigrations(app *app.App) error { func RunAllMigrations(app *app.Deps) error {
// Declare new dummy user for reflection // Declare new dummy user for reflection
user := User{ user := User{
Id: 1, // Id is handled automatically, but it is added here to show it will be skipped during column creation Id: 1, // Id is handled automatically, but it is added here to show it will be skipped during column creation

View File

@ -1,7 +1,7 @@
package models package models
import ( import (
"GoWeb/app" "GoWeb/internal"
"crypto/rand" "crypto/rand"
"encoding/hex" "encoding/hex"
"log/slog" "log/slog"
@ -31,7 +31,7 @@ const (
) )
// CreateSession creates a new session for a user // CreateSession creates a new session for a user
func CreateSession(app *app.App, w http.ResponseWriter, userId int64, remember bool) (Session, error) { func CreateSession(app *app.Deps, w http.ResponseWriter, userId int64, remember bool) (Session, error) {
session := Session{} session := Session{}
session.UserId = userId session.UserId = userId
session.AuthToken = generateAuthToken(app) session.AuthToken = generateAuthToken(app)
@ -62,7 +62,7 @@ func CreateSession(app *app.App, w http.ResponseWriter, userId int64, remember b
return session, nil return session, nil
} }
func SessionByAuthToken(app *app.App, authToken string) (Session, error) { func SessionByAuthToken(app *app.Deps, authToken string) (Session, error) {
session := Session{} session := Session{}
err := app.Db.QueryRow(selectSessionByAuthToken, authToken).Scan(&session.Id, &session.UserId, &session.AuthToken, &session.RememberMe, &session.CreatedAt) err := app.Db.QueryRow(selectSessionByAuthToken, authToken).Scan(&session.Id, &session.UserId, &session.AuthToken, &session.RememberMe, &session.CreatedAt)
@ -74,7 +74,7 @@ func SessionByAuthToken(app *app.App, authToken string) (Session, error) {
} }
// generateAuthToken generates a random 64-byte string // generateAuthToken generates a random 64-byte string
func generateAuthToken(app *app.App) string { func generateAuthToken(app *app.Deps) string {
b := make([]byte, 64) b := make([]byte, 64)
_, err := rand.Read(b) _, err := rand.Read(b)
if err != nil { if err != nil {
@ -85,7 +85,7 @@ func generateAuthToken(app *app.App) string {
} }
// createSessionCookie creates a new session cookie // createSessionCookie creates a new session cookie
func createSessionCookie(app *app.App, w http.ResponseWriter, session Session) { func createSessionCookie(app *app.Deps, w http.ResponseWriter, session Session) {
cookie := &http.Cookie{} cookie := &http.Cookie{}
if session.RememberMe { if session.RememberMe {
cookie = &http.Cookie{ cookie = &http.Cookie{
@ -111,7 +111,7 @@ func createSessionCookie(app *app.App, w http.ResponseWriter, session Session) {
} }
// deleteSessionCookie deletes the session cookie // deleteSessionCookie deletes the session cookie
func deleteSessionCookie(app *app.App, w http.ResponseWriter) { func deleteSessionCookie(app *app.Deps, w http.ResponseWriter) {
cookie := &http.Cookie{ cookie := &http.Cookie{
Name: "session", Name: "session",
Value: "", Value: "",
@ -123,7 +123,7 @@ func deleteSessionCookie(app *app.App, w http.ResponseWriter) {
} }
// DeleteSessionByAuthToken deletes a session from the database by AuthToken // DeleteSessionByAuthToken deletes a session from the database by AuthToken
func DeleteSessionByAuthToken(app *app.App, w http.ResponseWriter, authToken string) error { func DeleteSessionByAuthToken(app *app.Deps, w http.ResponseWriter, authToken string) error {
_, err := app.Db.Exec(deleteSessionByAuthToken, authToken) _, err := app.Db.Exec(deleteSessionByAuthToken, authToken)
if err != nil { if err != nil {
slog.Error("error deleting session from database") slog.Error("error deleting session from database")
@ -136,7 +136,7 @@ func DeleteSessionByAuthToken(app *app.App, w http.ResponseWriter, authToken str
} }
// ScheduledSessionCleanup deletes expired sessions from the database // ScheduledSessionCleanup deletes expired sessions from the database
func ScheduledSessionCleanup(app *app.App) { func ScheduledSessionCleanup(app *app.Deps) {
// Delete sessions older than 30 days (remember me sessions) // Delete sessions older than 30 days (remember me sessions)
_, err := app.Db.Exec(deleteSessionsOlderThan30Days) _, err := app.Db.Exec(deleteSessionsOlderThan30Days)
if err != nil { if err != nil {

View File

@ -1,7 +1,7 @@
package models package models
import ( import (
"GoWeb/app" "GoWeb/internal"
"crypto/sha256" "crypto/sha256"
"encoding/hex" "encoding/hex"
"log/slog" "log/slog"
@ -30,7 +30,7 @@ const (
) )
// CurrentUser finds the currently logged-in user by session cookie // CurrentUser finds the currently logged-in user by session cookie
func CurrentUser(app *app.App, r *http.Request) (User, error) { func CurrentUser(app *app.Deps, r *http.Request) (User, error) {
cookie, err := r.Cookie("session") cookie, err := r.Cookie("session")
if err != nil { if err != nil {
return User{}, err return User{}, err
@ -45,7 +45,7 @@ func CurrentUser(app *app.App, r *http.Request) (User, error) {
} }
// UserById finds a User table row in the database by id and returns a struct representing this row // UserById finds a User table row in the database by id and returns a struct representing this row
func UserById(app *app.App, id int64) (User, error) { func UserById(app *app.Deps, id int64) (User, error) {
user := User{} user := User{}
err := app.Db.QueryRow(selectUserById, id).Scan(&user.Id, &user.Username, &user.Password, &user.CreatedAt, &user.UpdatedAt) err := app.Db.QueryRow(selectUserById, id).Scan(&user.Id, &user.Username, &user.Password, &user.CreatedAt, &user.UpdatedAt)
@ -57,7 +57,7 @@ func UserById(app *app.App, id int64) (User, error) {
} }
// UserByUsername finds a User table row in the database by username and returns a struct representing this row // UserByUsername finds a User table row in the database by username and returns a struct representing this row
func UserByUsername(app *app.App, username string) (User, error) { func UserByUsername(app *app.Deps, username string) (User, error) {
user := User{} user := User{}
err := app.Db.QueryRow(selectUserByUsername, username).Scan(&user.Id, &user.Username, &user.Password, &user.CreatedAt, &user.UpdatedAt) err := app.Db.QueryRow(selectUserByUsername, username).Scan(&user.Id, &user.Username, &user.Password, &user.CreatedAt, &user.UpdatedAt)
@ -69,7 +69,7 @@ func UserByUsername(app *app.App, username string) (User, error) {
} }
// CreateUser creates a User table row in the database // CreateUser creates a User table row in the database
func CreateUser(app *app.App, username string, password string, createdAt time.Time, updatedAt time.Time) (User, error) { func CreateUser(app *app.Deps, username string, password string, createdAt time.Time, updatedAt time.Time) (User, error) {
// Get sha256 hash of password then get bcrypt hash to store // Get sha256 hash of password then get bcrypt hash to store
hash256 := sha256.New() hash256 := sha256.New()
hash256.Write([]byte(password)) hash256.Write([]byte(password))
@ -93,7 +93,7 @@ func CreateUser(app *app.App, username string, password string, createdAt time.T
} }
// AuthenticateUser validates the password for the specified user // AuthenticateUser validates the password for the specified user
func AuthenticateUser(app *app.App, w http.ResponseWriter, username string, password string, remember bool) (Session, error) { func AuthenticateUser(app *app.Deps, w http.ResponseWriter, username string, password string, remember bool) (Session, error) {
var user User var user User
err := app.Db.QueryRow(selectUserByUsername, username).Scan(&user.Id, &user.Username, &user.Password, &user.CreatedAt, &user.UpdatedAt) err := app.Db.QueryRow(selectUserByUsername, username).Scan(&user.Id, &user.Username, &user.Password, &user.CreatedAt, &user.UpdatedAt)
@ -117,7 +117,7 @@ func AuthenticateUser(app *app.App, w http.ResponseWriter, username string, pass
} }
// LogoutUser deletes the session cookie and AuthToken from the database // LogoutUser deletes the session cookie and AuthToken from the database
func LogoutUser(app *app.App, w http.ResponseWriter, r *http.Request) { func LogoutUser(app *app.Deps, w http.ResponseWriter, r *http.Request) {
cookie, err := r.Cookie("session") cookie, err := r.Cookie("session")
if err != nil { if err != nil {
return return

View File

@ -1,15 +1,15 @@
package routes package routes
import ( import (
"GoWeb/app" "GoWeb/internal"
"GoWeb/controllers" "GoWeb/internal/controllers"
"io/fs" "io/fs"
"log/slog" "log/slog"
"net/http" "net/http"
) )
// Get defines all project get routes // Get defines all project get routes
func Get(app *app.App) { func Get(app *app.Deps) {
// Get controller struct initialize // Get controller struct initialize
getController := controllers.Get{ getController := controllers.Get{
App: app, App: app,

View File

@ -1,14 +1,14 @@
package routes package routes
import ( import (
"GoWeb/app" "GoWeb/internal"
"GoWeb/controllers" "GoWeb/internal/controllers"
"GoWeb/middleware" "GoWeb/internal/middleware"
"net/http" "net/http"
) )
// Post defines all project post routes // Post defines all project post routes
func Post(app *app.App) { func Post(app *app.Deps) {
// Post controller struct initialize // Post controller struct initialize
postController := controllers.Post{ postController := controllers.Post{
App: app, App: app,

View File

@ -6,22 +6,22 @@ import (
) )
type Scheduled struct { type Scheduled struct {
EveryReboot []func(app *App) EveryReboot []func(app *Deps)
EverySecond []func(app *App) EverySecond []func(app *Deps)
EveryMinute []func(app *App) EveryMinute []func(app *Deps)
EveryHour []func(app *App) EveryHour []func(app *Deps)
EveryDay []func(app *App) EveryDay []func(app *Deps)
EveryWeek []func(app *App) EveryWeek []func(app *Deps)
EveryMonth []func(app *App) EveryMonth []func(app *Deps)
EveryYear []func(app *App) EveryYear []func(app *Deps)
} }
type Task struct { type Task struct {
Funcs []func(app *App) Funcs []func(app *Deps)
Interval time.Duration Interval time.Duration
} }
func RunScheduledTasks(app *App, poolSize int, stop <-chan struct{}) { func RunScheduledTasks(app *Deps, poolSize int, stop <-chan struct{}) {
for _, f := range app.ScheduledTasks.EveryReboot { for _, f := range app.ScheduledTasks.EveryReboot {
f(app) f(app)
} }
@ -51,7 +51,7 @@ func RunScheduledTasks(app *App, poolSize int, stop <-chan struct{}) {
case <-ticker.C: case <-ticker.C:
for _, f := range task.Funcs { for _, f := range task.Funcs {
runner <- true runner <- true
go func(f func(app *App)) { go func(f func(app *Deps)) {
defer func() { <-runner }() defer func() { <-runner }()
f(app) f(app)
}(f) }(f)

16
main.go
View File

@ -1,11 +1,11 @@
package main package main
import ( import (
"GoWeb/app"
"GoWeb/config" "GoWeb/config"
"GoWeb/database" "GoWeb/database"
"GoWeb/models" "GoWeb/internal"
"GoWeb/routes" "GoWeb/internal/models"
"GoWeb/internal/routes"
"GoWeb/templating" "GoWeb/templating"
"context" "context"
"embed" "embed"
@ -18,12 +18,12 @@ import (
"time" "time"
) )
//go:embed templates static //go:embed internal/frontend/templates internal/frontend/static
var res embed.FS var res embed.FS
func main() { func main() {
// Create instance of App // Create instance of Deps
appLoaded := app.App{} appLoaded := app.Deps{}
// Load config file to application // Load config file to application
appLoaded.Config = config.LoadConfig() appLoaded.Config = config.LoadConfig()
@ -60,8 +60,8 @@ func main() {
// Assign and run scheduled tasks // Assign and run scheduled tasks
appLoaded.ScheduledTasks = app.Scheduled{ appLoaded.ScheduledTasks = app.Scheduled{
EveryReboot: []func(app *app.App){models.ScheduledSessionCleanup}, EveryReboot: []func(app *app.Deps){models.ScheduledSessionCleanup},
EveryMinute: []func(app *app.App){models.ScheduledSessionCleanup}, EveryMinute: []func(app *app.Deps){models.ScheduledSessionCleanup},
} }
// Define Routes // Define Routes

View File

@ -1,5 +0,0 @@
package middleware
import "net/http"
type MiddlewareFunc func(f func(w http.ResponseWriter, r *http.Request)) func(w http.ResponseWriter, r *http.Request)

View File

@ -1,4 +1,4 @@
package restclient package rest
import ( import (
"bytes" "bytes"

View File

@ -1,7 +1,7 @@
package templating package templating
import ( import (
"GoWeb/app" "GoWeb/internal"
"fmt" "fmt"
"html/template" "html/template"
"io/fs" "io/fs"
@ -9,9 +9,9 @@ import (
"net/http" "net/http"
) )
var templates = make(map[string]*template.Template) // This is only used here, does not need to be in app.App var templates = make(map[string]*template.Template) // This is only used here, does not need to be in internal.Deps
func BuildPages(app *app.App) error { func BuildPages(app *app.Deps) error {
basePath := app.Config.Template.BaseName basePath := app.Config.Template.BaseName
baseContent, err := app.Res.ReadFile(basePath) baseContent, err := app.Res.ReadFile(basePath)