Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(config): add site_url config option #967

Merged
merged 2 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ update-conf:
go generate ./internal/config

update-conf-docs:
go run ./internal/config/meta/gen --format markdown --locale zh-cn -o ./docs/docs/guide/env.md
go run ./internal/config/meta/gen --format markdown --locale zh-CN -o ./docs/docs/guide/env.md

update-swagger:
go install github.com/swaggo/swag/cmd/swag@latest
Expand Down
3 changes: 2 additions & 1 deletion README.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,10 @@ docker run -d \
--name artalk \
-p 8080:23366 \
-v $(pwd)/data:/data \
-e "TZ=America/New_York" \
-e "ATK_LOCALE=en" \
-e "ATK_SITE_DEFAULT=Artalk Blog" \
-e "ATK_TRUSTED_DOMAINS=https://your_domain" \
-e "ATK_SITE_URL=https://example.com" \
artalk/artalk-go
```

Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,10 @@ docker run -d \
--name artalk \
-p 8080:23366 \
-v $(pwd)/data:/data \
-e "TZ=Asia/Shanghai" \
-e "ATK_LOCALE=zh-CN" \
-e "ATK_SITE_DEFAULT=Artalk 的博客" \
-e "ATK_TRUSTED_DOMAINS=https://your_domain" \
-e "ATK_SITE_URL=https://example.com" \
artalk/artalk-go
```

Expand Down
1 change: 1 addition & 0 deletions conf/artalk.example.simple.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ debug: false
locale: en
timezone: Asia/Shanghai
site_default: Default Site
site_url: ""
login_timeout: 259200
db:
type: sqlite
Expand Down
3 changes: 3 additions & 0 deletions conf/artalk.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ timezone: Asia/Shanghai
# Default site name (create when app is first launched)
site_default: Default Site

# Default site url
site_url: ""

# Login timeout (in seconds)
login_timeout: 259200

Expand Down
3 changes: 3 additions & 0 deletions conf/artalk.example.zh-CN.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ timezone: Asia/Shanghai
# 默认站点名
site_default: 默认站点

# 默认站点地址
site_url: ""

# 登录有效时长 (单位:秒)
login_timeout: 259200

Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ services:
- TZ=Asia/Shanghai
- ATK_LOCALE=zh-CN
- ATK_SITE_DEFAULT=Artalk 的博客
- ATK_TRUSTED_DOMAINS=https://your_domain
- ATK_SITE_URL=https://example.com
4 changes: 3 additions & 1 deletion docs/docs/guide/backend/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,9 @@ trusted_domains:

在侧边栏 [控制中心](../frontend/sidebar.md#控制中心) 的「站点」选项卡 - 选择站点「修改 URL」,填入站点 URL 也具有相同的设置效果;添加多个 URL 可使用 `,` 英文逗号分隔,修改后请手动重启 Artalk。

另外,你也可以在启动时通过环境变量 `TRUSTED_DOMAINS` 来配置可信域名,例如:
默认站点地址 `ATK_SITE_URL` 配置项也会被自动加入可信域名列表。

另外,你也可以在启动时通过环境变量 `ATK_TRUSTED_DOMAINS` 来配置可信域名,例如:

```bash
ATK_TRUSTED_DOMAINS="https://a.com https://b.org" artalk server
Expand Down
15 changes: 8 additions & 7 deletions docs/docs/guide/deploy.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ docker run -d \
--name artalk \
-p 8080:23366 \
-v $(pwd)/data:/data \
-e "TZ=Asia/Shanghai" \
-e "ATK_LOCALE=zh-CN" \
-e "ATK_SITE_DEFAULT=Artalk 的博客" \
-e "ATK_TRUSTED_DOMAINS=https://your_domain" \
-e "ATK_SITE_URL=https://example.com" \
artalk/artalk-go
```

Expand All @@ -21,18 +22,18 @@ docker run -d \
docker exec -it artalk artalk admin
```

浏览器输入 `http://your_domain:8080` 进入 Artalk 后台登录界面。
浏览器输入 `http://artalk.example.com:8080` 进入 Artalk 后台登录界面。

在网页中引入 Artalk 程序内嵌的的前端 JS 和 CSS 资源并初始化 Artalk:

<!-- prettier-ignore-start -->

```html
<!-- CSS -->
<link href="http://your_domain:8080/dist/Artalk.css" rel="stylesheet" />
<link href="http://artalk.example.com:8080/dist/Artalk.css" rel="stylesheet" />

<!-- JS -->
<script src="http://your_domain:8080/dist/Artalk.js"></script>
<script src="http://artalk.example.com:8080/dist/Artalk.js"></script>

<!-- Artalk -->
<div id="Comments"></div>
Expand All @@ -41,7 +42,7 @@ Artalk.init({
el: '#Comments', // 绑定元素的 Selector
pageKey: '/post/1', // 固定链接
pageTitle: '关于引入 Artalk 的这档子事', // 页面标题 (留空自动获取)
server: 'http://your_domain:8080', // 后端地址
server: 'http://artalk.example.com:8080', // 后端地址
site: 'Artalk 的博客', // 你的站点名
})
</script>
Expand All @@ -62,7 +63,7 @@ Artalk.init({
4. 配置

```js
Artalk.init({ server: 'http://your_domain:23366' })
Artalk.init({ server: 'http://artalk.example.com:23366' })
```

进阶操作:
Expand Down Expand Up @@ -90,7 +91,7 @@ services:
- TZ=Asia/Shanghai
- ATK_LOCALE=zh-CN
- ATK_SITE_DEFAULT=Artalk 的博客
- ATK_TRUSTED_DOMAINS=https://your_domain
- ATK_SITE_URL=https://your_domain
```

创建容器:
Expand Down
8 changes: 6 additions & 2 deletions docs/docs/guide/env.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@ services:
volumes:
- ./data:/data
environment:
- TZ=Asia/Shanghai
- ATK_LOCALE=zh-CN
- ATK_TRUSTED_DOMAINS=https://your_domain https://your_domain2
- ATK_SITE_DEFAULT=Artalk 的博客
- ATK_SITE_URL=https://example.com
- ATK_TRUSTED_DOMAINS=https://dev.example.com https://localhost:8080
- ATK_ADMIN_USERS_0_NAME=admin
- ATK_ADMIN_USERS_0_EMAIL=admin@example.org
- ATK_ADMIN_USERS_0_PASSWORD=(bcrypt)$2y$10$ti4vZYIrxVN8rLcYXVgXCO.GJND0dyI49r7IoF3xqIx8bBRmIBZRm
Expand Down Expand Up @@ -75,6 +78,7 @@ ATK_TRUSTED_DOMAINS_0="https://a.com"
| **ATK_LOGIN_TIMEOUT** | `259200` | 登录有效时长 (单位:秒) | login_timeout (登录有效时长) |
| **ATK_PORT** | `23366` | 服务器端口 | port (服务器端口) |
| **ATK_SITE_DEFAULT** | `"默认站点"` | 默认站点名 | site_default (默认站点名) |
| **ATK_SITE_URL** | `""` | 默认站点地址 | site_url (默认站点地址) |
| **ATK_TIMEZONE** | `"Asia/Shanghai"` | 时间区域 | timezone (时间区域) |
| **ATK_TRUSTED_DOMAINS** | `[]` | 可信域名 | trusted_domains (可信域名) |

Expand Down Expand Up @@ -115,7 +119,7 @@ ATK_TRUSTED_DOMAINS_0="https://a.com"

| 环境变量 | 默认值 | 描述 | 路径 |
| --- | --- | --- | --- |
| **ATK_AUTH_ANONYMOUS** | `true` | 允许匿名评论 (允许跳过验证,仅填写匿名的昵称和邮箱) | auth.anonymous (社交登录 > 允许匿名评论) |
| **ATK_AUTH_ANONYMOUS** | `false` | 允许匿名评论 (允许跳过验证,仅填写匿名的昵称和邮箱) | auth.anonymous (社交登录 > 允许匿名评论) |
| **ATK_AUTH_APPLE_CLIENT_ID** | `""` | ClientId | auth.apple.client_id (社交登录 > Apple > ClientId) |
| **ATK_AUTH_APPLE_CLIENT_SECRET** | `""` | ClientSecret | auth.apple.client_secret (社交登录 > Apple > ClientSecret) |
| **ATK_AUTH_APPLE_ENABLED** | `false` | 启用 | auth.apple.enabled (社交登录 > Apple > Enabled) |
Expand Down
4 changes: 2 additions & 2 deletions internal/config/cache.go

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type Config struct {
TrustedDomains []string `koanf:"trusted_domains" json:"trusted_domains"` // 可信任的域名 (新)
SSL SSLConf `koanf:"ssl" json:"ssl"` // SSL
SiteDefault string `koanf:"site_default" json:"site_default"` // 默认站点名(当请求无指定 site_name 时使用)
SiteURL string `koanf:"site_url" json:"site_url"` // 默认站点 URL
AdminUsers []AdminUserConf `koanf:"admin_users" json:"admin_users"` // 管理员账户
LoginTimeout int `koanf:"login_timeout" json:"login_timeout"` // 登录超时
Moderator ModeratorConf `koanf:"moderator" json:"moderator"` // 评论审查
Expand Down
13 changes: 11 additions & 2 deletions internal/core/conf_sync.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
package core

import (
"strconv"

"github.com/ArtalkJS/Artalk/internal/entity"
"github.com/ArtalkJS/Artalk/internal/log"
)

func (app *App) syncFromConf() {
// 初始化默认站点
app.Dao().FindCreateSite(app.Conf().SiteDefault)
// Initialize default site
siteDefault := app.Dao().FindCreateSite(app.Conf().SiteDefault, app.Conf().SiteURL)
if app.Conf().SiteURL != "" && siteDefault.Urls != app.Conf().SiteURL {
siteDefault.Urls = app.Conf().SiteURL
app.Dao().UpdateSite(&siteDefault)
log.Info("Default Site ", strconv.Quote(app.Conf().SiteDefault),
" URL has been updated to: ", strconv.Quote(app.Conf().SiteURL))
}

Check warning on line 18 in internal/core/conf_sync.go

View check run for this annotation

Codecov / codecov/patch

internal/core/conf_sync.go#L14-L18

Added lines #L14 - L18 were not covered by tests

// 导入配置文件的管理员用户
for _, admin := range app.Conf().AdminUsers {
Expand Down
4 changes: 2 additions & 2 deletions internal/dao/query_find_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ func FindCreateAction[T EntityHasIsEmpty](
return result.(T), nil
}

func (dao *Dao) FindCreateSite(siteName string) entity.Site {
func (dao *Dao) FindCreateSite(siteName string, siteURLs string) entity.Site {
r, _ := FindCreateAction(fmt.Sprintf(SiteByNameKey, siteName), func() (entity.Site, error) {
return dao.FindSite(siteName), nil
}, func() (entity.Site, error) {
return dao.NewSite(siteName, ""), nil
return dao.NewSite(siteName, siteURLs), nil
})
return r
}
Expand Down
8 changes: 5 additions & 3 deletions internal/dao/query_find_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,22 @@ func TestFindCreateSite(t *testing.T) {

t.Run("Create New Site", func(t *testing.T) {
siteName := "TestCreateNewSite"
siteURL := "https://artalk.example.com"

result := app.Dao().FindCreateSite(siteName)
result := app.Dao().FindCreateSite(siteName, siteURL)
assert.False(t, result.IsEmpty(), "直接获取创建后的站点数据有问题")
assert.Equal(t, siteName, result.Name)
assert.Equal(t, siteURL, result.Urls)

findSite := app.Dao().FindSite(siteName)
assert.False(t, findSite.IsEmpty(), "找不到创建后的站点")
assert.Equal(t, app.Dao().CookSite(&result), app.Dao().CookSite(&findSite), "创建后的站点数据有问题")
})

t.Run("Find Existed Site", func(t *testing.T) {
result := app.Dao().FindCreateSite("Site A")
result := app.Dao().FindCreateSite("Site A", "https://qwqaq.com")
assert.False(t, result.IsEmpty())
assert.Equal(t, "http://localhost:8080/,https://qwqaq.com", result.Urls)
assert.Equal(t, "http://localhost:8080/,https://qwqaq.com", result.Urls) // not modified
})
}

Expand Down