Skip to content

Commit

Permalink
feat: 创建静态、PHP 网站默认增加 404 页面
Browse files Browse the repository at this point in the history
  • Loading branch information
zhengkunwang223 committed Jun 19, 2024
1 parent f17db1d commit c8eb4b5
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 14 deletions.
3 changes: 2 additions & 1 deletion backend/app/service/website.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,8 @@ func (w WebsiteService) CreateWebsite(create request.WebsiteCreate) (err error)
}

if len(create.FtpUser) != 0 && len(create.FtpPassword) != 0 {
itemID, err := NewIFtpService().Create(dto.FtpCreate{User: create.FtpUser, Password: create.FtpPassword, Path: path.Join(global.CONF.System.BaseDir, "1panel/apps/openresty/openresty/www/sites", website.Alias, "index")})
indexDir := path.Join(nginxInstall.GetPath(), "www", "sites", website.Alias, "index")
itemID, err := NewIFtpService().Create(dto.FtpCreate{User: create.FtpUser, Password: create.FtpPassword, Path: indexDir})
if err != nil {
global.LOG.Errorf("create ftp for website failed, err: %v", err)
}
Expand Down
24 changes: 19 additions & 5 deletions backend/app/service/website_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,18 +80,23 @@ func createIndexFile(website *model.Website, runtime *model.Runtime) error {
if err != nil {
return err
}
var (
indexPath string
indexContent string
websiteService = NewIWebsiteService()
indexFolder = path.Join(nginxInstall.GetPath(), "www", "sites", website.Alias, "index")
)

indexFolder := path.Join(constant.AppInstallDir, constant.AppOpenresty, nginxInstall.Name, "www", "sites", website.Alias, "index")
indexPath := ""
indexContent := ""
switch website.Type {
case constant.Static:
indexPath = path.Join(indexFolder, "index.html")
indexContent = string(nginx_conf.Index)
indexHtml, _ := websiteService.GetDefaultHtml("index")
indexContent = indexHtml.Content
case constant.Runtime:
if runtime.Type == constant.RuntimePHP {
indexPath = path.Join(indexFolder, "index.php")
indexContent = string(nginx_conf.IndexPHP)
indexPhp, _ := websiteService.GetDefaultHtml("php")
indexContent = indexPhp.Content
}
}

Expand All @@ -114,6 +119,13 @@ func createIndexFile(website *model.Website, runtime *model.Runtime) error {
if err := fileOp.WriteFile(indexPath, strings.NewReader(indexContent), 0755); err != nil {
return err
}

html404, _ := websiteService.GetDefaultHtml("404")
path404 := path.Join(indexFolder, "404.html")
if err := fileOp.WriteFile(path404, strings.NewReader(html404.Content), 0755); err != nil {
return err
}

return nil
}

Expand Down Expand Up @@ -245,12 +257,14 @@ func configDefaultNginx(website *model.Website, domains []model.WebsiteDomain, a
server.UpdateRootProxy([]string{proxy})
case constant.Static:
server.UpdateRoot(rootIndex)
server.UpdateDirective("error_page", []string{"404", "/404.html"})
case constant.Proxy:
nginxInclude := fmt.Sprintf("/www/sites/%s/proxy/*.conf", website.Alias)
server.UpdateDirective("include", []string{nginxInclude})
case constant.Runtime:
switch runtime.Type {
case constant.RuntimePHP:
server.UpdateDirective("error_page", []string{"404", "/404.html"})
if runtime.Resource == constant.ResourceLocal {
switch runtime.Type {
case constant.RuntimePHP:
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/lang/modules/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2037,8 +2037,9 @@ const message = {
domain404: 'Website page does not exist',
indexHtml: 'Static website default page',
stopHtml: 'Website stop page',
indePhp: 'PHP website default page',
indexPHP: 'PHP website default page',
sslExpireDate: 'Certificate expiration date',
website404Helper: 'Website 404 error page only supports PHP runtime environment websites and static websites',
},
php: {
short_open_tag: 'Short tag support',
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/lang/modules/tw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1896,8 +1896,9 @@ const message = {
domain404: '網站不存在頁面',
indexHtml: '靜態網站預設頁',
stopHtml: '網站停用頁',
indePhp: 'PHP 網站預設頁',
indexPHP: 'PHP 網站預設頁',
sslExpireDate: '憑證過期時間',
website404Helper: '網 404 錯誤頁僅支援 PHP 運行環境網站和靜態網站',
},
php: {
short_open_tag: '短標簽支持',
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/lang/modules/zh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1898,8 +1898,9 @@ const message = {
domain404: '网站不存在页',
indexHtml: '静态网站默认页',
stopHtml: '网站停用页',
indePhp: 'PHP 网站默认页',
indexPHP: 'PHP 网站默认页',
sslExpireDate: '证书过期时间',
website404Helper: '网 404 错误页仅支持 PHP 运行环境网站和静态网站',
},
php: {
short_open_tag: '短标签支持',
Expand Down
13 changes: 8 additions & 5 deletions frontend/src/views/website/website/html/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,22 @@
<el-option :value="'404'" :label="$t('website.website404')"></el-option>
<el-option :value="'domain404'" :label="$t('website.domain404')"></el-option>
<el-option :value="'index'" :label="$t('website.indexHtml')"></el-option>
<el-option :value="'php'" :label="$t('website.indePhp')"></el-option>
<el-option :value="'php'" :label="$t('website.indexPHP')"></el-option>
<el-option :value="'stop'" :label="$t('website.stopHtml')"></el-option>
</el-select>
<div class="mt-1.5">
<el-text v-if="type == '404'" type="info">
{{ $t('website.website404Helper') }}
</el-text>
</div>
<div ref="htmlRef" class="default-html"></div>
</el-col>
</el-row>

<template #footer>
<span class="dialog-footer">
<el-button @click="handleClose" :disabled="loading">{{ $t('commons.button.cancel') }}</el-button>
<el-button type="primary" @click="submit()" :disabled="loading">
{{ $t('commons.button.confirm') }}
{{ $t('commons.button.save') }}
</el-button>
</span>
</template>
Expand Down Expand Up @@ -89,7 +93,6 @@ const submit = async () => {
const content = view.value.state.doc.toString();
await UpdateDefaultHtml({ type: type.value, content: content });
MsgSuccess(i18n.global.t('commons.msg.updateSuccess'));
handleClose();
} catch (error) {
} finally {
loading.value = false;
Expand All @@ -102,6 +105,6 @@ defineExpose({ acceptParams });
.default-html {
width: 100%;
min-height: 300px;
margin-top: 20px;
margin-top: 10px;
}
</style>

0 comments on commit c8eb4b5

Please sign in to comment.