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

refactor: 手动配置前端地址 #277

Closed
wants to merge 1 commit into from
Closed
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
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,12 @@ echo $RANDOM | sha256sum

打开 [https://tool.lu/uuid](https://tool.lu/uuid) 生成不带 `-` 的 `UUID` 作为 `JWT_KEY`。

## 配置前端地址

项目启动后,请在系统设置中设置前端地址并保存,如:`https://www.example.com`。如果没有配置域名,也可以使用`<protocol>://<ip>:<port>`的形式。

如果不配置前端地址,RSS和邮件通知等功能将无法正确生成原文链接。

# 开发

## 环境
Expand Down
4 changes: 2 additions & 2 deletions backend/handler/comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,9 @@ func (c CommentHandler) AddComment(ctx echo.Context) error {

if err = c.base.db.Save(&comment).Error; err == nil {
go func() {
frontendHost := ctx.QueryParam("frontend_host")
frontendHost := sysConfigVO.FrontendHost
if frontendHost == "" {
frontendHost = ctx.Request().Host // 如果未传递,则使用后端默认的 Host
frontendHost = ctx.Request().Host // 如果未设置,则使用后端默认的 Host
}
if err = c.commentEmailNotification(comment, frontendHost); err != nil {
c.base.log.Error().Msgf("邮件通知失败,原因:%s", err)
Expand Down
12 changes: 10 additions & 2 deletions backend/handler/rss.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,17 @@ func NewRssHandler(injector do.Injector) *RssHandler {
}

func (r RssHandler) GetRss(c echo.Context) error {
frontendHost := c.QueryParam("frontend_host")
var (
sysConfig db.SysConfig
sysConfigVO vo.FullSysConfigVO
)
r.base.db.First(&sysConfig)
_ = json.Unmarshal([]byte(sysConfig.Content), &sysConfigVO)
fmt.Println(sysConfigVO)

frontendHost := sysConfigVO.FrontendHost
if frontendHost == "" {
frontendHost = c.Request().Host // 如果未传递,则使用后端默认的 Host
frontendHost = c.Request().Host // 如果未设置,则使用后端默认的 Host
}
rss, err := r.generateRss(frontendHost)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions backend/vo/sysConfig_vo.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ type FullSysConfigVO struct {
SmtpPort string `json:"smtpPort,omitempty"` //smtp port
SmtpUsername string `json:"smtpUsername,omitempty"` //smtp username
SmtpPassword string `json:"smtpPassword,omitempty"` //smtp password
FrontendHost string `json:"frontendHost,omitempty"` //前端地址
Version string `json:"version,omitempty"`
CommitId string `json:"commitId,omitempty"`
}
2 changes: 1 addition & 1 deletion front/components/CommentBox.vue
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ const doComment = async (token?: string) => {
toast.error("评论字数超过限制长度:" + sysConfig.value.maxCommentLength)
return
}
await useMyFetch(`/comment/add?frontend_host=${encodeURIComponent(window.location.origin)}`, {...state, token:token})
await useMyFetch(`/comment/add`, {...state, token:token})
toast.success("评论成功!")
currentCommentBox.value = ''
state.username = ''
Expand Down
2 changes: 1 addition & 1 deletion front/layouts/default.vue
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ useHead({
rel: 'alternate',
type: 'application/rss+xml',
title: '我的 RSS 订阅',
href: sysConfigVO.rss || `/rss?frontend_host=${encodeURIComponent(window.location.origin)}`,
href: sysConfigVO.rss || `/rss`,
},
],
style: [
Expand Down
4 changes: 4 additions & 0 deletions front/pages/sys/settings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@
<UFormGroup label="smtp密码/授权码" name="smtpPassword" :ui="{label:{base:'font-bold'}}">
<UInput v-model="state.smtpPassword" type="password"/>
</UFormGroup>
<UFormGroup label="前端地址" name="frontendHost" :ui="{label:{base:'font-bold'}}">
<UInput v-model="state.frontendHost" placeholder="https://www.example.com"/>
</UFormGroup>

<UButton class="justify-center" @click="save">保存</UButton>
</div>
Expand Down Expand Up @@ -153,6 +156,7 @@ const state = reactive({
smtpPort: "",
smtpUsername: "",
smtpPassword: "",
frontendHost: "",
})

const reload = async () => {
Expand Down
1 change: 1 addition & 0 deletions front/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export type SysConfigVO = {
smtpPort: string
smtpUsername: string
smtpPassword: string
frontendHost: string
}


Expand Down