Compare commits
	
		
			5 Commits
		
	
	
		
			structure_
			...
			8ab50cb37c
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 
						 | 
					8ab50cb37c | ||
| 
						 | 
					6d6aff50b3 | ||
| 
						 | 
					a6be73765a | ||
| 
						 | 
					ddc9e51831 | ||
| 
						 | 
					dc450e26dd | 
@@ -6,8 +6,8 @@ import (
 | 
			
		||||
	"embed"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// Deps contains and supplies available configurations and connections
 | 
			
		||||
type Deps struct {
 | 
			
		||||
// App contains and supplies available configurations and connections
 | 
			
		||||
type App struct {
 | 
			
		||||
	Config         config.Configuration // Configuration file
 | 
			
		||||
	Db             *sql.DB              // Database connection
 | 
			
		||||
	Res            *embed.FS            // Resources from the embedded filesystem
 | 
			
		||||
@@ -6,22 +6,22 @@ import (
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type Scheduled struct {
 | 
			
		||||
	EveryReboot []func(app *Deps)
 | 
			
		||||
	EverySecond []func(app *Deps)
 | 
			
		||||
	EveryMinute []func(app *Deps)
 | 
			
		||||
	EveryHour   []func(app *Deps)
 | 
			
		||||
	EveryDay    []func(app *Deps)
 | 
			
		||||
	EveryWeek   []func(app *Deps)
 | 
			
		||||
	EveryMonth  []func(app *Deps)
 | 
			
		||||
	EveryYear   []func(app *Deps)
 | 
			
		||||
	EveryReboot []func(app *App)
 | 
			
		||||
	EverySecond []func(app *App)
 | 
			
		||||
	EveryMinute []func(app *App)
 | 
			
		||||
	EveryHour   []func(app *App)
 | 
			
		||||
	EveryDay    []func(app *App)
 | 
			
		||||
	EveryWeek   []func(app *App)
 | 
			
		||||
	EveryMonth  []func(app *App)
 | 
			
		||||
	EveryYear   []func(app *App)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type Task struct {
 | 
			
		||||
	Funcs    []func(app *Deps)
 | 
			
		||||
	Funcs    []func(app *App)
 | 
			
		||||
	Interval time.Duration
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func RunScheduledTasks(app *Deps, poolSize int, stop <-chan struct{}) {
 | 
			
		||||
func RunScheduledTasks(app *App, poolSize int, stop <-chan struct{}) {
 | 
			
		||||
	for _, f := range app.ScheduledTasks.EveryReboot {
 | 
			
		||||
		f(app)
 | 
			
		||||
	}
 | 
			
		||||
@@ -51,7 +51,7 @@ func RunScheduledTasks(app *Deps, poolSize int, stop <-chan struct{}) {
 | 
			
		||||
				case <-ticker.C:
 | 
			
		||||
					for _, f := range task.Funcs {
 | 
			
		||||
						runner <- true
 | 
			
		||||
						go func(f func(app *Deps)) {
 | 
			
		||||
						go func(f func(app *App)) {
 | 
			
		||||
							defer func() { <-runner }()
 | 
			
		||||
							f(app)
 | 
			
		||||
						}(f)
 | 
			
		||||
@@ -1,8 +1,8 @@
 | 
			
		||||
package controllers
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"GoWeb/internal"
 | 
			
		||||
	"GoWeb/internal/models"
 | 
			
		||||
	"GoWeb/app"
 | 
			
		||||
	"GoWeb/models"
 | 
			
		||||
	"GoWeb/security"
 | 
			
		||||
	"GoWeb/templating"
 | 
			
		||||
	"net/http"
 | 
			
		||||
@@ -10,16 +10,31 @@ import (
 | 
			
		||||
 | 
			
		||||
// Get is a wrapper struct for the App struct
 | 
			
		||||
type Get struct {
 | 
			
		||||
	App *app.Deps
 | 
			
		||||
	App *app.App
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (g *Get) ShowHome(w http.ResponseWriter, _ *http.Request) {
 | 
			
		||||
func (g *Get) ShowHome(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
	type dataStruct struct {
 | 
			
		||||
		CsrfToken       string
 | 
			
		||||
		IsAuthenticated bool
 | 
			
		||||
		Test            string
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	CsrfToken, err := security.GenerateCsrfToken(w, r)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	isAuthenticated := true
 | 
			
		||||
	user, err := models.CurrentUser(g.App, r)
 | 
			
		||||
	if err != nil || user.Id == 0 {
 | 
			
		||||
		isAuthenticated = false
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	data := dataStruct{
 | 
			
		||||
		CsrfToken:       CsrfToken,
 | 
			
		||||
		Test:            "Hello World!",
 | 
			
		||||
		IsAuthenticated: isAuthenticated,
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	templating.RenderTemplate(w, "templates/pages/home.html", data)
 | 
			
		||||
@@ -28,6 +43,7 @@ func (g *Get) ShowHome(w http.ResponseWriter, _ *http.Request) {
 | 
			
		||||
func (g *Get) ShowRegister(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
	type dataStruct struct {
 | 
			
		||||
		CsrfToken       string
 | 
			
		||||
		IsAuthenticated bool
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	CsrfToken, err := security.GenerateCsrfToken(w, r)
 | 
			
		||||
@@ -35,8 +51,15 @@ func (g *Get) ShowRegister(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	isAuthenticated := true
 | 
			
		||||
	user, err := models.CurrentUser(g.App, r)
 | 
			
		||||
	if err != nil || user.Id == 0 {
 | 
			
		||||
		isAuthenticated = false
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	data := dataStruct{
 | 
			
		||||
		CsrfToken:       CsrfToken,
 | 
			
		||||
		IsAuthenticated: isAuthenticated,
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	templating.RenderTemplate(w, "templates/pages/register.html", data)
 | 
			
		||||
@@ -45,6 +68,7 @@ func (g *Get) ShowRegister(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
func (g *Get) ShowLogin(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
	type dataStruct struct {
 | 
			
		||||
		CsrfToken       string
 | 
			
		||||
		IsAuthenticated bool
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	CsrfToken, err := security.GenerateCsrfToken(w, r)
 | 
			
		||||
@@ -58,8 +82,3 @@ func (g *Get) ShowLogin(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
 | 
			
		||||
	templating.RenderTemplate(w, "templates/pages/login.html", data)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (g *Get) Logout(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
	models.LogoutUser(g.App, w, r)
 | 
			
		||||
	http.Redirect(w, r, "/", http.StatusFound)
 | 
			
		||||
}
 | 
			
		||||
@@ -1,8 +1,8 @@
 | 
			
		||||
package controllers
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"GoWeb/internal"
 | 
			
		||||
	"GoWeb/internal/models"
 | 
			
		||||
	"GoWeb/app"
 | 
			
		||||
	"GoWeb/models"
 | 
			
		||||
	"log/slog"
 | 
			
		||||
	"net/http"
 | 
			
		||||
	"time"
 | 
			
		||||
@@ -10,7 +10,7 @@ import (
 | 
			
		||||
 | 
			
		||||
// Post is a wrapper struct for the App struct
 | 
			
		||||
type Post struct {
 | 
			
		||||
	App *app.Deps
 | 
			
		||||
	App *app.App
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (p *Post) Login(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
@@ -50,3 +50,8 @@ func (p *Post) Register(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
 | 
			
		||||
	http.Redirect(w, r, "/login", http.StatusFound)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (p *Post) Logout(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
	models.LogoutUser(p.App, w, r)
 | 
			
		||||
	http.Redirect(w, r, "/", http.StatusFound)
 | 
			
		||||
}
 | 
			
		||||
@@ -1,7 +1,7 @@
 | 
			
		||||
package database
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"GoWeb/internal"
 | 
			
		||||
	"GoWeb/app"
 | 
			
		||||
	"database/sql"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	_ "github.com/lib/pq"
 | 
			
		||||
@@ -9,7 +9,7 @@ import (
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// Connect returns a new database connection
 | 
			
		||||
func Connect(app *app.Deps) *sql.DB {
 | 
			
		||||
func Connect(app *app.App) *sql.DB {
 | 
			
		||||
	postgresConfig := fmt.Sprintf("host=%s port=%s user=%s "+
 | 
			
		||||
		"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)
 | 
			
		||||
 
 | 
			
		||||
@@ -1,7 +1,7 @@
 | 
			
		||||
package database
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"GoWeb/internal"
 | 
			
		||||
	"GoWeb/app"
 | 
			
		||||
	"errors"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"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
 | 
			
		||||
// as the type and create columns with the same name as the fields of the object
 | 
			
		||||
func Migrate(app *app.Deps, anyStruct interface{}) error {
 | 
			
		||||
func Migrate(app *app.App, anyStruct interface{}) error {
 | 
			
		||||
	valueOfStruct := reflect.ValueOf(anyStruct)
 | 
			
		||||
	typeOfStruct := valueOfStruct.Type()
 | 
			
		||||
 | 
			
		||||
@@ -41,7 +41,7 @@ func Migrate(app *app.Deps, 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
 | 
			
		||||
func createTable(app *app.Deps, tableName string) error {
 | 
			
		||||
func createTable(app *app.App, tableName string) error {
 | 
			
		||||
	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)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
@@ -67,7 +67,7 @@ func createTable(app *app.Deps, tableName string) error {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// createColumn creates a column with the given name and type if it doesn't exist
 | 
			
		||||
func createColumn(app *app.Deps, tableName, columnName, columnType string) error {
 | 
			
		||||
func createColumn(app *app.App, tableName, columnName, columnType string) error {
 | 
			
		||||
	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)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
 
 | 
			
		||||
@@ -12,7 +12,7 @@
 | 
			
		||||
    "HttpPort": "8090"
 | 
			
		||||
  },
 | 
			
		||||
  "Template": {
 | 
			
		||||
    "BaseTemplateName": "internal/frontend/templates/base.html",
 | 
			
		||||
    "ContentPath": "internal/frontend/templates"
 | 
			
		||||
    "BaseTemplateName": "templates/base.html",
 | 
			
		||||
    "ContentPath": "templates"
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										2
									
								
								go.mod
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								go.mod
									
									
									
									
									
								
							@@ -4,5 +4,5 @@ go 1.22
 | 
			
		||||
 | 
			
		||||
require (
 | 
			
		||||
	github.com/lib/pq v1.10.9
 | 
			
		||||
	golang.org/x/crypto v0.24.0
 | 
			
		||||
	golang.org/x/crypto v0.19.0
 | 
			
		||||
)
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										4
									
								
								go.sum
									
									
									
									
									
								
							
							
						
						
									
										4
									
								
								go.sum
									
									
									
									
									
								
							@@ -1,4 +1,4 @@
 | 
			
		||||
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
 | 
			
		||||
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
 | 
			
		||||
golang.org/x/crypto v0.24.0 h1:mnl8DM0o513X8fdIkmyFE/5hTYxbwYOjDS/+rK6qpRI=
 | 
			
		||||
golang.org/x/crypto v0.24.0/go.mod h1:Z1PMYSOR5nyMcyAVAIQSKCDwalqy85Aqn1x3Ws4L5DM=
 | 
			
		||||
golang.org/x/crypto v0.19.0 h1:ENy+Az/9Y1vSrlrvBSyna3PITt4tiZLf7sgCjZBX7Wo=
 | 
			
		||||
golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU=
 | 
			
		||||
 
 | 
			
		||||
@@ -1,16 +0,0 @@
 | 
			
		||||
<!DOCTYPE html>
 | 
			
		||||
<html lang="en">
 | 
			
		||||
<head>
 | 
			
		||||
    <meta charset="UTF-8">
 | 
			
		||||
    <title>SiteName - {{ template "pageTitle" }}</title>
 | 
			
		||||
    <link href="/app/frontend/staticyle.css" rel="stylesheet">
 | 
			
		||||
</head>
 | 
			
		||||
<body>
 | 
			
		||||
{{ template "content" . }}
 | 
			
		||||
<div class="footer-container">
 | 
			
		||||
    <footer>
 | 
			
		||||
        <p>SiteName - Powered by GoWeb!</p>
 | 
			
		||||
    </footer>
 | 
			
		||||
</div>
 | 
			
		||||
</body>
 | 
			
		||||
</html>
 | 
			
		||||
@@ -1,20 +0,0 @@
 | 
			
		||||
package routes
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"GoWeb/internal"
 | 
			
		||||
	"GoWeb/internal/controllers"
 | 
			
		||||
	"GoWeb/internal/middleware"
 | 
			
		||||
	"net/http"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// Post defines all project post routes
 | 
			
		||||
func Post(app *app.Deps) {
 | 
			
		||||
	// Post controller struct initialize
 | 
			
		||||
	postController := controllers.Post{
 | 
			
		||||
		App: app,
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// User authentication
 | 
			
		||||
	http.HandleFunc("/register-handle", middleware.Csrf(postController.Register))
 | 
			
		||||
	http.HandleFunc("/login-handle", middleware.Csrf(postController.Login))
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										16
									
								
								main.go
									
									
									
									
									
								
							
							
						
						
									
										16
									
								
								main.go
									
									
									
									
									
								
							@@ -1,11 +1,11 @@
 | 
			
		||||
package main
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"GoWeb/app"
 | 
			
		||||
	"GoWeb/config"
 | 
			
		||||
	"GoWeb/database"
 | 
			
		||||
	"GoWeb/internal"
 | 
			
		||||
	"GoWeb/internal/models"
 | 
			
		||||
	"GoWeb/internal/routes"
 | 
			
		||||
	"GoWeb/models"
 | 
			
		||||
	"GoWeb/routes"
 | 
			
		||||
	"GoWeb/templating"
 | 
			
		||||
	"context"
 | 
			
		||||
	"embed"
 | 
			
		||||
@@ -18,12 +18,12 @@ import (
 | 
			
		||||
	"time"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
//go:embed internal/frontend/templates internal/frontend/static
 | 
			
		||||
//go:embed templates static
 | 
			
		||||
var res embed.FS
 | 
			
		||||
 | 
			
		||||
func main() {
 | 
			
		||||
	// Create instance of Deps
 | 
			
		||||
	appLoaded := app.Deps{}
 | 
			
		||||
	// Create instance of App
 | 
			
		||||
	appLoaded := app.App{}
 | 
			
		||||
 | 
			
		||||
	// Load config file to application
 | 
			
		||||
	appLoaded.Config = config.LoadConfig()
 | 
			
		||||
@@ -60,8 +60,8 @@ func main() {
 | 
			
		||||
 | 
			
		||||
	// Assign and run scheduled tasks
 | 
			
		||||
	appLoaded.ScheduledTasks = app.Scheduled{
 | 
			
		||||
		EveryReboot: []func(app *app.Deps){models.ScheduledSessionCleanup},
 | 
			
		||||
		EveryMinute: []func(app *app.Deps){models.ScheduledSessionCleanup},
 | 
			
		||||
		EveryReboot: []func(app *app.App){models.ScheduledSessionCleanup},
 | 
			
		||||
		EveryMinute: []func(app *app.App){models.ScheduledSessionCleanup},
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Define Routes
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										5
									
								
								middleware/groups.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										5
									
								
								middleware/groups.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,5 @@
 | 
			
		||||
package middleware
 | 
			
		||||
 | 
			
		||||
import "net/http"
 | 
			
		||||
 | 
			
		||||
type MiddlewareFunc func(f func(w http.ResponseWriter, r *http.Request)) func(w http.ResponseWriter, r *http.Request)
 | 
			
		||||
@@ -2,8 +2,6 @@ package middleware
 | 
			
		||||
 | 
			
		||||
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
 | 
			
		||||
// 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
 | 
			
		||||
@@ -1,13 +1,13 @@
 | 
			
		||||
package models
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"GoWeb/app"
 | 
			
		||||
	"GoWeb/database"
 | 
			
		||||
	"GoWeb/internal"
 | 
			
		||||
	"time"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// RunAllMigrations defines the structs that should be represented in the database
 | 
			
		||||
func RunAllMigrations(app *app.Deps) error {
 | 
			
		||||
func RunAllMigrations(app *app.App) error {
 | 
			
		||||
	// Declare new dummy user for reflection
 | 
			
		||||
	user := User{
 | 
			
		||||
		Id:        1, // Id is handled automatically, but it is added here to show it will be skipped during column creation
 | 
			
		||||
@@ -25,7 +25,7 @@ func RunAllMigrations(app *app.Deps) error {
 | 
			
		||||
		Id:         1,
 | 
			
		||||
		UserId:     1,
 | 
			
		||||
		AuthToken:  "migrate",
 | 
			
		||||
		RememberMe: false,
 | 
			
		||||
		RememberMe: true, // Booleans must be true to migrate properly
 | 
			
		||||
		CreatedAt:  time.Now(),
 | 
			
		||||
	}
 | 
			
		||||
	err = database.Migrate(app, session)
 | 
			
		||||
@@ -1,7 +1,7 @@
 | 
			
		||||
package models
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"GoWeb/internal"
 | 
			
		||||
	"GoWeb/app"
 | 
			
		||||
	"crypto/rand"
 | 
			
		||||
	"encoding/hex"
 | 
			
		||||
	"log/slog"
 | 
			
		||||
@@ -31,7 +31,7 @@ const (
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// CreateSession creates a new session for a user
 | 
			
		||||
func CreateSession(app *app.Deps, w http.ResponseWriter, userId int64, remember bool) (Session, error) {
 | 
			
		||||
func CreateSession(app *app.App, w http.ResponseWriter, userId int64, remember bool) (Session, error) {
 | 
			
		||||
	session := Session{}
 | 
			
		||||
	session.UserId = userId
 | 
			
		||||
	session.AuthToken = generateAuthToken(app)
 | 
			
		||||
@@ -62,7 +62,7 @@ func CreateSession(app *app.Deps, w http.ResponseWriter, userId int64, remember
 | 
			
		||||
	return session, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func SessionByAuthToken(app *app.Deps, authToken string) (Session, error) {
 | 
			
		||||
func SessionByAuthToken(app *app.App, authToken string) (Session, error) {
 | 
			
		||||
	session := Session{}
 | 
			
		||||
 | 
			
		||||
	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.Deps, authToken string) (Session, error) {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// generateAuthToken generates a random 64-byte string
 | 
			
		||||
func generateAuthToken(app *app.Deps) string {
 | 
			
		||||
func generateAuthToken(app *app.App) string {
 | 
			
		||||
	b := make([]byte, 64)
 | 
			
		||||
	_, err := rand.Read(b)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
@@ -85,7 +85,7 @@ func generateAuthToken(app *app.Deps) string {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// createSessionCookie creates a new session cookie
 | 
			
		||||
func createSessionCookie(app *app.Deps, w http.ResponseWriter, session Session) {
 | 
			
		||||
func createSessionCookie(app *app.App, w http.ResponseWriter, session Session) {
 | 
			
		||||
	cookie := &http.Cookie{}
 | 
			
		||||
	if session.RememberMe {
 | 
			
		||||
		cookie = &http.Cookie{
 | 
			
		||||
@@ -111,7 +111,7 @@ func createSessionCookie(app *app.Deps, w http.ResponseWriter, session Session)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// deleteSessionCookie deletes the session cookie
 | 
			
		||||
func deleteSessionCookie(app *app.Deps, w http.ResponseWriter) {
 | 
			
		||||
func deleteSessionCookie(app *app.App, w http.ResponseWriter) {
 | 
			
		||||
	cookie := &http.Cookie{
 | 
			
		||||
		Name:   "session",
 | 
			
		||||
		Value:  "",
 | 
			
		||||
@@ -123,7 +123,7 @@ func deleteSessionCookie(app *app.Deps, w http.ResponseWriter) {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// DeleteSessionByAuthToken deletes a session from the database by AuthToken
 | 
			
		||||
func DeleteSessionByAuthToken(app *app.Deps, w http.ResponseWriter, authToken string) error {
 | 
			
		||||
func DeleteSessionByAuthToken(app *app.App, w http.ResponseWriter, authToken string) error {
 | 
			
		||||
	_, err := app.Db.Exec(deleteSessionByAuthToken, authToken)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		slog.Error("error deleting session from database")
 | 
			
		||||
@@ -136,7 +136,7 @@ func DeleteSessionByAuthToken(app *app.Deps, w http.ResponseWriter, authToken st
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ScheduledSessionCleanup deletes expired sessions from the database
 | 
			
		||||
func ScheduledSessionCleanup(app *app.Deps) {
 | 
			
		||||
func ScheduledSessionCleanup(app *app.App) {
 | 
			
		||||
	// Delete sessions older than 30 days (remember me sessions)
 | 
			
		||||
	_, err := app.Db.Exec(deleteSessionsOlderThan30Days)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
@@ -1,7 +1,7 @@
 | 
			
		||||
package models
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"GoWeb/internal"
 | 
			
		||||
	"GoWeb/app"
 | 
			
		||||
	"crypto/sha256"
 | 
			
		||||
	"encoding/hex"
 | 
			
		||||
	"log/slog"
 | 
			
		||||
@@ -30,7 +30,7 @@ const (
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// CurrentUser finds the currently logged-in user by session cookie
 | 
			
		||||
func CurrentUser(app *app.Deps, r *http.Request) (User, error) {
 | 
			
		||||
func CurrentUser(app *app.App, r *http.Request) (User, error) {
 | 
			
		||||
	cookie, err := r.Cookie("session")
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return User{}, err
 | 
			
		||||
@@ -45,7 +45,7 @@ func CurrentUser(app *app.Deps, r *http.Request) (User, error) {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// UserById finds a User table row in the database by id and returns a struct representing this row
 | 
			
		||||
func UserById(app *app.Deps, id int64) (User, error) {
 | 
			
		||||
func UserById(app *app.App, id int64) (User, error) {
 | 
			
		||||
	user := User{}
 | 
			
		||||
 | 
			
		||||
	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.Deps, id int64) (User, error) {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// UserByUsername finds a User table row in the database by username and returns a struct representing this row
 | 
			
		||||
func UserByUsername(app *app.Deps, username string) (User, error) {
 | 
			
		||||
func UserByUsername(app *app.App, username string) (User, error) {
 | 
			
		||||
	user := User{}
 | 
			
		||||
 | 
			
		||||
	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.Deps, username string) (User, error) {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// CreateUser creates a User table row in the database
 | 
			
		||||
func CreateUser(app *app.Deps, username string, password string, createdAt time.Time, updatedAt time.Time) (User, error) {
 | 
			
		||||
func CreateUser(app *app.App, username string, password string, createdAt time.Time, updatedAt time.Time) (User, error) {
 | 
			
		||||
	// Get sha256 hash of password then get bcrypt hash to store
 | 
			
		||||
	hash256 := sha256.New()
 | 
			
		||||
	hash256.Write([]byte(password))
 | 
			
		||||
@@ -93,7 +93,7 @@ func CreateUser(app *app.Deps, username string, password string, createdAt time.
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// AuthenticateUser validates the password for the specified user
 | 
			
		||||
func AuthenticateUser(app *app.Deps, w http.ResponseWriter, username string, password string, remember bool) (Session, error) {
 | 
			
		||||
func AuthenticateUser(app *app.App, w http.ResponseWriter, username string, password string, remember bool) (Session, error) {
 | 
			
		||||
	var user User
 | 
			
		||||
 | 
			
		||||
	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.Deps, w http.ResponseWriter, username string, pas
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// LogoutUser deletes the session cookie and AuthToken from the database
 | 
			
		||||
func LogoutUser(app *app.Deps, w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
func LogoutUser(app *app.App, w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
	cookie, err := r.Cookie("session")
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return
 | 
			
		||||
@@ -1,4 +1,4 @@
 | 
			
		||||
package rest
 | 
			
		||||
package restclient
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"bytes"
 | 
			
		||||
@@ -1,15 +1,15 @@
 | 
			
		||||
package routes
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"GoWeb/internal"
 | 
			
		||||
	"GoWeb/internal/controllers"
 | 
			
		||||
	"GoWeb/app"
 | 
			
		||||
	"GoWeb/controllers"
 | 
			
		||||
	"io/fs"
 | 
			
		||||
	"log/slog"
 | 
			
		||||
	"net/http"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// Get defines all project get routes
 | 
			
		||||
func Get(app *app.Deps) {
 | 
			
		||||
func Get(app *app.App) {
 | 
			
		||||
	// Get controller struct initialize
 | 
			
		||||
	getController := controllers.Get{
 | 
			
		||||
		App: app,
 | 
			
		||||
@@ -22,12 +22,11 @@ func Get(app *app.Deps) {
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
	staticHandler := http.FileServer(http.FS(staticFS))
 | 
			
		||||
	http.Handle("/static/", http.StripPrefix("/static/", staticHandler))
 | 
			
		||||
	http.Handle("GET /static/", http.StripPrefix("/static/", staticHandler))
 | 
			
		||||
	slog.Info("serving static files from embedded file system /static")
 | 
			
		||||
 | 
			
		||||
	// Pages
 | 
			
		||||
	http.HandleFunc("/", getController.ShowHome)
 | 
			
		||||
	http.HandleFunc("/login", getController.ShowLogin)
 | 
			
		||||
	http.HandleFunc("/register", getController.ShowRegister)
 | 
			
		||||
	http.HandleFunc("/logout", getController.Logout)
 | 
			
		||||
	http.HandleFunc("GET /", getController.ShowHome)
 | 
			
		||||
	http.HandleFunc("GET /login", getController.ShowLogin)
 | 
			
		||||
	http.HandleFunc("GET /register", getController.ShowRegister)
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										21
									
								
								routes/post.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								routes/post.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,21 @@
 | 
			
		||||
package routes
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"GoWeb/app"
 | 
			
		||||
	"GoWeb/controllers"
 | 
			
		||||
	"GoWeb/middleware"
 | 
			
		||||
	"net/http"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// Post defines all project post routes
 | 
			
		||||
func Post(app *app.App) {
 | 
			
		||||
	// Post controller struct initialize
 | 
			
		||||
	postController := controllers.Post{
 | 
			
		||||
		App: app,
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// User authentication
 | 
			
		||||
	http.HandleFunc("POST /register-handle", middleware.Csrf(postController.Register))
 | 
			
		||||
	http.HandleFunc("POST /login-handle", middleware.Csrf(postController.Login))
 | 
			
		||||
	http.HandleFunc("POST /logout", middleware.Csrf(postController.Logout))
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										31
									
								
								templates/base.html
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										31
									
								
								templates/base.html
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,31 @@
 | 
			
		||||
<!DOCTYPE html>
 | 
			
		||||
<html lang="en">
 | 
			
		||||
<head>
 | 
			
		||||
    <meta charset="UTF-8">
 | 
			
		||||
    <title>SiteName - {{ template "pageTitle" }}</title>
 | 
			
		||||
    <link href="/static/css/style.css" rel="stylesheet">
 | 
			
		||||
</head>
 | 
			
		||||
<body>
 | 
			
		||||
<div class="navbar">
 | 
			
		||||
    {{ if .IsAuthenticated }}
 | 
			
		||||
    <form action="/logout" method="post">
 | 
			
		||||
        <input name="csrf_token" type="hidden" value="{{ .CsrfToken }}">
 | 
			
		||||
        <input type="submit" value="Logout">
 | 
			
		||||
    </form>
 | 
			
		||||
    {{ else }}
 | 
			
		||||
    <form action="/login" method="get">
 | 
			
		||||
        <input type="submit" value="Login">
 | 
			
		||||
    </form>
 | 
			
		||||
    <form action="/register" method="get">
 | 
			
		||||
        <input type="submit" value="Register">
 | 
			
		||||
    </form>
 | 
			
		||||
    {{ end }}
 | 
			
		||||
</div>
 | 
			
		||||
{{ template "content" . }}
 | 
			
		||||
<div class="footer-container">
 | 
			
		||||
    <footer>
 | 
			
		||||
        <p>SiteName - Powered by GoWeb!</p>
 | 
			
		||||
    </footer>
 | 
			
		||||
</div>
 | 
			
		||||
</body>
 | 
			
		||||
</html>
 | 
			
		||||
@@ -1,7 +1,7 @@
 | 
			
		||||
package templating
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"GoWeb/internal"
 | 
			
		||||
	"GoWeb/app"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"html/template"
 | 
			
		||||
	"io/fs"
 | 
			
		||||
@@ -9,9 +9,9 @@ import (
 | 
			
		||||
	"net/http"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
var templates = make(map[string]*template.Template) // This is only used here, does not need to be in internal.Deps
 | 
			
		||||
var templates = make(map[string]*template.Template) // This is only used here, does not need to be in app.App
 | 
			
		||||
 | 
			
		||||
func BuildPages(app *app.Deps) error {
 | 
			
		||||
func BuildPages(app *app.App) error {
 | 
			
		||||
	basePath := app.Config.Template.BaseName
 | 
			
		||||
 | 
			
		||||
	baseContent, err := app.Res.ReadFile(basePath)
 | 
			
		||||
		Reference in New Issue
	
	Block a user