From 0a8a66e17af1baac7daf3a730eeb60855efbd8a2 Mon Sep 17 00:00:00 2001 From: penndev Date: Sun, 26 Nov 2023 23:22:29 +0800 Subject: [PATCH] fix dir crate --- .env.sample | 1 + .github/workflows/release.yml | 15 ++++++++------- conf/nginx.conf.default | 1 - main.go | 11 ++++++----- serve/conf/domain.go | 11 ++++++++--- serve/conf/env.go | 1 - serve/util/nginx.go | 6 +++++- serve/util/util.go | 14 ++++++++++++++ 8 files changed, 42 insertions(+), 18 deletions(-) diff --git a/.env.sample b/.env.sample index b8c2b64..6cfb302 100644 --- a/.env.sample +++ b/.env.sample @@ -1,6 +1,7 @@ MODE=prod KEY=123456 LISTEN=:8888 +NGINX_PATH=/usr/bin/nginx CACHE_DIR=/data CACHE_TASK_CYCLE=60 CACHE_DISK_LIMIT=90 diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index f217398..10d429c 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -8,14 +8,11 @@ on: jobs: build: runs-on: ubuntu-latest - strategy: - matrix: - go-version: ['1.21.x'] steps: - - name: Setup Go ${{ matrix.go-version }} + - name: Setup Go uses: actions/setup-go@v2 with: - go-version: ${{ matrix.go-version }} + go-version: '1.21.x' - name: Setup Luajit run: | sudo apt-get update @@ -23,9 +20,13 @@ jobs: - name: Checkout Code uses: actions/checkout@v4 - name: Build Golnag - run: go build + run: | + go version + export CGO_ENABLED=0 + go build - name: Build Luajit run: | + luajit -v folder="./script" for file in $folder/*.lua do @@ -33,7 +34,7 @@ jobs: done - name: Archive Files run: | - tar -czvf wafcdn.tar.gz * + tar -czvf wafcdn.tar.gz wafcdn .env.sample conf script zip -r wafcdn.zip * - name: Release uses: softprops/action-gh-release@v1 diff --git a/conf/nginx.conf.default b/conf/nginx.conf.default index d0a737c..28d9937 100644 --- a/conf/nginx.conf.default +++ b/conf/nginx.conf.default @@ -15,7 +15,6 @@ env UPCACHE_LIMIT_COUNT=10; http { access_log off; - resolver 223.5.5.5 223.6.6.6 valid=300s; lua_package_path "$prefix/script/?.lua;;"; lua_shared_dict ssl 128m; diff --git a/main.go b/main.go index 5142c01..cc821d4 100644 --- a/main.go +++ b/main.go @@ -10,9 +10,10 @@ import ( func main() { conf.LoadEnv(".env") // 加载与校验配置 conf.LoadDomain(conf.DomainFileName) // 加载持久域名配置信息 - orm.LoadCache(".cache.db") // 加载持久缓存sqlite数据。 - orm.CacheInAndLruOutTask() // 启动缓存文件入库和清理。 - util.StartNginx() // 启动nginx(openresty) - util.InitNetTraffic() // 监控系统的流量 - serve.Listen() // http接口 + util.MkdirLogDir("logs") + orm.LoadCache(".cache.db") // 加载持久缓存sqlite数据。 + orm.CacheInAndLruOutTask() // 启动缓存文件入库和清理。 + util.StartNginx() // 启动nginx(openresty) + util.InitNetTraffic() // 监控系统的流量 + serve.Listen() // http接口 } diff --git a/serve/conf/domain.go b/serve/conf/domain.go index 2eb781c..273d5a9 100644 --- a/serve/conf/domain.go +++ b/serve/conf/domain.go @@ -3,6 +3,8 @@ package conf import ( "crypto/md5" "encoding/json" + "fmt" + "io" "os" "time" ) @@ -78,14 +80,17 @@ func GetDomain() map[string]DomainItem { var DomainFileName = ".domain.json" func LoadDomain(domainFile string) { - domainByte, err := os.ReadFile(domainFile) + domainRead, err := os.OpenFile(domainFile, os.O_RDWR|os.O_CREATE, 0755) + if err != nil { + panic(err) + } + domainByte, err := io.ReadAll(domainRead) if err != nil { panic(err) } - // 如果文件不存在怎么办。?待思考 var domainConfigs []DomainItem if err := json.Unmarshal(domainByte, &domainConfigs); err != nil { - panic(err) + fmt.Println(err) } domainMap = make(map[string]DomainItem) for _, domaininfo := range domainConfigs { diff --git a/serve/conf/env.go b/serve/conf/env.go index d30a067..309ab97 100644 --- a/serve/conf/env.go +++ b/serve/conf/env.go @@ -47,5 +47,4 @@ func LoadEnv(f string) { } DocacheLimitStart = docacheLimitStart DocacheLimitCount = docacheLimitCount - } diff --git a/serve/util/nginx.go b/serve/util/nginx.go index 51a69cc..5144e04 100644 --- a/serve/util/nginx.go +++ b/serve/util/nginx.go @@ -61,9 +61,13 @@ func genNginxConfFile() { } func StartNginx() { + // 获取nginx的安装路径。 + nginxPath := os.Getenv("NGINX_PATH") + if nginxPath == "" { + panic("cant find the NGINX_PATH") + } // 生成nginx配置文件。 genNginxConfFile() - var err error var cmd *exec.Cmd _, err = os.Stat("logs/nginx.pid") diff --git a/serve/util/util.go b/serve/util/util.go index 567616f..a454a2c 100644 --- a/serve/util/util.go +++ b/serve/util/util.go @@ -1,7 +1,9 @@ package util import ( + "fmt" "log" + "os" "time" "github.com/penndev/wafcdn/serve/conf" @@ -47,3 +49,15 @@ func InitNetTraffic() { } }() } + +func MkdirLogDir(logDir string) { + if _, err := os.Stat(logDir); os.IsNotExist(err) { + err := os.MkdirAll(logDir, 0755) + if err != nil { + panic(err) + } + fmt.Println("Directory created successfully") + } else if err != nil { + panic(err) + } +}