Skip to content

Commit

Permalink
修改env环境判断方式
Browse files Browse the repository at this point in the history
  • Loading branch information
bbbbbbbbbbbbba committed Jun 2, 2024
1 parent 067289b commit 46e44e8
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 32 deletions.
3 changes: 1 addition & 2 deletions server/bbs-go.docker.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
Env: dev # 环境,线上环境:prod、测试环境:dev
BaseUrl: http://127.0.0.1:3000 # 网站域名
Port: 8082 # 端口
BaseUrl: http://127.0.0.1:3000 # 网站域名
StaticPath: /data/www # 根路径下的静态文件目录,可配置绝对路径
IpDataPath: # IP数据文件,默认读取当前目录下ip2region.xdb文件,最新数据文件请从这里下载:https://github.com/lionsoul2014/ip2region/tree/master/data
AllowedOrigins:
Expand Down
33 changes: 16 additions & 17 deletions server/bbs-go.example.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
Env: prod # 环境,线上环境:prod、测试环境:dev
BaseUrl: https://mlog.club # 网站域名
Port: 8082 # 端口
BaseUrl: https://mlog.club # 网站域名
StaticPath: /data/www # 根路径下的静态文件目录,可配置绝对路径
IpDataPath: # IP数据文件,默认读取当前目录下ip2region.xdb文件,最新数据文件请从这里下载:https://github.com/lionsoul2014/ip2region/tree/master/data
AllowedOrigins:
Expand All @@ -26,27 +25,27 @@ Uploader:
Enable: Oss
# 阿里云oss配置
AliyunOss:
Host: 请配置成你自己的
Bucket: 请配置成你自己的
Endpoint: 请配置成你自己的
AccessId: 请配置成你自己的
AccessSecret: 请配置成你自己的
StyleSplitter: 阿里云oss图片样式分隔符
StyleAvatar: 头像图片样式名称
StylePreview: 预览图片样式名称
StyleSmall: 小图样式名称
StyleDetail: 详情图片样式名称
Host: # 阿里云OSS资源外网访问地址
Bucket: # 阿里云OSS bucket名称
Endpoint: # 阿里云OSS Endpoint
AccessId: # 阿里云OSS accessID
AccessSecret: # 阿里云OSS accessKey
StyleSplitter: # 阿里云oss图片样式分隔符
StyleAvatar: # 头像图片样式名称
StylePreview: # 预览图片样式名称
StyleSmall: # 小图样式名称
StyleDetail: # 详情图片样式名称
# 本地文件上传
Local:
Host: https://st.mlog.club/ # 上传文件域名
Host: https://st.mlog.club/ # 上传文件域名
Path: /data/www/st.mlog.club # 上传目录

# 邮件服务器配置,用于邮件通知
Smtp:
Host: smtp.qq.com
Port: 25
Username: 请配置成你自己的
Password: 请配置成你自己的
Host: # smtp服务地址,请设置成你自己的,例如:smtp.qq.com
Port: # smtp服务端口,请设置成你自己额,例如:25
Username: # 请配置成你自己的
Password: # 请配置成你自己的
SSL: true

# 百度SEO相关配置
Expand Down
6 changes: 0 additions & 6 deletions server/internal/pkg/common/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package common

import (
"bbs-go/internal/models/constants"
"bbs-go/internal/pkg/config"
"bbs-go/internal/pkg/html"
"bbs-go/internal/pkg/markdown"
"bbs-go/internal/pkg/text"
Expand All @@ -11,11 +10,6 @@ import (
"strings"
)

// IsProd 是否是正式环境
func IsProd() bool {
return config.Instance.Env == "prod"
}

func GetSummary(contentType string, content string) (summary string) {
if contentType == constants.ContentTypeMarkdown {
summary = markdown.GetSummary(content, constants.SummaryLen)
Expand Down
9 changes: 8 additions & 1 deletion server/internal/pkg/config/config.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package config

import (
"strings"

"github.com/mlogclub/simple/sqls"
)

var Instance *Config

type Config struct {
Env string // 环境:prod、dev
Env string // 环境
BaseUrl string // base url
Port string // 端口
StaticPath string // 静态文件目录
Expand Down Expand Up @@ -73,3 +75,8 @@ type Config struct {
IndexPath string
}
}

func (c *Config) IsProd() bool {
e := strings.ToLower(c.Env)
return e == "prod" || e == "production"
}
2 changes: 1 addition & 1 deletion server/internal/pkg/sitemap/sitemap.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ var building = false

// Generate
func Generate() {
if config.Instance.Env != "prod" {
if !config.Instance.IsProd() {
return
}
if building {
Expand Down
6 changes: 3 additions & 3 deletions server/internal/pkg/uploader/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ func generateImageKey(data []byte, contentType string) string {
ext = exts[0]
}
}
if config.Instance.Env == "dev" {
return "test/images/" + dates.Format(time.Now(), "2006/01/02/") + md5 + ext
} else {
if config.Instance.IsProd() {
return "images/" + dates.Format(time.Now(), "2006/01/02/") + md5 + ext
} else {
return "test/images/" + dates.Format(time.Now(), "2006/01/02/") + md5 + ext
}
}

Expand Down
5 changes: 3 additions & 2 deletions server/internal/server/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package server

import (
"bbs-go/internal/models"
"bbs-go/internal/pkg/common"
"bbs-go/internal/pkg/config"
"bbs-go/internal/pkg/gormlogs"
"bbs-go/internal/pkg/iplocator"
Expand Down Expand Up @@ -55,6 +54,8 @@ func initConfig() {
panic(fmt.Errorf("fatal error config file: %w", err))
}

config.Instance.Env = env

slog.Info("Load config", slog.String("ENV", env), slog.String("config", jsons.ToJsonStr(config.Instance)))
}

Expand All @@ -71,7 +72,7 @@ func initDB() {
}

func initCron() {
if common.IsProd() {
if config.Instance.IsProd() {
scheduler.Start()
}
}
Expand Down

0 comments on commit 46e44e8

Please sign in to comment.