Skip to content

Commit

Permalink
wip: less path configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
ElaBosak233 committed May 29, 2024
1 parent 0a3662d commit 2996b91
Show file tree
Hide file tree
Showing 32 changed files with 266 additions and 206 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ uploads
/build/
/captures/
test.go
/dist/
/dist/
86 changes: 86 additions & 0 deletions config/application.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
{
"gin": {
"cors": {
"allow_methods": ["GET", "POST", "PUT", "DELETE"],
"allow_origins": ["*"]
},
"jwt": {
"expiration": 180
},
"cache": {
"provider": "memory",
"redis": {
"host": "cache",
"port": 6379,
"password": "",
"db": 0
}
},
"host": "0.0.0.0",
"port": 8888
},
"container": {
"provider": "docker",
"entry": "127.0.0.1",
"docker": {
"uri": "unix:///var/run/docker.sock"
},
"k8s": {
"namespace": "default",
"config": {
"path": "./configs/k8s.yml"
}
},
"proxy": {
"enabled": false,
"type": "ws",
"traffic_capture": {
"enabled": false
}
}
},
"db": {
"provider": "sqlite",
"postgres": {
"dbname": "cloudsdale",
"host": "db",
"username": "cloudsdale",
"password": "cloudsdale",
"port": 5432,
"sslmode": "disable"
},
"mysql": {
"dbname": "cloudsdale",
"host": "db",
"username": "cloudsdale",
"password": "cloudsdale",
"port": 3306
},
"sqlite": {
"path": "./db/db.sqlite"
}
},
"email": {
"address": "",
"password": "",
"smtp": {
"host": "",
"port": 0
}
},
"captcha": {
"enabled": false,
"provider": "turnstile",
"turnstile": {
"url": "https://challenges.cloudflare.com/turnstile/v0/siteverify",
"site_key": "",
"secret_key": ""
},
"recaptcha": {
"url:": "https://www.google.com/recaptcha/api/siteverify",
"site_key": "",
"secret_key": "",
"threshold": 0.5
}
}
}
19 changes: 19 additions & 0 deletions config/platform.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"site": {
"description": "Hack for fun not for profit.",
"title": "Cloudsdale"
},
"container": {
"parallel_limit": 1,
"request_limit": 0
},
"user": {
"registration": {
"enabled": true,
"email": {
"domain": [],
"verification": false
}
}
}
}
13 changes: 8 additions & 5 deletions internal/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,10 @@ import (
"html/template"
"net/http"
"os"
"strconv"
)

func init() {
data, _ := files.FS.ReadFile("statics/banner.txt")
data, _ := files.F().ReadFile("statics/banner.txt")
banner := string(data)
t, _ := template.New("cloudsdale").Parse(banner)
_ = t.Execute(os.Stdout, struct {
Expand Down Expand Up @@ -97,13 +96,17 @@ func Run() {
// Frontend resources
r.Use(middleware.Frontend("/"))

s := &http.Server{
Addr: config.AppCfg().Gin.Host + ":" + strconv.Itoa(config.AppCfg().Gin.Port),
srv := &http.Server{
Addr: fmt.Sprintf(
"%s:%d",
config.AppCfg().Gin.Host,
config.AppCfg().Gin.Port,
),
Handler: r,
}
zap.L().Info(fmt.Sprintf("Here's the address! %s:%d", config.AppCfg().Gin.Host, config.AppCfg().Gin.Port))
zap.L().Info("The Cloudsdale service is running! Enjoy your hacking challenges!")
err := s.ListenAndServe()
err := srv.ListenAndServe()
if err != nil {
zap.L().Fatal("Err... It seems that the port for Cloudsdale is not available. Plz try again.")
}
Expand Down
48 changes: 20 additions & 28 deletions internal/app/config/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package config

import (
"github.com/elabosak233/cloudsdale/internal/extension/files"
"github.com/elabosak233/cloudsdale/internal/utils"
"github.com/spf13/viper"
"go.uber.org/zap"
"io"
Expand Down Expand Up @@ -36,10 +37,6 @@ type ApplicationCfg struct {
DB int `yaml:"db" json:"db" mapstructure:"db"`
} `yaml:"redis" json:"redis" mapstructure:"redis"`
} `yaml:"cache" json:"cache" mapstructure:"cache"`
Paths struct {
Assets string `yaml:"assets" json:"assets" mapstructure:"assets"`
Media string `yaml:"media" json:"media" mapstructure:"media"`
} `yaml:"paths" json:"paths" mapstructure:"paths"`
} `yaml:"gin" json:"gin" mapstructure:"gin"`
Email struct {
Address string `yaml:"address" json:"address" mapstructure:"address"`
Expand Down Expand Up @@ -75,7 +72,7 @@ type ApplicationCfg struct {
Sslmode string `yaml:"sslmode" json:"sslmode" mapstructure:"sslmode"`
} `yaml:"postgres" json:"postgres" mapstructure:"postgres"`
SQLite struct {
Filename string `yaml:"filename" json:"filename" mapstructure:"filename"`
Path string `yaml:"path" json:"path" mapstructure:"path"`
} `yaml:"sqlite" json:"sqlite" mapstructure:"sqlite"`
MySQL struct {
Host string `yaml:"host" json:"host" mapstructure:"host"`
Expand All @@ -87,23 +84,24 @@ type ApplicationCfg struct {
} `yaml:"db" json:"db" mapstructure:"db"`
Container struct {
Provider string `yaml:"provider" json:"provider" mapstructure:"provider"`
Entry string `yaml:"entry" json:"entry" mapstructure:"entry"`
Docker struct {
URI string `yaml:"uri" json:"uri" mapstructure:"uri"`
Entry string `yaml:"entry" json:"entry" mapstructure:"entry"`
URI string `yaml:"uri" json:"uri" mapstructure:"uri"`
} `yaml:"docker" json:"docker" mapstructure:"docker"`
K8s struct {
NameSpace string `yaml:"namespace" json:"namespace" mapstructure:"namespace"`
Config string `yaml:"config" json:"config" mapstructure:"config"`
Entry string `yaml:"entry" json:"entry" mapstructure:"entry"`
Config struct {
Path string `yaml:"path" json:"path" mapstructure:"path"`
} `yaml:"config" json:"config" mapstructure:"config"`
} `yaml:"k8s" json:"k8s" mapstructure:"k8s"`
Proxy struct {
Enabled bool `yaml:"enabled" json:"enabled" mapstructure:"enabled"`
Type string `yaml:"type" json:"type" mapstructure:"type"`
Enabled bool `yaml:"enabled" json:"enabled" mapstructure:"enabled"`
Type string `yaml:"type" json:"type" mapstructure:"type"`
TrafficCapture struct {
Enabled bool `yaml:"enabled" json:"enabled" mapstructure:"enabled"`
Path string `yaml:"path" json:"path" mapstructure:"path"`
} `yaml:"traffic_capture" json:"traffic_capture" mapstructure:"traffic_capture"`
} `yaml:"proxy" json:"proxy" mapstructure:"proxy"`
TrafficCapture struct {
Enabled bool `yaml:"enabled" json:"enabled" mapstructure:"enabled"`
Path string `yaml:"path" json:"path" mapstructure:"path"`
} `yaml:"traffic_capture" json:"traffic_capture" mapstructure:"traffic_capture"`
} `yaml:"container" json:"container" mapstructure:"container"`
}

Expand All @@ -113,14 +111,14 @@ func AppCfg() *ApplicationCfg {

func InitApplicationCfg() {
v1 = viper.New()
configFile := path.Join("configs", "application.json")
configFile := path.Join(utils.ConfigsPath, "application.json")
v1.SetConfigType("json")
v1.SetConfigFile(configFile)
if _, err := os.Stat(configFile); err != nil {
zap.L().Warn("No configuration file found, default configuration file will be created.")

// Read default configuration from files
defaultConfig, _err := files.FS.Open("configs/application.json")
defaultConfig, _err := files.F().Open("configs/application.json")
if _err != nil {
zap.L().Error("Unable to read default configuration file.")
return
Expand Down Expand Up @@ -154,22 +152,16 @@ func InitApplicationCfg() {
}

func Mkdirs() {
if AppCfg().Container.TrafficCapture.Enabled {
if _, err := os.Stat(AppCfg().Container.TrafficCapture.Path); err != nil {
if _err := os.MkdirAll(AppCfg().Container.TrafficCapture.Path, os.ModePerm); _err != nil {
if AppCfg().Container.Proxy.TrafficCapture.Enabled {
if _, err := os.Stat(utils.CapturesPath); err != nil {
if _err := os.MkdirAll(utils.CapturesPath, os.ModePerm); _err != nil {
zap.L().Fatal("Unable to create directory for traffic capture.")
}
}
}

if _, err := os.Stat(AppCfg().Gin.Paths.Assets); err != nil {
if _err := os.MkdirAll(AppCfg().Gin.Paths.Assets, os.ModePerm); _err != nil {
zap.L().Fatal("Unable to create directory for assets.")
}
}

if _, err := os.Stat(AppCfg().Gin.Paths.Media); err != nil {
if _err := os.MkdirAll(AppCfg().Gin.Paths.Media, os.ModePerm); _err != nil {
if _, err := os.Stat(utils.MediaPath); err != nil {
if _err := os.MkdirAll(utils.MediaPath, os.ModePerm); _err != nil {
zap.L().Fatal("Unable to create directory for media.")
}
}
Expand Down
7 changes: 3 additions & 4 deletions internal/app/config/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package config

import (
"github.com/elabosak233/cloudsdale/internal/utils"
"github.com/google/uuid"
"os"
)
Expand All @@ -14,11 +15,9 @@ func JwtSecretKey() string {
}

func InitConfig() {
configPath := "configs"
if _, err := os.Stat(configPath); os.IsNotExist(err) {
_ = os.Mkdir(configPath, os.ModePerm)
if _, err := os.Stat(utils.ConfigsPath); os.IsNotExist(err) {
_ = os.Mkdir(utils.ConfigsPath, os.ModePerm)
}

InitApplicationCfg()
InitPlatformCfg()
jwtSecretKey = uuid.NewString()
Expand Down
5 changes: 3 additions & 2 deletions internal/app/config/platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package config

import (
"github.com/elabosak233/cloudsdale/internal/extension/files"
"github.com/elabosak233/cloudsdale/internal/utils"
"github.com/spf13/viper"
"go.uber.org/zap"
"io"
Expand Down Expand Up @@ -36,14 +37,14 @@ func PltCfg() *PlatformCfg {

func InitPlatformCfg() {
v2 = viper.New()
configFile := path.Join("configs", "platform.json")
configFile := path.Join(utils.ConfigsPath, "platform.json")
v2.SetConfigType("json")
v2.SetConfigFile(configFile)
if _, err := os.Stat(configFile); err != nil {
zap.L().Warn("No configuration file found, default configuration file will be created.")

// Read default configuration from files
defaultConfig, _err := files.FS.Open("configs/platform.json")
defaultConfig, _err := files.F().Open("configs/platform.json")
if _err != nil {
zap.L().Error("Unable to read default configuration file.")
return
Expand Down
2 changes: 1 addition & 1 deletion internal/app/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func initDatabaseEngine() {
)
db, err = gorm.Open(mysql.Open(dbInfo), &gorm.Config{})
case "sqlite":
dbInfo = config.AppCfg().DB.SQLite.Filename
dbInfo = config.AppCfg().DB.SQLite.Path
db, err = gorm.Open(sqlite.Open(dbInfo), &gorm.Config{})
}
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions internal/controller/media.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package controller

import (
"github.com/elabosak233/cloudsdale/internal/app/config"
"github.com/elabosak233/cloudsdale/internal/service"
"github.com/elabosak233/cloudsdale/internal/utils"
"github.com/gin-gonic/gin"
"net/http"
"os"
Expand All @@ -25,7 +25,7 @@ func NewMediaController(appService *service.Service) IMediaController {

func (m *MediaController) GetFile(ctx *gin.Context) {
a := ctx.Param("path")
p := path.Join(config.AppCfg().Gin.Paths.Media, a)
p := path.Join(utils.MediaPath, a)
_, err := os.Stat(p)
if os.IsNotExist(err) {
ctx.Status(http.StatusNotFound)
Expand Down
14 changes: 7 additions & 7 deletions internal/controller/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func (c *PodController) Renew(ctx *gin.Context) {
instanceRenewRequest.ID = convertor.ToUintD(ctx.Param("id"), 0)
user := ctx.MustGet("user").(*model.User)
instanceRenewRequest.UserID = user.ID
removedAt, err := c.podService.Renew(instanceRenewRequest)
err = c.podService.Renew(instanceRenewRequest)
if err != nil {
ctx.JSON(http.StatusBadRequest, gin.H{
"code": http.StatusBadRequest,
Expand All @@ -118,8 +118,7 @@ func (c *PodController) Renew(ctx *gin.Context) {
}
cache.C().DeleteByPrefix("pods")
ctx.JSON(http.StatusOK, gin.H{
"code": http.StatusOK,
"removed_at": removedAt,
"code": http.StatusOK,
})
}

Expand All @@ -142,15 +141,16 @@ func (c *PodController) Find(ctx *gin.Context) {
}
value, exist := cache.C().Get(fmt.Sprintf("pods:%s", utils.HashStruct(podFindRequest)))
if !exist {
pods, _ := c.podService.Find(podFindRequest)
pods, total, _ := c.podService.Find(podFindRequest)
value = gin.H{
"code": http.StatusOK,
"data": pods,
"code": http.StatusOK,
"data": pods,
"total": total,
}
cache.C().Set(
fmt.Sprintf("pods:%s", utils.HashStruct(podFindRequest)),
value,
5*time.Minute,
2*time.Minute,
)
}
ctx.JSON(http.StatusOK, value)
Expand Down
2 changes: 1 addition & 1 deletion internal/extension/casbin/casbin.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func InitCasbin() {
&gormadapter.CasbinRule{},
"casbins",
)
cfg, err := files.FS.ReadFile("configs/casbin.conf")
cfg, err := files.F().ReadFile("configs/casbin.conf")
md, _ := model.NewModelFromString(string(cfg))
Enforcer, err = casbin.NewEnforcer(md, adapter)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions internal/extension/container/manager/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func (c *DockerManager) Setup() (nats []*model.Nat, err error) {
for _, binding := range bindings {
entries = append(entries, fmt.Sprintf(
"%s:%d",
config.AppCfg().Container.Docker.Entry,
config.AppCfg().Container.Entry,
convertor.ToIntD(binding.HostPort, 0),
))
}
Expand All @@ -147,7 +147,7 @@ func (c *DockerManager) Setup() (nats []*model.Nat, err error) {
DstPort: convertor.ToIntD(binding.HostPort, 0),
Entry: fmt.Sprintf(
"%s:%d",
config.AppCfg().Container.Docker.Entry,
config.AppCfg().Container.Entry,
convertor.ToIntD(binding.HostPort, 0),
),
})
Expand Down
Loading

0 comments on commit 2996b91

Please sign in to comment.