Skip to content

Commit

Permalink
Merge branch 'dev' of github.com:soxft/busuanzi into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
soxft committed Jan 6, 2024
2 parents a4ffc71 + 92b4e0c commit bc73ef9
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 59 deletions.
19 changes: 16 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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 \
Expand All @@ -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" ]
ENTRYPOINT [ "./entrypoint.sh" ]
41 changes: 14 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,35 +1,22 @@
<h1 align="center">
<br>
<a href="https://busuanzi.9420.ltd/" alt="logo" ><img src="https://cdn.iirose.cn/bsz/favicon.svg" width="150"/></a>
<br>
soxft/busuanzi
<br>
<a href="http://www.apache.org/licenses/LICENSE-2.0.html">
<img src="https://img.shields.io/github/license/soxft/busuanzi.svg?style=for-the-badge" alt="License">
</a>
<a href="https://github.com/soxft/busuanzi/stargazers">
<img src="https://img.shields.io/github/stars/soxft/busuanzi.svg?style=for-the-badge" alt="GitHub stars">
</a>
<a href="https://github.com/soxft/busuanzi/network/members">
<img src="https://img.shields.io/github/forks/soxft/busuanzi.svg?style=for-the-badge" alt="GitHub forks">
</a>
<a href = "https://github.com/soxft/busuanzi/releases">
<img alt="GitHub last commit" src="https://img.shields.io/github/last-commit/soxft/busuanzi?style=for-the-badge">
</a>
</h1>

> 自建不蒜子
>
> 一个基于 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) 找到
2 changes: 1 addition & 1 deletion app/middleware/cors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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前缀
Expand Down
68 changes: 60 additions & 8 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package config
import (
"flag"
"gopkg.in/yaml.v2"
"io/ioutil"
"log"
"os"
"strconv"
)

var (
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions dist/favicon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions dist/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<title>不蒜子 - powered by xcsoft</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="基于golang的简易页面访问统计">
<link rel="shortcut icon" href="https://raw.githubusercontent.com/soxft/busuanzi/main/dist/favicon.svg">
<link href="https://lf26-cdn-tos.bytecdntp.com/cdn/expire-1-M/bootstrap/4.6.0/css/bootstrap.min.css" type="text/css" rel="stylesheet" />
<link href="https://lf9-cdn-tos.bytecdntp.com/cdn/expire-1-M/highlight.js/11.4.0/styles/atom-one-dark.min.css" type="text/css" rel="stylesheet" />
<script src="https://lf3-cdn-tos.bytecdntp.com/cdn/expire-1-M/highlight.js/11.4.0/highlight.min.js" type="application/javascript"></script>
Expand Down
22 changes: 3 additions & 19 deletions entrypoint.sh
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit bc73ef9

Please sign in to comment.