Compare commits
10 Commits
v1.4.0
...
file_uploa
Author | SHA1 | Date | |
---|---|---|---|
37391190fb | |||
6da7d408f9 | |||
1fb8fdef81 | |||
e993bcf317 | |||
baef0cbe78 | |||
d0da1a9114 | |||
9b231a73d6 | |||
34acd0fa8d | |||
71d3bd77d0 | |||
1451abcca4 |
1
.gitattributes
vendored
Normal file
1
.gitattributes
vendored
Normal file
@ -0,0 +1 @@
|
||||
* text=auto eol=lf
|
@ -15,6 +15,7 @@ fine with getting your hands dirty, but I plan on having it ready to go for more
|
||||
- CSRF protection
|
||||
- Minimal user login/registration + sessions
|
||||
- Config file handling
|
||||
- Scheduled tasks
|
||||
- Entire website compiles into a single binary (~10mb) (excluding env.json)
|
||||
- Minimal dependencies (just standard library, postgres driver, and experimental package for bcrypt)
|
||||
|
||||
|
@ -25,6 +25,11 @@ type Configuration struct {
|
||||
Template struct {
|
||||
BaseName string `json:"BaseTemplateName"`
|
||||
}
|
||||
|
||||
Upload struct {
|
||||
BaseName string `json:"UploadDirectoryName"`
|
||||
MaxSize int64 `json:"MaxUploadSize"`
|
||||
}
|
||||
}
|
||||
|
||||
// LoadConfig loads and returns a configuration struct
|
||||
|
@ -61,6 +61,13 @@ func (getController *GetController) ShowLogin(w http.ResponseWriter, r *http.Req
|
||||
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)
|
||||
|
@ -4,8 +4,11 @@ import (
|
||||
"GoWeb/app"
|
||||
"GoWeb/models"
|
||||
"GoWeb/security"
|
||||
"io"
|
||||
"log"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
@ -69,3 +72,60 @@ func (postController *PostController) Register(w http.ResponseWriter, r *http.Re
|
||||
|
||||
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)
|
||||
}
|
||||
|
@ -13,5 +13,9 @@
|
||||
},
|
||||
"Template": {
|
||||
"BaseTemplateName": "templates/base.html"
|
||||
},
|
||||
"Upload": {
|
||||
"UploadDirectoryName": "goweb-uploads/",
|
||||
"MaxUploadSize": 10485760
|
||||
}
|
||||
}
|
||||
}
|
||||
|
4
go.mod
4
go.mod
@ -3,6 +3,6 @@ module GoWeb
|
||||
go 1.20
|
||||
|
||||
require (
|
||||
github.com/lib/pq v1.10.7
|
||||
golang.org/x/crypto v0.7.0
|
||||
github.com/lib/pq v1.10.9
|
||||
golang.org/x/crypto v0.8.0
|
||||
)
|
||||
|
8
go.sum
8
go.sum
@ -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.7.0 h1:AvwMYaRytfdeVt3u6mLaxYtErKYjxA2OXjJ1HHq6t3A=
|
||||
golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU=
|
||||
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.8.0 h1:pd9TJtTueMTVQXzk8E2XESSMQDj/U7OUu0PqJqPXQjQ=
|
||||
golang.org/x/crypto v0.8.0/go.mod h1:mRqEX+O9/h5TFCrQhkgjo2yKi0yYA+9ecGkdQoHrywE=
|
||||
|
10
main.go
10
main.go
@ -8,6 +8,7 @@ import (
|
||||
"GoWeb/routes"
|
||||
"context"
|
||||
"embed"
|
||||
"errors"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
@ -43,6 +44,14 @@ func main() {
|
||||
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)
|
||||
|
||||
// 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
|
||||
appLoaded.Db = database.ConnectDB(&appLoaded)
|
||||
if appLoaded.Config.Db.AutoMigrate {
|
||||
@ -78,6 +87,7 @@ func main() {
|
||||
signal.Notify(interrupt, os.Interrupt, syscall.SIGTERM)
|
||||
stop := make(chan struct{})
|
||||
go app.RunScheduledTasks(&appLoaded, 100, stop)
|
||||
|
||||
<-interrupt
|
||||
log.Println("Interrupt signal received. Shutting down server...")
|
||||
|
||||
|
@ -64,6 +64,18 @@ func CreateSession(app *app.App, w http.ResponseWriter, userId int64, remember b
|
||||
return session, nil
|
||||
}
|
||||
|
||||
func GetSessionByAuthToken(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)
|
||||
if err != nil {
|
||||
log.Println("Error getting session by auth token")
|
||||
return Session{}, err
|
||||
}
|
||||
|
||||
return session, nil
|
||||
}
|
||||
|
||||
// Generates a random 64-byte string
|
||||
func generateAuthToken(app *app.App) string {
|
||||
// Generate random bytes
|
||||
|
@ -23,10 +23,9 @@ const userColumns = "\"Id\", " + userColumnsNoId
|
||||
const userTable = "public.\"User\""
|
||||
|
||||
const (
|
||||
selectSessionIdByAuthToken = "SELECT \"Id\" FROM public.\"Session\" WHERE \"AuthToken\" = $1"
|
||||
selectUserById = "SELECT " + userColumns + " FROM " + userTable + " WHERE \"Id\" = $1"
|
||||
selectUserByUsername = "SELECT " + userColumns + " FROM " + userTable + " WHERE \"Username\" = $1"
|
||||
insertUser = "INSERT INTO " + userTable + " (" + userColumnsNoId + ") VALUES ($1, $2, $3, $4) RETURNING \"Id\""
|
||||
selectUserById = "SELECT " + userColumns + " FROM " + userTable + " WHERE \"Id\" = $1"
|
||||
selectUserByUsername = "SELECT " + userColumns + " FROM " + userTable + " WHERE \"Username\" = $1"
|
||||
insertUser = "INSERT INTO " + userTable + " (" + userColumnsNoId + ") VALUES ($1, $2, $3, $4) RETURNING \"Id\""
|
||||
)
|
||||
|
||||
// GetCurrentUser finds the currently logged-in user by session cookie
|
||||
@ -37,16 +36,13 @@ func GetCurrentUser(app *app.App, r *http.Request) (User, error) {
|
||||
return User{}, err
|
||||
}
|
||||
|
||||
var userId int64
|
||||
|
||||
// Query row by AuthToken
|
||||
err = app.Db.QueryRow(selectSessionIdByAuthToken, cookie.Value).Scan(&userId)
|
||||
session, err := GetSessionByAuthToken(app, cookie.Value)
|
||||
if err != nil {
|
||||
log.Println("Error querying session row with session: " + cookie.Value)
|
||||
log.Println("Error getting session by auth token")
|
||||
return User{}, err
|
||||
}
|
||||
|
||||
return GetUserById(app, userId)
|
||||
return GetUserById(app, session.UserId)
|
||||
}
|
||||
|
||||
// GetUserById finds a User table row in the database by id and returns a struct representing this row
|
||||
|
@ -30,4 +30,7 @@ func GetRoutes(app *app.App) {
|
||||
http.HandleFunc("/login", getController.ShowLogin)
|
||||
http.HandleFunc("/register", getController.ShowRegister)
|
||||
http.HandleFunc("/logout", getController.Logout)
|
||||
|
||||
// Files
|
||||
http.HandleFunc("/uploads", getController.ShowFile)
|
||||
}
|
||||
|
@ -16,4 +16,5 @@ func PostRoutes(app *app.App) {
|
||||
// User authentication
|
||||
http.HandleFunc("/register-handle", postController.Register)
|
||||
http.HandleFunc("/login-handle", postController.Login)
|
||||
http.HandleFunc("/upload-handle", postController.FileUpload)
|
||||
}
|
||||
|
@ -1,5 +1,25 @@
|
||||
{{ 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" }}
|
||||
{{ .Test }}
|
||||
{{ end }}
|
||||
|
||||
<!-- 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 }}
|
||||
|
Reference in New Issue
Block a user