diff --git a/Dockerfile b/Dockerfile index c5ce7ba..0533462 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,6 +1,6 @@ FROM golang:1.18-alpine as builder - WORKDIR /app + ENV GOPROXY=https://goproxy.cn,direct COPY . . RUN set -evx -o pipefail \ @@ -9,14 +9,27 @@ RUN set -evx -o pipefail \ && rm -rf /var/cache/apk/* \ && go build -ldflags="-s -w" -o busuanzi main.go +FROM node:16-alpine as ts-builder +WORKDIR /app +COPY ./dist . +RUN set -evx -o pipefail \ + && npm install -g pnpm \ + && pnpm install \ + && pnpm run build \ + && rm -rf node_modules \ + && rm -rf pnpm-lock.yaml \ + && rm -rf tsconfig.json FROM alpine:3.16 WORKDIR /app COPY --from=builder /app/busuanzi /app +COPY --from=ts-builder /app /app/dist COPY --from=builder /app/config.yaml /app/config.yaml -COPY --from=builder /app/dist /app/dist COPY --from=builder /app/entrypoint.sh /app +# remove cache +RUN chmod +x /app/entrypoint.sh + EXPOSE 8080 -ENTRYPOINT [ "./entrypoint.sh" ] \ No newline at end of file +ENTRYPOINT [ "./entrypoint.sh" ] \ No newline at end of file diff --git a/README.md b/README.md index 6687fb3..4f12ca1 100644 --- a/README.md +++ b/README.md @@ -1,35 +1,22 @@ -

-
- -
- soxft/busuanzi -
- - License - - - GitHub stars - - - GitHub forks - - - GitHub last commit - -

- -> 自建不蒜子 -> -> 一个基于 Golang + Redis 的简易访问量统计系统 +[![soxft/busuanzi](https://socialify.git.ci/soxft/busuanzi/image?description=1&font=Bitter&forks=1&language=1&logo=https://raw.githubusercontent.com/soxft/busuanzi/main/dist/favicon.svg&name=1&owner=1&pattern=Solid&stargazers=1&theme=Dark)](https://busuanzi.9420.ltd) + +## 自建不蒜子 +> 一个基于 Golang + Redis 的简易访问量统计系统 +> +> A simple visitor statistics system based on Golang + Redis + - 统计站点的 UV, PV - - 统计文章页的 UV, PV + - 统计子页面的 UV, PV ## 安装 -支持多种运行方式: 源码编译运行, Docker 运行. 详见: [Install](https://github.com/soxft/busuanzi/wiki/Install) +支持多种运行方式: 源码编译运行, Docker 运行. 详见: [Install](https://github.com/soxft/busuanzi/wiki/install) + +## 使用方式 -## 配置文档 +支持多种自定义属性, 兼容 pjax 网页, 支持自定义 标签前缀. 详见: [使用文档](https://github.com/soxft/busuanzi/wiki/usage) -支持多种自定义属性, 兼容 pjax 网页, 支持自定义 标签前缀. 详见: [配置文档](https://github.com/soxft/busuanzi/wiki/Helper) +## 其他 +[Yuantuo](https://github.com/yuantuo666) 提供了一个 支持 Web 管理的版本. 可以在 [yuantuo666/busuanzi](https://github.com/yuantuo666/busuanzi) 找到 diff --git a/app/middleware/cors.go b/app/middleware/cors.go index 7ae0820..86e4b62 100644 --- a/app/middleware/cors.go +++ b/app/middleware/cors.go @@ -8,7 +8,7 @@ import ( func Cors() gin.HandlerFunc { return func(c *gin.Context) { c.Header("Access-Control-Allow-Origin", config.Web.Cors) - c.Header("Server", "busuanzi-by-xcsoft/2.7.4") + c.Header("Server", "busuanzi-by-xcsoft/2.7.5") if c.Request.Method == "OPTIONS" { c.Header("Access-Control-Allow-Methods", "GET, POST, HEAD, OPTIONS") c.Header("Access-Control-Allow-Headers", "x-bsz-referer, Authorization") diff --git a/config.yaml b/config.yaml index a2b8550..bcd1357 100644 --- a/config.yaml +++ b/config.yaml @@ -4,7 +4,7 @@ Web: Debug: true # 是否开启debug模式 Log: true # 是否开启日志 Redis: - Address: 127.0.0.1:6379 # redis地址 + Address: redis:6379 # redis地址 Password: Database: 0 Prefix: bsz # redis前缀 diff --git a/config/config.go b/config/config.go index 2dcd364..515e413 100644 --- a/config/config.go +++ b/config/config.go @@ -3,8 +3,9 @@ package config import ( "flag" "gopkg.in/yaml.v2" - "io/ioutil" "log" + "os" + "strconv" ) var ( @@ -19,20 +20,71 @@ var ( DistPath string ) +var defaultConfig = Config{ + Redis: RedisConfig{ + Address: "redis:6379", + Password: "", + Database: 0, + Prefix: "bsz_", + MaxIdle: 10, + MaxActive: 100, + }, + Web: WebConfig{ + Address: "0.0.0.0:8080", + Cors: "*", + Log: true, + Debug: false, + }, + Bsz: BszConfig{ + Expire: 0, + JwtSecret: "bsz", + }, +} + func init() { // get config file path flag.StringVar(&configPath, "c", "config.yaml", "config path") flag.StringVar(&DistPath, "d", "dist", "dist path") flag.Parse() - data, err := ioutil.ReadFile(configPath) - if err != nil { - log.Fatal("Error reading config file:\r\n" + err.Error()) - } + var data []byte + var err error C = &Config{} - err = yaml.Unmarshal(data, C) - if err != nil { - log.Fatal("Error parsing config file:\r\n" + err.Error()) + + if data, err = os.ReadFile(configPath); err == nil { + if err = yaml.Unmarshal(data, C); err != nil { + log.Fatal("Error parsing config file:\r\n" + err.Error()) + } + } else { + log.Println("Error reading config file:\r\n" + err.Error()) + log.Println("Using default config", defaultConfig) + C = &defaultConfig + } + + // READ FROM ENV + if _redisAddr, ok := os.LookupEnv("REDIS_ADDR"); ok { + C.Redis.Address = _redisAddr + } + if _redisPwd, ok := os.LookupEnv("REDIS_PWD"); ok { + C.Redis.Password = _redisPwd + } + if _redisDb, ok := os.LookupEnv("REDIS_DATABASE"); ok { + if _redisDbInt, err := strconv.Atoi(_redisDb); err == nil { + C.Redis.Database = _redisDbInt + } + } + if _log, ok := os.LookupEnv("LOG_ENABLE"); ok { + if _logBool, err := strconv.ParseBool(_log); err == nil { + C.Web.Log = _logBool + } + } + if _debug, ok := os.LookupEnv("DEBUG_ENABLE"); ok { + if _debugBool, err := strconv.ParseBool(_debug); err == nil { + C.Web.Debug = _debugBool + } + } + if _jwt, ok := os.LookupEnv("JWT_SECRET"); ok { + C.Bsz.JwtSecret = _jwt } Redis = C.Redis diff --git a/dist/favicon.svg b/dist/favicon.svg new file mode 100644 index 0000000..9be1f47 --- /dev/null +++ b/dist/favicon.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/dist/index.html b/dist/index.html index cd940aa..fce255d 100644 --- a/dist/index.html +++ b/dist/index.html @@ -5,6 +5,7 @@ 不蒜子 - powered by xcsoft + diff --git a/entrypoint.sh b/entrypoint.sh old mode 100755 new mode 100644 index a7df53d..a285257 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -4,31 +4,15 @@ set -x cd /app || exit -if [ -f "config.yaml" ]; then +if [ ! -f "/app/expose/install.lock" ];then # busuanzi js API address if [ -n "$API_SERVER" ];then sed -i "s/http:\/\/127.0.0.1:8080\/api/$API_SERVER/g" dist/busuanzi.js fi - # redis地址 - if [ -n "$REDIS_ADDR" ];then - sed -i "s/Address: 127.0.0.1:6379/Address: $REDIS_ADDR/g" config.yaml - else - sed -i "s/Address: 127.0.0.1:6379/Address: redis:6379/g" config.yaml - fi - - # redis 密码 - if [ -n "$REDIS_PWD" ];then - sed -i "s/Password:/Password: $REDIS_PWD/g" config.yaml - fi - - # 是否开启日志 - if [ -n "$LOG_ENABLE" ];then - sed -i "s/Log: true/Log: $LOG_ENABLE/g" config.yaml - fi - - mv config.yaml /app/expose/config.yaml mv dist /app/expose/dist + mv config.yaml /app/expose/config.yaml + touch /app/expose/install.lock fi exec /app/busuanzi -c ./expose/config.yaml -d ./expose/dist \ No newline at end of file