From 2e48192a35d600c8d42559c7ad799365925d5467 Mon Sep 17 00:00:00 2001 From: Bagas Aulia Rezki Date: Sat, 27 Apr 2024 15:34:46 +0700 Subject: [PATCH 1/4] Add some tool for docker --- .dockerignore | 8 ++++++++ .env | 4 ++-- .gitignore | 1 + Dockerfile | 27 +++++++++++++++++++++++++++ utils/utils.go | 8 +++++--- 5 files changed, 43 insertions(+), 5 deletions(-) create mode 100644 .dockerignore create mode 100644 Dockerfile diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..cacf31c --- /dev/null +++ b/.dockerignore @@ -0,0 +1,8 @@ +staging.bat +staging.sh +uploads +.idea +.env +tmp +log +/public/output.css \ No newline at end of file diff --git a/.env b/.env index 4441a74..c44cab9 100644 --- a/.env +++ b/.env @@ -1,4 +1,4 @@ -SERVER_HOST=localhost +SERVER_HOST=0.0.0.0 SERVER_PORT=8000 DOMAIN=filekeeper.fossy.my.id @@ -7,7 +7,7 @@ CORS_PROTO=https CORS_LIST=filekeeper.fossy.my.id:443,fossy.my.id:443 CORS_METHODS=POST,GET -DB_HOST=127.0.0.1 +DB_HOST=192.168.1.5 DB_PORT=3306 DB_USERNAME=root DB_PASSWORD=test123 diff --git a/.gitignore b/.gitignore index c613e59..dfe0a32 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ /uploads /public/output.css /log +/.idea *_templ.txt *_templ.go \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..bf20708 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,27 @@ +FROM node:current-alpine3.19 AS tailwind + +WORKDIR /src +COPY ./public/input.css ./public/ +COPY tailwind.config.js . +COPY ./view ./view + +RUN npm install -g tailwindcss +RUN npx tailwindcss -i ./public/input.css -o ./public/output.css + +FROM golang:1.22.2-alpine3.19 AS go_builder + +WORKDIR /src +COPY . . +COPY --from=tailwind /src/public/output.css ./public/ + +RUN go install github.com/a-h/templ/cmd/templ@$(go list -m -f '{{ .Version }}' github.com/a-h/templ) +RUN templ generate +RUN go build -o ./tmp/main + +FROM scratch + +WORKDIR /src + +COPY --from=go_builder /src /src + +ENTRYPOINT ["./tmp/main"] diff --git a/utils/utils.go b/utils/utils.go index dd2ad9d..90213ae 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -109,9 +109,11 @@ func Getenv(key string) string { return val } - err := godotenv.Load(".env") - if err != nil { - log.Error("Error loading .env file: %s", err) + if os.Getenv("HOSTNAME") == "" { + err := godotenv.Load(".env") + if err != nil { + log.Error("Error loading .env file: %s", err) + } } val := os.Getenv(key) From dfaf8e999f62fa7e2c051e92bf5200276f2fef40 Mon Sep 17 00:00:00 2001 From: Bagas Aulia Rezki Date: Sat, 27 Apr 2024 15:43:11 +0700 Subject: [PATCH 2/4] Move SMTP credentials to .env file --- .env | 5 +++++ .gitignore | 2 ++ handler/forgotPassword/forgotPassword.go | 4 +++- handler/signup/signup.go | 4 +++- 4 files changed, 13 insertions(+), 2 deletions(-) diff --git a/.env b/.env index c44cab9..84586cd 100644 --- a/.env +++ b/.env @@ -13,5 +13,10 @@ DB_USERNAME=root DB_PASSWORD=test123 DB_NAME=filekeeper +SMTP_HOST=mail.fossy.my.id +SMTP_PORT=25 +SMTP_USER=test@fossy.my.id +SMTP_PASSWORD=Test123456 + SESSION_NAME=Session SESSION_MAX_AGE=604800 \ No newline at end of file diff --git a/.gitignore b/.gitignore index dfe0a32..03dea1d 100644 --- a/.gitignore +++ b/.gitignore @@ -4,5 +4,7 @@ /log /.idea +.env + *_templ.txt *_templ.go \ No newline at end of file diff --git a/handler/forgotPassword/forgotPassword.go b/handler/forgotPassword/forgotPassword.go index f1e42a0..0b5fb77 100644 --- a/handler/forgotPassword/forgotPassword.go +++ b/handler/forgotPassword/forgotPassword.go @@ -15,6 +15,7 @@ import ( forgotPasswordView "github.com/fossyy/filekeeper/view/forgotPassword" "gorm.io/gorm" "net/http" + "strconv" "sync" "time" ) @@ -34,7 +35,8 @@ var UserForgotPassword = make(map[string]string) func init() { log = logger.Logger() ListForgotPassword = make(map[string]*ForgotPassword) - mailServer = email.NewSmtpServer("mail.fossy.my.id", 25, "test@fossy.my.id", "Test123456") + smtpPort, _ := strconv.Atoi(utils.Getenv("SMTP_PORT")) + mailServer = email.NewSmtpServer(utils.Getenv("SMTP_HOST"), smtpPort, utils.Getenv("SMTP_USER"), utils.Getenv("SMTP_PASSWORD")) ticker := time.NewTicker(time.Minute) go func() { for { diff --git a/handler/signup/signup.go b/handler/signup/signup.go index 92cebb6..3229cd8 100644 --- a/handler/signup/signup.go +++ b/handler/signup/signup.go @@ -16,6 +16,7 @@ import ( "github.com/google/uuid" "gorm.io/gorm" "net/http" + "strconv" "sync" "time" ) @@ -34,7 +35,8 @@ var VerifyEmail map[string]string func init() { log = logger.Logger() - mailServer = email.NewSmtpServer("mail.fossy.my.id", 25, "test@fossy.my.id", "Test123456") + smtpPort, _ := strconv.Atoi(utils.Getenv("SMTP_PORT")) + mailServer = email.NewSmtpServer(utils.Getenv("SMTP_HOST"), smtpPort, utils.Getenv("SMTP_USER"), utils.Getenv("SMTP_PASSWORD")) VerifyUser = make(map[string]*UnverifiedUser) VerifyEmail = make(map[string]string) From a539e0a2bfbcdcac97c1533306311b3068792844 Mon Sep 17 00:00:00 2001 From: Bagas Aulia Rezki Date: Sat, 27 Apr 2024 15:44:11 +0700 Subject: [PATCH 3/4] Update .env --- .env | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/.env b/.env index 84586cd..a0c431e 100644 --- a/.env +++ b/.env @@ -7,16 +7,16 @@ CORS_PROTO=https CORS_LIST=filekeeper.fossy.my.id:443,fossy.my.id:443 CORS_METHODS=POST,GET -DB_HOST=192.168.1.5 -DB_PORT=3306 -DB_USERNAME=root -DB_PASSWORD=test123 +DB_HOST= +DB_PORT= +DB_USERNAME= +DB_PASSWORD= DB_NAME=filekeeper -SMTP_HOST=mail.fossy.my.id -SMTP_PORT=25 -SMTP_USER=test@fossy.my.id -SMTP_PASSWORD=Test123456 +SMTP_HOST= +SMTP_PORT= +SMTP_USER= +SMTP_PASSWORD= SESSION_NAME=Session -SESSION_MAX_AGE=604800 \ No newline at end of file +SESSION_MAX_AGE=604800 From 5cf1aacf237c74c79314ba99969629c768dea384 Mon Sep 17 00:00:00 2001 From: Bagas Aulia Rezki Date: Sat, 27 Apr 2024 16:07:07 +0700 Subject: [PATCH 4/4] Refactor codebase and removed leftover development code --- .env | 22 ------------------- .idea/.gitignore | 8 ------- .idea/filekeeper.iml | 9 -------- .idea/inspectionProfiles/Project_Default.xml | 10 --------- .idea/modules.xml | 8 ------- .idea/vcs.xml | 6 ----- db/database.go | 10 ++++----- db/model/user/user.go | 5 +++-- handler/download/download.go | 3 ++- handler/download/file/file.go | 7 +++--- handler/error/error.go | 3 ++- handler/forgotPassword/forgotPassword.go | 9 ++++---- handler/index/index.go | 5 +++-- handler/logout/logout.go | 3 ++- handler/misc/misc.go | 12 ---------- handler/signin/signin.go | 5 +++-- handler/signup/signup.go | 9 ++++---- handler/signup/verify/verify.go | 3 ++- .../upload/initialisation/initialisation.go | 9 ++++---- handler/upload/upload.go | 13 ++++++----- handler/user/user.go | 3 ++- main.go | 3 ++- middleware/middleware.go | 5 +++-- routes/routes.go | 3 ++- session/session.go | 3 ++- utils/utils.go | 7 +++--- 26 files changed, 63 insertions(+), 120 deletions(-) delete mode 100644 .env delete mode 100644 .idea/.gitignore delete mode 100644 .idea/filekeeper.iml delete mode 100644 .idea/inspectionProfiles/Project_Default.xml delete mode 100644 .idea/modules.xml delete mode 100644 .idea/vcs.xml diff --git a/.env b/.env deleted file mode 100644 index a0c431e..0000000 --- a/.env +++ /dev/null @@ -1,22 +0,0 @@ -SERVER_HOST=0.0.0.0 -SERVER_PORT=8000 - -DOMAIN=filekeeper.fossy.my.id - -CORS_PROTO=https -CORS_LIST=filekeeper.fossy.my.id:443,fossy.my.id:443 -CORS_METHODS=POST,GET - -DB_HOST= -DB_PORT= -DB_USERNAME= -DB_PASSWORD= -DB_NAME=filekeeper - -SMTP_HOST= -SMTP_PORT= -SMTP_USER= -SMTP_PASSWORD= - -SESSION_NAME=Session -SESSION_MAX_AGE=604800 diff --git a/.idea/.gitignore b/.idea/.gitignore deleted file mode 100644 index 13566b8..0000000 --- a/.idea/.gitignore +++ /dev/null @@ -1,8 +0,0 @@ -# Default ignored files -/shelf/ -/workspace.xml -# Editor-based HTTP Client requests -/httpRequests/ -# Datasource local storage ignored files -/dataSources/ -/dataSources.local.xml diff --git a/.idea/filekeeper.iml b/.idea/filekeeper.iml deleted file mode 100644 index 5e764c4..0000000 --- a/.idea/filekeeper.iml +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - - - - \ No newline at end of file diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml deleted file mode 100644 index eefade7..0000000 --- a/.idea/inspectionProfiles/Project_Default.xml +++ /dev/null @@ -1,10 +0,0 @@ - - - - \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml deleted file mode 100644 index 33fcc00..0000000 --- a/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index 35eb1dd..0000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/db/database.go b/db/database.go index 8123f2e..86510e1 100644 --- a/db/database.go +++ b/db/database.go @@ -2,14 +2,14 @@ package db import ( "fmt" + "os" + "strings" + "github.com/fossyy/filekeeper/logger" "github.com/fossyy/filekeeper/utils" "gorm.io/driver/mysql" - _ "gorm.io/driver/mysql" "gorm.io/gorm" gormLogger "gorm.io/gorm/logger" - "os" - "strings" ) var DB *gorm.DB @@ -23,11 +23,11 @@ func init() { Logger: gormLogger.Default.LogMode(gormLogger.Silent), }) if err != nil { - panic("failed to connect database") + panic("failed to connect database" + err.Error()) } file, err := os.ReadFile("schema.sql") if err != nil { - log.Error("Error opening file: %v", err) + log.Error("Error opening file: %s", err.Error()) } querys := strings.Split(string(file), "\n") for _, query := range querys { diff --git a/db/model/user/user.go b/db/model/user/user.go index bd2240c..6d72a1f 100644 --- a/db/model/user/user.go +++ b/db/model/user/user.go @@ -2,11 +2,12 @@ package user import ( "fmt" + "sync" + "time" + "github.com/fossyy/filekeeper/db" "github.com/fossyy/filekeeper/logger" "github.com/google/uuid" - "sync" - "time" ) type Cache struct { diff --git a/handler/download/download.go b/handler/download/download.go index c612e5b..f53ec36 100644 --- a/handler/download/download.go +++ b/handler/download/download.go @@ -2,6 +2,8 @@ package downloadHandler import ( "errors" + "net/http" + "github.com/fossyy/filekeeper/db" "github.com/fossyy/filekeeper/logger" "github.com/fossyy/filekeeper/middleware" @@ -10,7 +12,6 @@ import ( "github.com/fossyy/filekeeper/types/models" "github.com/fossyy/filekeeper/utils" downloadView "github.com/fossyy/filekeeper/view/download" - "net/http" ) var log *logger.AggregatedLogger diff --git a/handler/download/file/file.go b/handler/download/file/file.go index 29c4464..f258c44 100644 --- a/handler/download/file/file.go +++ b/handler/download/file/file.go @@ -1,12 +1,13 @@ package downloadFileHandler import ( - "github.com/fossyy/filekeeper/db" - "github.com/fossyy/filekeeper/logger" - "github.com/fossyy/filekeeper/types/models" "net/http" "os" "path/filepath" + + "github.com/fossyy/filekeeper/db" + "github.com/fossyy/filekeeper/logger" + "github.com/fossyy/filekeeper/types/models" ) var log *logger.AggregatedLogger diff --git a/handler/error/error.go b/handler/error/error.go index 447a384..7cb4c4e 100644 --- a/handler/error/error.go +++ b/handler/error/error.go @@ -1,9 +1,10 @@ package errorHandler import ( + "net/http" + "github.com/fossyy/filekeeper/logger" errorView "github.com/fossyy/filekeeper/view/error" - "net/http" ) var log *logger.AggregatedLogger diff --git a/handler/forgotPassword/forgotPassword.go b/handler/forgotPassword/forgotPassword.go index 0b5fb77..f11c233 100644 --- a/handler/forgotPassword/forgotPassword.go +++ b/handler/forgotPassword/forgotPassword.go @@ -5,6 +5,11 @@ import ( "context" "errors" "fmt" + "net/http" + "strconv" + "sync" + "time" + "github.com/fossyy/filekeeper/db" "github.com/fossyy/filekeeper/email" "github.com/fossyy/filekeeper/logger" @@ -14,10 +19,6 @@ import ( emailView "github.com/fossyy/filekeeper/view/email" forgotPasswordView "github.com/fossyy/filekeeper/view/forgotPassword" "gorm.io/gorm" - "net/http" - "strconv" - "sync" - "time" ) type ForgotPassword struct { diff --git a/handler/index/index.go b/handler/index/index.go index bc17bb8..f1838bd 100644 --- a/handler/index/index.go +++ b/handler/index/index.go @@ -1,9 +1,10 @@ package indexHandler import ( - "github.com/fossyy/filekeeper/logger" - "github.com/fossyy/filekeeper/view/index" "net/http" + + "github.com/fossyy/filekeeper/logger" + indexView "github.com/fossyy/filekeeper/view/index" ) var log *logger.AggregatedLogger diff --git a/handler/logout/logout.go b/handler/logout/logout.go index 4eda278..181310e 100644 --- a/handler/logout/logout.go +++ b/handler/logout/logout.go @@ -2,11 +2,12 @@ package logoutHandler import ( "errors" + "net/http" + "github.com/fossyy/filekeeper/logger" "github.com/fossyy/filekeeper/session" "github.com/fossyy/filekeeper/types" "github.com/fossyy/filekeeper/utils" - "net/http" ) var log *logger.AggregatedLogger diff --git a/handler/misc/misc.go b/handler/misc/misc.go index 5992897..a4d6241 100644 --- a/handler/misc/misc.go +++ b/handler/misc/misc.go @@ -9,17 +9,5 @@ func Robot(w http.ResponseWriter, r *http.Request) { } func Favicon(w http.ResponseWriter, r *http.Request) { - //currentDir, _ := os.Getwd() - //fmt.Println(currentDir) - //logo := "../../../favicon.ico" - //basePath := filepath.Join(currentDir, "public") - //logoPath := filepath.Join(basePath, logo) - //fmt.Println(filepath.Dir(logoPath)) - //if filepath.Dir(logoPath) != basePath { - // log.Print("invalid logo path", logoPath) - // w.WriteHeader(500) - // return - //} - //http.ServeContent() http.Redirect(w, r, "/public/favicon.ico", http.StatusSeeOther) } diff --git a/handler/signin/signin.go b/handler/signin/signin.go index c6d52e4..ae932f0 100644 --- a/handler/signin/signin.go +++ b/handler/signin/signin.go @@ -2,14 +2,15 @@ package signinHandler import ( "errors" + "net/http" + "strings" + "github.com/fossyy/filekeeper/db/model/user" "github.com/fossyy/filekeeper/logger" "github.com/fossyy/filekeeper/session" "github.com/fossyy/filekeeper/types" "github.com/fossyy/filekeeper/utils" signinView "github.com/fossyy/filekeeper/view/signin" - "net/http" - "strings" ) var log *logger.AggregatedLogger diff --git a/handler/signup/signup.go b/handler/signup/signup.go index 3229cd8..ade664f 100644 --- a/handler/signup/signup.go +++ b/handler/signup/signup.go @@ -5,6 +5,11 @@ import ( "context" "errors" "fmt" + "net/http" + "strconv" + "sync" + "time" + "github.com/fossyy/filekeeper/db" "github.com/fossyy/filekeeper/email" "github.com/fossyy/filekeeper/logger" @@ -15,10 +20,6 @@ import ( signupView "github.com/fossyy/filekeeper/view/signup" "github.com/google/uuid" "gorm.io/gorm" - "net/http" - "strconv" - "sync" - "time" ) type UnverifiedUser struct { diff --git a/handler/signup/verify/verify.go b/handler/signup/verify/verify.go index b5f3d30..01d0af2 100644 --- a/handler/signup/verify/verify.go +++ b/handler/signup/verify/verify.go @@ -1,12 +1,13 @@ package signupVerifyHandler import ( + "net/http" + "github.com/fossyy/filekeeper/db" signupHandler "github.com/fossyy/filekeeper/handler/signup" "github.com/fossyy/filekeeper/logger" "github.com/fossyy/filekeeper/types" signupView "github.com/fossyy/filekeeper/view/signup" - "net/http" ) var log *logger.AggregatedLogger diff --git a/handler/upload/initialisation/initialisation.go b/handler/upload/initialisation/initialisation.go index a5026f1..91d6ac6 100644 --- a/handler/upload/initialisation/initialisation.go +++ b/handler/upload/initialisation/initialisation.go @@ -3,6 +3,11 @@ package initialisation import ( "encoding/json" "errors" + "io" + "net/http" + "os" + "path/filepath" + "github.com/fossyy/filekeeper/db" "github.com/fossyy/filekeeper/logger" "github.com/fossyy/filekeeper/middleware" @@ -11,10 +16,6 @@ import ( "github.com/fossyy/filekeeper/types/models" "github.com/google/uuid" "gorm.io/gorm" - "io" - "net/http" - "os" - "path/filepath" ) var log *logger.AggregatedLogger diff --git a/handler/upload/upload.go b/handler/upload/upload.go index 83f6003..644adea 100644 --- a/handler/upload/upload.go +++ b/handler/upload/upload.go @@ -2,18 +2,19 @@ package uploadHandler import ( "errors" - "github.com/fossyy/filekeeper/db" - "github.com/fossyy/filekeeper/handler/upload/initialisation" - "github.com/fossyy/filekeeper/logger" - "github.com/fossyy/filekeeper/middleware" - "github.com/fossyy/filekeeper/session" - filesView "github.com/fossyy/filekeeper/view/upload" "io" "net/http" "os" "path/filepath" "strconv" "sync" + + "github.com/fossyy/filekeeper/db" + "github.com/fossyy/filekeeper/handler/upload/initialisation" + "github.com/fossyy/filekeeper/logger" + "github.com/fossyy/filekeeper/middleware" + "github.com/fossyy/filekeeper/session" + filesView "github.com/fossyy/filekeeper/view/upload" ) var log *logger.AggregatedLogger diff --git a/handler/user/user.go b/handler/user/user.go index 07ad7f7..82c376e 100644 --- a/handler/user/user.go +++ b/handler/user/user.go @@ -2,11 +2,12 @@ package userHandler import ( "errors" + "net/http" + "github.com/fossyy/filekeeper/logger" "github.com/fossyy/filekeeper/middleware" "github.com/fossyy/filekeeper/session" userView "github.com/fossyy/filekeeper/view/user" - "net/http" ) var log *logger.AggregatedLogger diff --git a/main.go b/main.go index f674f0c..4d81e3d 100644 --- a/main.go +++ b/main.go @@ -2,10 +2,11 @@ package main import ( "fmt" + "net/http" + "github.com/fossyy/filekeeper/middleware" "github.com/fossyy/filekeeper/routes" "github.com/fossyy/filekeeper/utils" - "net/http" ) func main() { diff --git a/middleware/middleware.go b/middleware/middleware.go index dde1de0..fbcb4d8 100644 --- a/middleware/middleware.go +++ b/middleware/middleware.go @@ -3,13 +3,14 @@ package middleware import ( "errors" "fmt" + "net/http" + "strings" + errorHandler "github.com/fossyy/filekeeper/handler/error" "github.com/fossyy/filekeeper/logger" "github.com/fossyy/filekeeper/session" "github.com/fossyy/filekeeper/types" "github.com/fossyy/filekeeper/utils" - "net/http" - "strings" ) var log *logger.AggregatedLogger diff --git a/routes/routes.go b/routes/routes.go index 4c5646c..0b9b1ab 100644 --- a/routes/routes.go +++ b/routes/routes.go @@ -1,6 +1,8 @@ package routes import ( + "net/http" + downloadHandler "github.com/fossyy/filekeeper/handler/download" downloadFileHandler "github.com/fossyy/filekeeper/handler/download/file" forgotPasswordHandler "github.com/fossyy/filekeeper/handler/forgotPassword" @@ -15,7 +17,6 @@ import ( "github.com/fossyy/filekeeper/handler/upload/initialisation" userHandler "github.com/fossyy/filekeeper/handler/user" "github.com/fossyy/filekeeper/middleware" - "net/http" ) func SetupRoutes() *http.ServeMux { diff --git a/session/session.go b/session/session.go index 47a62fe..5f37fbe 100644 --- a/session/session.go +++ b/session/session.go @@ -1,11 +1,12 @@ package session import ( - "github.com/fossyy/filekeeper/utils" "net/http" "strconv" "sync" "time" + + "github.com/fossyy/filekeeper/utils" ) type Session struct { diff --git a/utils/utils.go b/utils/utils.go index 90213ae..fdd905b 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -2,9 +2,6 @@ package utils import ( "fmt" - "github.com/fossyy/filekeeper/logger" - "github.com/joho/godotenv" - "golang.org/x/crypto/bcrypt" "math/rand" "net/http" "os" @@ -12,6 +9,10 @@ import ( "sync" "time" "unicode" + + "github.com/fossyy/filekeeper/logger" + "github.com/joho/godotenv" + "golang.org/x/crypto/bcrypt" ) type Env struct {