From 14865975a6539ade2d1469a57f80a0654675c74d Mon Sep 17 00:00:00 2001 From: Firdavs Date: Sun, 24 Mar 2024 12:48:22 +0300 Subject: [PATCH 1/4] [new] Add core initiation and update dependencies --- cmd/server/main.go | 4 ++++ core/installation.go | 35 +++++++++++++++++++++++++++++++++++ go.mod | 13 +++++++++---- go.sum | 2 ++ 4 files changed, 50 insertions(+), 4 deletions(-) create mode 100644 core/installation.go diff --git a/cmd/server/main.go b/cmd/server/main.go index a47379c..6ff15b5 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -3,9 +3,13 @@ package main import ( "github.com/Firdavs9512/qk-server/app/http" "github.com/Firdavs9512/qk-server/config" + "github.com/Firdavs9512/qk-server/core" ) func main() { + // Start installation + core.StartInitiation() + server := http.Server{ Host: config.App.AppHost, Port: config.App.AppPort, diff --git a/core/installation.go b/core/installation.go new file mode 100644 index 0000000..ec91fc3 --- /dev/null +++ b/core/installation.go @@ -0,0 +1,35 @@ +package core + +import ( + "fmt" + + "github.com/Firdavs9512/qk-server/config" + "github.com/fatih/color" +) + +func StartInitiation() { + startText() + authorText() + versionText() +} + +func startText() { + fmt.Print(` + ____ __ __ _____ + / __ \ / //_/ / ___/___ ______ _____ _____ + / / / / / ,< \__ \/ _ \/ ___/ | / / _ \/ ___/ +/ /_/ / / /| | ___/ / __/ / | |/ / __/ / +\___\_\/_/ |_| /____/\___/_/ |___/\___/_/ +`) + fmt.Print("\n") +} + +func authorText() { + red := color.New(color.FgRed).SprintFunc() + + fmt.Printf("Created by: %s\n", red("Firdavs")) +} + +func versionText() { + fmt.Printf("Version: %s\n", config.App.Version) +} diff --git a/go.mod b/go.mod index 3d05a89..f0617c5 100644 --- a/go.mod +++ b/go.mod @@ -2,7 +2,13 @@ module github.com/Firdavs9512/qk-server go 1.22.0 -require github.com/kataras/iris/v12 v12.2.10 +require ( + github.com/fatih/color v1.15.0 + github.com/google/uuid v1.5.0 + github.com/kataras/iris/v12 v12.2.10 + gorm.io/driver/sqlite v1.5.5 + gorm.io/gorm v1.25.8 +) require ( github.com/BurntSushi/toml v1.3.2 // indirect @@ -20,7 +26,6 @@ require ( github.com/gobwas/ws v1.3.2 // indirect github.com/golang/snappy v0.0.4 // indirect github.com/gomarkdown/markdown v0.0.0-20231222211730-1d6d20845b47 // indirect - github.com/google/uuid v1.5.0 // indirect github.com/gorilla/css v1.0.0 // indirect github.com/gorilla/websocket v1.5.1 // indirect github.com/iris-contrib/go.uuid v2.0.0+incompatible // indirect @@ -38,6 +43,8 @@ require ( github.com/kr/pretty v0.3.1 // indirect github.com/mailgun/raymond/v2 v2.0.48 // indirect github.com/mailru/easyjson v0.7.7 // indirect + github.com/mattn/go-colorable v0.1.13 // indirect + github.com/mattn/go-isatty v0.0.19 // indirect github.com/mattn/go-sqlite3 v1.14.22 // indirect github.com/mediocregopher/radix/v3 v3.8.1 // indirect github.com/microcosm-cc/bluemonday v1.0.26 // indirect @@ -63,6 +70,4 @@ require ( google.golang.org/protobuf v1.32.0 // indirect gopkg.in/ini.v1 v1.67.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect - gorm.io/driver/sqlite v1.5.5 // indirect - gorm.io/gorm v1.25.8 // indirect ) diff --git a/go.sum b/go.sum index d5c8e10..da368e4 100644 --- a/go.sum +++ b/go.sum @@ -92,6 +92,7 @@ github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0 github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc= github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= +github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA= github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU= @@ -180,6 +181,7 @@ golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20211019181941-9d821ace8654/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU= golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= From 30d5b7974a36397d61697442fbba75f736fbd8dc Mon Sep 17 00:00:00 2001 From: Firdavs Date: Sun, 24 Mar 2024 12:49:34 +0300 Subject: [PATCH 2/4] [new] Add migration for Settings model --- app/models/Settings.go | 9 +++++++++ config/database.go | 2 +- 2 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 app/models/Settings.go diff --git a/app/models/Settings.go b/app/models/Settings.go new file mode 100644 index 0000000..f70123c --- /dev/null +++ b/app/models/Settings.go @@ -0,0 +1,9 @@ +package models + +import "gorm.io/gorm" + +type Settings struct { + gorm.Model + Key string + Value string +} diff --git a/config/database.go b/config/database.go index a2d689e..aa2ec0b 100644 --- a/config/database.go +++ b/config/database.go @@ -44,7 +44,7 @@ func (d *DatabaseType) Init() { } // Migrate the schema - d.DB.AutoMigrate(&models.Files{}) + d.DB.AutoMigrate(&models.Files{}, &models.Settings{}) fmt.Println("Database connected!") } From b8251bfa629c7c077782e2869ccc991855fa7cf0 Mon Sep 17 00:00:00 2001 From: Firdavs Date: Sun, 24 Mar 2024 13:49:23 +0300 Subject: [PATCH 3/4] [change] Refactor server initialization and file directory initialization --- Makefile | 2 +- app/http/server.go | 35 ++++++++++--------- cmd/server/main.go | 6 +--- core/FileDirectory.go | 2 +- core/configInit.go | 80 +++++++++++++++++++++++++++++++++++++++++++ core/installation.go | 7 ++++ 6 files changed, 108 insertions(+), 24 deletions(-) create mode 100644 core/configInit.go diff --git a/Makefile b/Makefile index 9906c0a..376987c 100644 --- a/Makefile +++ b/Makefile @@ -13,7 +13,7 @@ build: $(GOBUILD) -o $(BINARY_NAME_SERVER) -v cmd/server/main.go test: - $(GOTEST) -v ./... + $(GOTEST) -v ./test/... clean: $(GOCLEAN) diff --git a/app/http/server.go b/app/http/server.go index 48dbe29..e0c430b 100644 --- a/app/http/server.go +++ b/app/http/server.go @@ -5,35 +5,36 @@ import ( "github.com/Firdavs9512/qk-server/app/http/controllers" "github.com/Firdavs9512/qk-server/config" - "github.com/Firdavs9512/qk-server/core" "github.com/kataras/iris/v12" "github.com/kataras/iris/v12/mvc" ) -type Server struct { - Host string - Port int -} +type Server struct{} + +var Application *iris.Application func (s *Server) Start() { // Create a new Iris application - app := iris.Default() - - // Init database - config.Database.Init() - - // Init file directory - core.Init() + Application = iris.Default() // Configure - app.Use(iris.LimitRequestBodySize(config.App.MaxFileSize)) + Application.Use(iris.LimitRequestBodySize(config.App.MaxFileSize)) - app.Get("/", func(ctx iris.Context) { - ctx.JSON(iris.Map{"message": "Hello, World!"}) + Application.Get("/", func(ctx iris.Context) { + ctx.JSON(iris.Map{"message": "Ok!"}) }) - mvc := mvc.New(app.Party("/upload")) + mvc := mvc.New(Application.Party("/upload")) mvc.Handle(new(controllers.FileUploadController)) - app.Listen(fmt.Sprintf("%s:%d", s.Host, s.Port)) + Application.Listen(fmt.Sprintf("%s:%d", config.App.AppHost, config.App.AppPort)) +} + +func RestartServer() { + if Application != nil { + Application.ConfigureHost(func(h *iris.Supervisor) { + // Restart the server + h.Server.Addr = fmt.Sprintf("%s:%d", config.App.AppHost, config.App.AppPort) + }) + } } diff --git a/cmd/server/main.go b/cmd/server/main.go index 6ff15b5..2c535e8 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -2,7 +2,6 @@ package main import ( "github.com/Firdavs9512/qk-server/app/http" - "github.com/Firdavs9512/qk-server/config" "github.com/Firdavs9512/qk-server/core" ) @@ -10,10 +9,7 @@ func main() { // Start installation core.StartInitiation() - server := http.Server{ - Host: config.App.AppHost, - Port: config.App.AppPort, - } + server := http.Server{} server.Start() } diff --git a/core/FileDirectory.go b/core/FileDirectory.go index e9cc908..b3d28cb 100644 --- a/core/FileDirectory.go +++ b/core/FileDirectory.go @@ -7,7 +7,7 @@ import ( "github.com/Firdavs9512/qk-server/config" ) -func Init() { +func InitDirectory() { CreateDirectory(config.App.UploadUrl) } diff --git a/core/configInit.go b/core/configInit.go new file mode 100644 index 0000000..bea6a0c --- /dev/null +++ b/core/configInit.go @@ -0,0 +1,80 @@ +package core + +import ( + "strconv" + + "github.com/Firdavs9512/qk-server/app/http" + "github.com/Firdavs9512/qk-server/app/models" + "github.com/Firdavs9512/qk-server/config" +) + +// Local config files initialization in database +func ConfigInit() { + var appConfig config.AppType + appConfig.Name = config.App.Name + appConfig.Author = config.App.Author + // TODO: Change this to database + appConfig.MaxFileSize = config.App.MaxFileSize + + // Application host + var host *models.Settings + config.Database.DB.Where("key = ?", "app_host").First(&host) + if host == nil { + config.Database.DB.Create(&models.Settings{ + Key: "app_host", + Value: config.App.AppHost, + }) + appConfig.AppHost = config.App.AppHost + } else { + appConfig.AppHost = host.Value + } + + // Application port + var port *models.Settings + config.Database.DB.Where("key = ?", "app_port").First(&port) + if port == nil { + config.Database.DB.Create(&models.Settings{ + Key: "app_port", + Value: strconv.Itoa(config.App.AppPort), + }) + appConfig.AppPort = config.App.AppPort + } else { + number, err := strconv.Atoi(port.Value) + if err != nil { + number = config.App.AppPort + } + appConfig.AppPort = number + } + + // Application version + var version *models.Settings + config.Database.DB.Where("key = ?", "app_version").First(&version) + if version == nil { + config.Database.DB.Create(&models.Settings{ + Key: "app_version", + Value: config.App.Version, + }) + appConfig.Version = config.App.Version + } else { + appConfig.Version = version.Value + } + + // Application Upload URL + var uploadUrl *models.Settings + config.Database.DB.Where("key = ?", "upload_url").First(&uploadUrl) + if uploadUrl == nil { + config.Database.DB.Create(&models.Settings{ + Key: "upload_url", + Value: config.App.UploadUrl, + }) + appConfig.UploadUrl = config.App.UploadUrl + } else { + appConfig.UploadUrl = uploadUrl.Value + } + + // Set the new config + config.App = appConfig + + // Restart http server + http.RestartServer() +} diff --git a/core/installation.go b/core/installation.go index ec91fc3..99d8030 100644 --- a/core/installation.go +++ b/core/installation.go @@ -8,6 +8,13 @@ import ( ) func StartInitiation() { + // Init database + config.Database.Init() + + // Init file directory + InitDirectory() + + // Console texts startText() authorText() versionText() From 2659a192dbe979c815152d64c81eb613a86738e9 Mon Sep 17 00:00:00 2001 From: Firdavs Date: Sun, 24 Mar 2024 13:51:31 +0300 Subject: [PATCH 4/4] [change] Add rtest target to Makefile --- Makefile | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Makefile b/Makefile index 376987c..5a1957f 100644 --- a/Makefile +++ b/Makefile @@ -25,3 +25,6 @@ cli: server: $(GORUN) cmd/server/main.go + +rtest: + $(GOTEST) -v ./test/...