Compare commits
4 Commits
9670b7d717
...
file_uploa
Author | SHA1 | Date | |
---|---|---|---|
37391190fb | |||
1fb8fdef81 | |||
baef0cbe78 | |||
d0da1a9114 |
@ -12,9 +12,7 @@ fine with getting your hands dirty, but I plan on having it ready to go for more
|
|||||||
- Routing/controllers
|
- Routing/controllers
|
||||||
- Templating
|
- Templating
|
||||||
- Simple database migration system
|
- Simple database migration system
|
||||||
- Built in REST client
|
|
||||||
- CSRF protection
|
- CSRF protection
|
||||||
- Middleware
|
|
||||||
- Minimal user login/registration + sessions
|
- Minimal user login/registration + sessions
|
||||||
- Config file handling
|
- Config file handling
|
||||||
- Scheduled tasks
|
- Scheduled tasks
|
||||||
|
@ -22,6 +22,7 @@ type Task struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func RunScheduledTasks(app *App, poolSize int, stop <-chan struct{}) {
|
func RunScheduledTasks(app *App, poolSize int, stop <-chan struct{}) {
|
||||||
|
// Run every time the server starts
|
||||||
for _, f := range app.ScheduledTasks.EveryReboot {
|
for _, f := range app.ScheduledTasks.EveryReboot {
|
||||||
f(app)
|
f(app)
|
||||||
}
|
}
|
||||||
@ -36,6 +37,7 @@ func RunScheduledTasks(app *App, poolSize int, stop <-chan struct{}) {
|
|||||||
{Interval: 365 * 24 * time.Hour, Funcs: app.ScheduledTasks.EveryYear},
|
{Interval: 365 * 24 * time.Hour, Funcs: app.ScheduledTasks.EveryYear},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Set up task runners
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
runners := make([]chan bool, len(tasks))
|
runners := make([]chan bool, len(tasks))
|
||||||
for i, task := range tasks {
|
for i, task := range tasks {
|
||||||
@ -63,8 +65,10 @@ func RunScheduledTasks(app *App, poolSize int, stop <-chan struct{}) {
|
|||||||
}(task, runner)
|
}(task, runner)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Wait for all goroutines to finish
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
|
|
||||||
|
// Close channels
|
||||||
for _, runner := range runners {
|
for _, runner := range runners {
|
||||||
close(runner)
|
close(runner)
|
||||||
}
|
}
|
||||||
|
@ -25,6 +25,11 @@ type Configuration struct {
|
|||||||
Template struct {
|
Template struct {
|
||||||
BaseName string `json:"BaseTemplateName"`
|
BaseName string `json:"BaseTemplateName"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Upload struct {
|
||||||
|
BaseName string `json:"UploadDirectoryName"`
|
||||||
|
MaxSize int64 `json:"MaxUploadSize"`
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// LoadConfig loads and returns a configuration struct
|
// LoadConfig loads and returns a configuration struct
|
||||||
@ -43,6 +48,7 @@ func LoadConfig() Configuration {
|
|||||||
}
|
}
|
||||||
}(file)
|
}(file)
|
||||||
|
|
||||||
|
// Decode json config file to Configuration struct named config
|
||||||
decoder := json.NewDecoder(file)
|
decoder := json.NewDecoder(file)
|
||||||
Config := Configuration{}
|
Config := Configuration{}
|
||||||
err = decoder.Decode(&Config)
|
err = decoder.Decode(&Config)
|
||||||
|
@ -1,65 +0,0 @@
|
|||||||
package controllers
|
|
||||||
|
|
||||||
import (
|
|
||||||
"GoWeb/app"
|
|
||||||
"GoWeb/models"
|
|
||||||
"GoWeb/security"
|
|
||||||
"GoWeb/templating"
|
|
||||||
"net/http"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Get is a wrapper struct for the App struct
|
|
||||||
type Get struct {
|
|
||||||
App *app.App
|
|
||||||
}
|
|
||||||
|
|
||||||
func (g *Get) ShowHome(w http.ResponseWriter, _ *http.Request) {
|
|
||||||
type dataStruct struct {
|
|
||||||
Test string
|
|
||||||
}
|
|
||||||
|
|
||||||
data := dataStruct{
|
|
||||||
Test: "Hello World!",
|
|
||||||
}
|
|
||||||
|
|
||||||
templating.RenderTemplate(g.App, w, "templates/pages/home.html", data)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (g *Get) ShowRegister(w http.ResponseWriter, r *http.Request) {
|
|
||||||
type dataStruct struct {
|
|
||||||
CsrfToken string
|
|
||||||
}
|
|
||||||
|
|
||||||
CsrfToken, err := security.GenerateCsrfToken(w, r)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
data := dataStruct{
|
|
||||||
CsrfToken: CsrfToken,
|
|
||||||
}
|
|
||||||
|
|
||||||
templating.RenderTemplate(g.App, w, "templates/pages/register.html", data)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (g *Get) ShowLogin(w http.ResponseWriter, r *http.Request) {
|
|
||||||
type dataStruct struct {
|
|
||||||
CsrfToken string
|
|
||||||
}
|
|
||||||
|
|
||||||
CsrfToken, err := security.GenerateCsrfToken(w, r)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
data := dataStruct{
|
|
||||||
CsrfToken: CsrfToken,
|
|
||||||
}
|
|
||||||
|
|
||||||
templating.RenderTemplate(g.App, 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)
|
|
||||||
}
|
|
74
controllers/getController.go
Normal file
74
controllers/getController.go
Normal file
@ -0,0 +1,74 @@
|
|||||||
|
package controllers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"GoWeb/app"
|
||||||
|
"GoWeb/models"
|
||||||
|
"GoWeb/security"
|
||||||
|
"GoWeb/templating"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
// GetController is a wrapper struct for the App struct
|
||||||
|
type GetController struct {
|
||||||
|
App *app.App
|
||||||
|
}
|
||||||
|
|
||||||
|
func (getController *GetController) ShowHome(w http.ResponseWriter, _ *http.Request) {
|
||||||
|
type dataStruct struct {
|
||||||
|
Test string
|
||||||
|
}
|
||||||
|
|
||||||
|
data := dataStruct{
|
||||||
|
Test: "Hello World!",
|
||||||
|
}
|
||||||
|
|
||||||
|
templating.RenderTemplate(getController.App, w, "templates/pages/home.html", data)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (getController *GetController) ShowRegister(w http.ResponseWriter, r *http.Request) {
|
||||||
|
type dataStruct struct {
|
||||||
|
CsrfToken string
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create csrf token
|
||||||
|
CsrfToken, err := security.GenerateCsrfToken(w, r)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
data := dataStruct{
|
||||||
|
CsrfToken: CsrfToken,
|
||||||
|
}
|
||||||
|
|
||||||
|
templating.RenderTemplate(getController.App, w, "templates/pages/register.html", data)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (getController *GetController) ShowLogin(w http.ResponseWriter, r *http.Request) {
|
||||||
|
type dataStruct struct {
|
||||||
|
CsrfToken string
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create csrf token
|
||||||
|
CsrfToken, err := security.GenerateCsrfToken(w, r)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
data := dataStruct{
|
||||||
|
CsrfToken: CsrfToken,
|
||||||
|
}
|
||||||
|
|
||||||
|
templating.RenderTemplate(getController.App, w, "templates/pages/login.html", data)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (getController *GetController) ShowFile(w http.ResponseWriter, r *http.Request) {
|
||||||
|
// GET /uploads?name=file.jpg
|
||||||
|
// will serve file.jpg
|
||||||
|
name := r.URL.Query().Get("name")
|
||||||
|
http.ServeFile(w, r, getController.App.Config.Upload.BaseName+name)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (getController *GetController) Logout(w http.ResponseWriter, r *http.Request) {
|
||||||
|
models.LogoutUser(getController.App, w, r)
|
||||||
|
http.Redirect(w, r, "/", http.StatusFound)
|
||||||
|
}
|
@ -1,56 +0,0 @@
|
|||||||
package controllers
|
|
||||||
|
|
||||||
import (
|
|
||||||
"GoWeb/app"
|
|
||||||
"GoWeb/models"
|
|
||||||
"log"
|
|
||||||
"net/http"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Post is a wrapper struct for the App struct
|
|
||||||
type Post struct {
|
|
||||||
App *app.App
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *Post) Login(w http.ResponseWriter, r *http.Request) {
|
|
||||||
username := r.FormValue("username")
|
|
||||||
password := r.FormValue("password")
|
|
||||||
remember := r.FormValue("remember") == "on"
|
|
||||||
|
|
||||||
if username == "" || password == "" {
|
|
||||||
log.Println("Tried to login user with empty username or password")
|
|
||||||
http.Redirect(w, r, "/login", http.StatusFound)
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err := models.AuthenticateUser(p.App, w, username, password, remember)
|
|
||||||
if err != nil {
|
|
||||||
log.Println("Error authenticating user")
|
|
||||||
log.Println(err)
|
|
||||||
http.Redirect(w, r, "/login", http.StatusFound)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
http.Redirect(w, r, "/", http.StatusFound)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *Post) Register(w http.ResponseWriter, r *http.Request) {
|
|
||||||
username := r.FormValue("username")
|
|
||||||
password := r.FormValue("password")
|
|
||||||
createdAt := time.Now()
|
|
||||||
updatedAt := time.Now()
|
|
||||||
|
|
||||||
if username == "" || password == "" {
|
|
||||||
log.Println("Tried to create user with empty username or password")
|
|
||||||
http.Redirect(w, r, "/register", http.StatusFound)
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err := models.CreateUser(p.App, username, password, createdAt, updatedAt)
|
|
||||||
if err != nil {
|
|
||||||
log.Println("Error creating user")
|
|
||||||
log.Println(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
http.Redirect(w, r, "/login", http.StatusFound)
|
|
||||||
}
|
|
131
controllers/postController.go
Normal file
131
controllers/postController.go
Normal file
@ -0,0 +1,131 @@
|
|||||||
|
package controllers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"GoWeb/app"
|
||||||
|
"GoWeb/models"
|
||||||
|
"GoWeb/security"
|
||||||
|
"io"
|
||||||
|
"log"
|
||||||
|
"mime/multipart"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// PostController is a wrapper struct for the App struct
|
||||||
|
type PostController struct {
|
||||||
|
App *app.App
|
||||||
|
}
|
||||||
|
|
||||||
|
func (postController *PostController) Login(w http.ResponseWriter, r *http.Request) {
|
||||||
|
// Validate csrf token
|
||||||
|
_, err := security.VerifyCsrfToken(r)
|
||||||
|
if err != nil {
|
||||||
|
log.Println("Error verifying csrf token")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
username := r.FormValue("username")
|
||||||
|
password := r.FormValue("password")
|
||||||
|
remember := r.FormValue("remember") == "on"
|
||||||
|
|
||||||
|
if username == "" || password == "" {
|
||||||
|
log.Println("Tried to login user with empty username or password")
|
||||||
|
http.Redirect(w, r, "/login", http.StatusFound)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = models.AuthenticateUser(postController.App, w, username, password, remember)
|
||||||
|
if err != nil {
|
||||||
|
log.Println("Error authenticating user")
|
||||||
|
log.Println(err)
|
||||||
|
http.Redirect(w, r, "/login", http.StatusFound)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
http.Redirect(w, r, "/", http.StatusFound)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (postController *PostController) Register(w http.ResponseWriter, r *http.Request) {
|
||||||
|
// Validate csrf token
|
||||||
|
_, err := security.VerifyCsrfToken(r)
|
||||||
|
if err != nil {
|
||||||
|
log.Println("Error verifying csrf token")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
username := r.FormValue("username")
|
||||||
|
password := r.FormValue("password")
|
||||||
|
createdAt := time.Now()
|
||||||
|
updatedAt := time.Now()
|
||||||
|
|
||||||
|
if username == "" || password == "" {
|
||||||
|
log.Println("Tried to create user with empty username or password")
|
||||||
|
http.Redirect(w, r, "/register", http.StatusFound)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = models.CreateUser(postController.App, username, password, createdAt, updatedAt)
|
||||||
|
if err != nil {
|
||||||
|
log.Println("Error creating user")
|
||||||
|
log.Println(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
http.Redirect(w, r, "/login", http.StatusFound)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (postController *PostController) FileUpload(w http.ResponseWriter, r *http.Request) {
|
||||||
|
max := postController.App.Config.Upload.MaxSize
|
||||||
|
err := r.ParseMultipartForm(max)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// FormFile returns the first file for the given key `file`
|
||||||
|
// it also returns the FileHeader, so we can get the Filename,
|
||||||
|
// the Header and the size of the file
|
||||||
|
file, handler, err := r.FormFile("file")
|
||||||
|
if err != nil {
|
||||||
|
log.Println("Error Retrieving the File")
|
||||||
|
log.Println(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer func(file multipart.File) {
|
||||||
|
err := file.Close()
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
}
|
||||||
|
}(file)
|
||||||
|
|
||||||
|
if handler.Size > max {
|
||||||
|
log.Println("User tried uploading a file which is too large.")
|
||||||
|
http.Redirect(w, r, "/", http.StatusRequestHeaderFieldsTooLarge)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create a temporary file within upload directory
|
||||||
|
tempFile, err := os.Create(postController.App.Config.Upload.BaseName + handler.Filename)
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
http.Redirect(w, r, "/", http.StatusNotAcceptable)
|
||||||
|
}
|
||||||
|
defer func(tempFile *os.File) {
|
||||||
|
err := tempFile.Close()
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
}
|
||||||
|
}(tempFile)
|
||||||
|
|
||||||
|
// read all the contents of our uploaded file into a
|
||||||
|
// byte array
|
||||||
|
fileBytes, err := io.ReadAll(file)
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = tempFile.Write(fileBytes)
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
http.Redirect(w, r, "/", http.StatusFound)
|
||||||
|
}
|
@ -8,8 +8,8 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Connect returns a new database connection
|
// ConnectDB returns a new database connection
|
||||||
func Connect(app *app.App) *sql.DB {
|
func ConnectDB(app *app.App) *sql.DB {
|
||||||
// Set connection parameters from config
|
// Set connection parameters from config
|
||||||
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",
|
@ -36,6 +36,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.App, tableName string) error {
|
||||||
|
// Check to see if the table already exists
|
||||||
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 {
|
||||||
@ -62,6 +63,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.App, tableName, columnName, columnType string) error {
|
||||||
|
// Check to see if the column already exists
|
||||||
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 {
|
||||||
|
@ -13,5 +13,9 @@
|
|||||||
},
|
},
|
||||||
"Template": {
|
"Template": {
|
||||||
"BaseTemplateName": "templates/base.html"
|
"BaseTemplateName": "templates/base.html"
|
||||||
|
},
|
||||||
|
"Upload": {
|
||||||
|
"UploadDirectoryName": "goweb-uploads/",
|
||||||
|
"MaxUploadSize": 10485760
|
||||||
}
|
}
|
||||||
}
|
}
|
2
go.mod
2
go.mod
@ -4,5 +4,5 @@ go 1.20
|
|||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/lib/pq v1.10.9
|
github.com/lib/pq v1.10.9
|
||||||
golang.org/x/crypto v0.11.0
|
golang.org/x/crypto v0.8.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 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
|
||||||
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
|
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
|
||||||
golang.org/x/crypto v0.11.0 h1:6Ewdq3tDic1mg5xRO4milcWCfMVQhI4NkqWWvqejpuA=
|
golang.org/x/crypto v0.8.0 h1:pd9TJtTueMTVQXzk8E2XESSMQDj/U7OUu0PqJqPXQjQ=
|
||||||
golang.org/x/crypto v0.11.0/go.mod h1:xgJhtzW8F9jGdVFWZESrid1U1bjeNy4zgy5cRr/CIio=
|
golang.org/x/crypto v0.8.0/go.mod h1:mRqEX+O9/h5TFCrQhkgjo2yKi0yYA+9ecGkdQoHrywE=
|
||||||
|
15
main.go
15
main.go
@ -8,6 +8,7 @@ import (
|
|||||||
"GoWeb/routes"
|
"GoWeb/routes"
|
||||||
"context"
|
"context"
|
||||||
"embed"
|
"embed"
|
||||||
|
"errors"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
@ -43,8 +44,16 @@ func main() {
|
|||||||
file, err := os.OpenFile("logs/"+time.Now().Format("2006-01-02")+".log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
|
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)
|
log.SetOutput(file)
|
||||||
|
|
||||||
|
// Create upload directory if it doesn't exist
|
||||||
|
uploadPath := appLoaded.Config.Upload.BaseName
|
||||||
|
if _, err := os.Stat(uploadPath); errors.Is(err, os.ErrNotExist) {
|
||||||
|
if err := os.MkdirAll(uploadPath, os.ModePerm); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Connect to database and run migrations
|
// Connect to database and run migrations
|
||||||
appLoaded.Db = database.Connect(&appLoaded)
|
appLoaded.Db = database.ConnectDB(&appLoaded)
|
||||||
if appLoaded.Config.Db.AutoMigrate {
|
if appLoaded.Config.Db.AutoMigrate {
|
||||||
err = models.RunAllMigrations(&appLoaded)
|
err = models.RunAllMigrations(&appLoaded)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -60,8 +69,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Define Routes
|
// Define Routes
|
||||||
routes.Get(&appLoaded)
|
routes.GetRoutes(&appLoaded)
|
||||||
routes.Post(&appLoaded)
|
routes.PostRoutes(&appLoaded)
|
||||||
|
|
||||||
// Start server
|
// Start server
|
||||||
server := &http.Server{Addr: appLoaded.Config.Listen.Ip + ":" + appLoaded.Config.Listen.Port}
|
server := &http.Server{Addr: appLoaded.Config.Listen.Ip + ":" + appLoaded.Config.Listen.Port}
|
||||||
|
@ -1,21 +0,0 @@
|
|||||||
package middleware
|
|
||||||
|
|
||||||
import (
|
|
||||||
"GoWeb/security"
|
|
||||||
"log"
|
|
||||||
"net/http"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Csrf validates the CSRF token and returns the handler function if it succeded
|
|
||||||
func Csrf(f func(w http.ResponseWriter, r *http.Request)) func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
_, err := security.VerifyCsrfToken(r)
|
|
||||||
if err != nil {
|
|
||||||
log.Println("Error verifying csrf token")
|
|
||||||
http.Error(w, "Forbidden", http.StatusForbidden)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
f(w, r)
|
|
||||||
}
|
|
||||||
}
|
|
@ -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)
|
|
@ -1,14 +0,0 @@
|
|||||||
package middleware
|
|
||||||
|
|
||||||
import "net/http"
|
|
||||||
|
|
||||||
// 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
|
|
||||||
func ProcessGroup(f func(w http.ResponseWriter, r *http.Request), m []MiddlewareFunc) func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
for _, middleware := range m {
|
|
||||||
_ = middleware(f)
|
|
||||||
}
|
|
||||||
|
|
||||||
return f
|
|
||||||
}
|
|
@ -78,12 +78,14 @@ func GetSessionByAuthToken(app *app.App, authToken string) (Session, error) {
|
|||||||
|
|
||||||
// Generates a random 64-byte string
|
// Generates a random 64-byte string
|
||||||
func generateAuthToken(app *app.App) string {
|
func generateAuthToken(app *app.App) string {
|
||||||
|
// Generate random bytes
|
||||||
b := make([]byte, 64)
|
b := make([]byte, 64)
|
||||||
_, err := rand.Read(b)
|
_, err := rand.Read(b)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Error generating random bytes")
|
log.Println("Error generating random bytes")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Convert random bytes to hex string
|
||||||
return hex.EncodeToString(b)
|
return hex.EncodeToString(b)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -127,6 +129,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.App, w http.ResponseWriter, authToken string) error {
|
||||||
|
// Delete session from database
|
||||||
_, err := app.Db.Exec(deleteSessionByAuthToken, authToken)
|
_, err := app.Db.Exec(deleteSessionByAuthToken, authToken)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Error deleting session from database")
|
log.Println("Error deleting session from database")
|
||||||
|
@ -49,6 +49,7 @@ func GetCurrentUser(app *app.App, r *http.Request) (User, error) {
|
|||||||
func GetUserById(app *app.App, id int64) (User, error) {
|
func GetUserById(app *app.App, id int64) (User, error) {
|
||||||
user := User{}
|
user := User{}
|
||||||
|
|
||||||
|
// Query row by id
|
||||||
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)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Get user error (user not found) for user id:" + strconv.FormatInt(id, 10))
|
log.Println("Get user error (user not found) for user id:" + strconv.FormatInt(id, 10))
|
||||||
@ -62,6 +63,7 @@ func GetUserById(app *app.App, id int64) (User, error) {
|
|||||||
func GetUserByUsername(app *app.App, username string) (User, error) {
|
func GetUserByUsername(app *app.App, username string) (User, error) {
|
||||||
user := User{}
|
user := User{}
|
||||||
|
|
||||||
|
// Query row by username
|
||||||
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)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Get user error (user not found) for user:" + username)
|
log.Println("Get user error (user not found) for user:" + username)
|
||||||
@ -73,6 +75,7 @@ func GetUserByUsername(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.App, username string, password string, createdAt time.Time, updatedAt time.Time) (User, error) {
|
||||||
|
// Hash password
|
||||||
hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Error hashing password when creating user")
|
log.Println("Error hashing password when creating user")
|
||||||
@ -94,12 +97,14 @@ func CreateUser(app *app.App, username string, password string, createdAt time.T
|
|||||||
func AuthenticateUser(app *app.App, 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
|
var user User
|
||||||
|
|
||||||
|
// Query row by username
|
||||||
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)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Authentication error (user not found) for user:" + username)
|
log.Println("Authentication error (user not found) for user:" + username)
|
||||||
return Session{}, err
|
return Session{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Validate password
|
||||||
err = bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(password))
|
err = bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(password))
|
||||||
if err != nil { // Failed to validate password, doesn't match
|
if err != nil { // Failed to validate password, doesn't match
|
||||||
log.Println("Authentication error (incorrect password) for user:" + username)
|
log.Println("Authentication error (incorrect password) for user:" + username)
|
||||||
@ -111,12 +116,14 @@ 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.App, w http.ResponseWriter, r *http.Request) {
|
||||||
|
// Get cookie from request
|
||||||
cookie, err := r.Cookie("session")
|
cookie, err := r.Cookie("session")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Error getting cookie from request")
|
log.Println("Error getting cookie from request")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Set token to empty string
|
||||||
err = DeleteSessionByAuthToken(app, w, cookie.Value)
|
err = DeleteSessionByAuthToken(app, w, cookie.Value)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Error deleting session by AuthToken")
|
log.Println("Error deleting session by AuthToken")
|
||||||
|
@ -1,57 +0,0 @@
|
|||||||
package restclient
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"encoding/json"
|
|
||||||
"mime/multipart"
|
|
||||||
"net/http"
|
|
||||||
)
|
|
||||||
|
|
||||||
// SendRequest sends an HTTP request to a URL and includes the specified headers and body.
|
|
||||||
// A body can be nil for GET requests, a map[string]string for multipart/form-data requests,
|
|
||||||
// or a struct for JSON requests
|
|
||||||
func SendRequest(url string, method string, headers map[string]string, body interface{}) (http.Response, error) {
|
|
||||||
var reqBody *bytes.Buffer
|
|
||||||
var contentType string
|
|
||||||
|
|
||||||
switch v := body.(type) {
|
|
||||||
case nil:
|
|
||||||
reqBody = bytes.NewBuffer([]byte(""))
|
|
||||||
case map[string]string:
|
|
||||||
reqBody = &bytes.Buffer{}
|
|
||||||
writer := multipart.NewWriter(reqBody)
|
|
||||||
for key, value := range v {
|
|
||||||
writer.WriteField(key, value)
|
|
||||||
}
|
|
||||||
writer.Close()
|
|
||||||
contentType = writer.FormDataContentType()
|
|
||||||
default:
|
|
||||||
jsonBody, err := json.Marshal(body)
|
|
||||||
if err != nil {
|
|
||||||
return http.Response{}, err
|
|
||||||
}
|
|
||||||
reqBody = bytes.NewBuffer(jsonBody)
|
|
||||||
contentType = "application/json"
|
|
||||||
}
|
|
||||||
|
|
||||||
req, err := http.NewRequest(method, url, reqBody)
|
|
||||||
if err != nil {
|
|
||||||
return http.Response{}, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if contentType != "" {
|
|
||||||
req.Header.Set("Content-Type", contentType)
|
|
||||||
}
|
|
||||||
|
|
||||||
for key, value := range headers {
|
|
||||||
req.Header.Add(key, value)
|
|
||||||
}
|
|
||||||
|
|
||||||
client := &http.Client{}
|
|
||||||
resp, err := client.Do(req)
|
|
||||||
if err != nil {
|
|
||||||
return http.Response{}, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return *resp, nil
|
|
||||||
}
|
|
@ -8,10 +8,10 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Get defines all project get routes
|
// GetRoutes defines all project get routes
|
||||||
func Get(app *app.App) {
|
func GetRoutes(app *app.App) {
|
||||||
// Get controller struct initialize
|
// Get controller struct initialize
|
||||||
getController := controllers.Get{
|
getController := controllers.GetController{
|
||||||
App: app,
|
App: app,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -30,4 +30,7 @@ func Get(app *app.App) {
|
|||||||
http.HandleFunc("/login", getController.ShowLogin)
|
http.HandleFunc("/login", getController.ShowLogin)
|
||||||
http.HandleFunc("/register", getController.ShowRegister)
|
http.HandleFunc("/register", getController.ShowRegister)
|
||||||
http.HandleFunc("/logout", getController.Logout)
|
http.HandleFunc("/logout", getController.Logout)
|
||||||
|
|
||||||
|
// Files
|
||||||
|
http.HandleFunc("/uploads", getController.ShowFile)
|
||||||
}
|
}
|
@ -1,20 +0,0 @@
|
|||||||
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("/register-handle", middleware.Csrf(postController.Register))
|
|
||||||
http.HandleFunc("/login-handle", middleware.Csrf(postController.Login))
|
|
||||||
}
|
|
20
routes/postRoutes.go
Normal file
20
routes/postRoutes.go
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
package routes
|
||||||
|
|
||||||
|
import (
|
||||||
|
"GoWeb/app"
|
||||||
|
"GoWeb/controllers"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
// PostRoutes defines all project post routes
|
||||||
|
func PostRoutes(app *app.App) {
|
||||||
|
// Post controller struct initialize
|
||||||
|
postController := controllers.PostController{
|
||||||
|
App: app,
|
||||||
|
}
|
||||||
|
|
||||||
|
// User authentication
|
||||||
|
http.HandleFunc("/register-handle", postController.Register)
|
||||||
|
http.HandleFunc("/login-handle", postController.Login)
|
||||||
|
http.HandleFunc("/upload-handle", postController.FileUpload)
|
||||||
|
}
|
@ -10,6 +10,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, _ *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)))
|
buff := make([]byte, int(math.Ceil(float64(64)/2)))
|
||||||
_, err := rand.Read(buff)
|
_, err := rand.Read(buff)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -1,5 +1,25 @@
|
|||||||
{{ define "pageTitle" }}Home{{ end }}
|
{{ define "pageTitle" }}Home{{ end }}
|
||||||
|
|
||||||
|
{{ define "file-upload" }}
|
||||||
|
<form
|
||||||
|
enctype="multipart/form-data"
|
||||||
|
action="/upload-handle"
|
||||||
|
method="post"
|
||||||
|
>
|
||||||
|
<input type="file" accept="*/*" name="file" />
|
||||||
|
<input type="submit" value="upload" />
|
||||||
|
</form>
|
||||||
|
{{ end }}
|
||||||
|
|
||||||
{{ define "content" }}
|
{{ define "content" }}
|
||||||
{{ .Test }}
|
{{ .Test }}
|
||||||
|
|
||||||
|
<!-- Uncomment below to demo file upload system -->
|
||||||
|
|
||||||
|
<!-- {{ template "file-upload" . }} -->
|
||||||
|
<!-- <p>Upload an image called test.jpg to test the file upload system</p> -->
|
||||||
|
<!-- <img src="/uploads?name=test.jpg" alt=""> -->
|
||||||
|
{{ end }}
|
||||||
|
|
||||||
|
{{ define "content" }}
|
||||||
{{ end }}
|
{{ end }}
|
Reference in New Issue
Block a user