实现Web静态文件分发

This commit is contained in:
GoEdgeLab
2020-09-26 11:21:52 +08:00
parent 86229e5a6f
commit afe4156395
14 changed files with 216 additions and 33 deletions

View File

@@ -188,7 +188,21 @@ func (this *CreateAction) RunPost(params struct {
// Web地址
switch params.ServerType {
case serverconfigs.ServerTypeHTTPWeb:
webResp, err := this.RPC().HTTPWebRPC().CreateHTTPWeb(this.AdminContext(), &pb.CreateHTTPWebRequest{Root: params.WebRoot})
var rootJSON []byte
var err error
if len(params.WebRoot) > 0 {
rootConfig := &serverconfigs.HTTPRootConfig{}
rootConfig.IsOn = true
rootConfig.Dir = params.WebRoot
rootConfig.Indexes = []string{"index.html", "index.htm"}
rootJSON, err = json.Marshal(rootConfig)
if err != nil {
this.ErrorPage(err)
return
}
}
webResp, err := this.RPC().HTTPWebRPC().CreateHTTPWeb(this.AdminContext(), &pb.CreateHTTPWebRequest{RootJSON: rootJSON})
if err != nil {
this.ErrorPage(err)
return

View File

@@ -23,21 +23,22 @@ func (this *IndexAction) RunGet(params struct {
return
}
this.Data["webConfig"] = webConfig
this.Data["webId"] = webConfig.Id
this.Data["rootConfig"] = webConfig.Root
this.Show()
}
func (this *IndexAction) RunPost(params struct {
WebId int64
Root string
WebId int64
RootJSON []byte
Must *actions.Must
}) {
_, err := this.RPC().HTTPWebRPC().UpdateHTTPWeb(this.AdminContext(), &pb.UpdateHTTPWebRequest{
WebId: params.WebId,
Root: params.Root,
WebId: params.WebId,
RootJSON: params.RootJSON,
})
if err != nil {
this.ErrorPage(err)

View File

@@ -0,0 +1,32 @@
package web
import (
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/iwind/TeaGo/actions"
)
// 创建首页文件
type CreateIndexAction struct {
actionutils.ParentAction
}
func (this *CreateIndexAction) Init() {
this.Nav("", "", "")
}
func (this *CreateIndexAction) RunGet(params struct{}) {
this.Show()
}
func (this *CreateIndexAction) RunPost(params struct {
Index string
Must *actions.Must
}) {
params.Must.
Field("index", params.Index).
Require("首页文件不能为空")
this.Data["index"] = params.Index
this.Success()
}

View File

@@ -25,7 +25,8 @@ func (this *IndexAction) RunGet(params struct {
return
}
this.Data["webConfig"] = webConfig
this.Data["webId"] = webConfig.Id
this.Data["rootConfig"] = webConfig.Root
this.Show()
}
@@ -33,14 +34,14 @@ func (this *IndexAction) RunGet(params struct {
func (this *IndexAction) RunPost(params struct {
ServerId int64
WebId int64
Root string
RootJSON []byte
Must *actions.Must
}) {
_, err := this.RPC().HTTPWebRPC().UpdateHTTPWeb(this.AdminContext(), &pb.UpdateHTTPWebRequest{
WebId: params.WebId,
Root: params.Root,
WebId: params.WebId,
RootJSON: params.RootJSON,
})
if err != nil {
this.ErrorPage(err)

View File

@@ -13,6 +13,7 @@ func init() {
Helper(serverutils.NewServerHelper()).
Prefix("/servers/server/settings/web").
GetPost("", new(IndexAction)).
GetPost("/createIndex", new(CreateIndexAction)).
EndAll()
})
}

View File

@@ -0,0 +1,18 @@
Vue.component("more-options-tbody", {
data: function () {
return {
isVisible: false
}
},
methods: {
show: function () {
this.isVisible = !this.isVisible
this.$emit("change", this.isVisible)
}
},
template: `<tbody>
<tr>
<td colspan="2"><a href="" @click.prevent="show()"><span v-if="!isVisible">更多选项</span><span v-if="isVisible">收起选项</span><i class="icon angle" :class="{down:!isVisible, up:isVisible}"></i></a></td>
</tr>
</tbody>`
})

View File

@@ -0,0 +1,111 @@
Vue.component("http-web-root-box", {
props: ["v-root-config", "v-is-location"],
data: function () {
let rootConfig = this.vRootConfig
if (rootConfig == null) {
rootConfig = {
isPrior: false,
isOn: true,
dir: "",
indexes: [],
stripPrefix: "",
decodePath: false,
isBreak: false
}
}
if (rootConfig.indexes == null) {
rootConfig.indexes = []
}
return {
rootConfig: rootConfig,
advancedVisible: false
}
},
methods: {
changeAdvancedVisible: function (v) {
this.advancedVisible = v
},
addIndex: function () {
let that = this
teaweb.popup("/servers/server/settings/web/createIndex", {
height: "10em",
callback: function (resp) {
that.rootConfig.indexes.push(resp.data.index)
}
})
},
removeIndex: function (i) {
this.rootConfig.indexes.$remove(i)
}
},
template: `<div>
<input type="hidden" name="rootJSON" :value="JSON.stringify(rootConfig)"/>
<table class="ui table selectable definition">
<prior-checkbox :v-config="rootConfig" v-if="vIsLocation"></prior-checkbox>
<tbody v-show="!vIsLocation || rootConfig.isPrior">
<tr>
<td>是否开启</td>
<td>
<div class="ui checkbox">
<input type="checkbox" v-model="rootConfig.isOn"/>
<label></label>
</div>
</td>
</tr>
<tr>
<td class="title">文档根目录</td>
<td>
<input type="text" name="root" v-model="rootConfig.dir" ref="focus" placeholder="类似于 /home/www"/>
<p class="comment">可以访问此根目录下的静态资源。</p>
</td>
</tr>
</tbody>
<more-options-tbody @change="changeAdvancedVisible" v-if="!vIsLocation || rootConfig.isPrior"></more-options-tbody>
<tbody v-show="(!vIsLocation || rootConfig.isPrior) && advancedVisible">
<tr>
<td>首页文件</td>
<td>
<!-- TODO 支持排序 -->
<div v-if="rootConfig.indexes.length > 0">
<div v-for="(index, i) in rootConfig.indexes" class="ui label tiny">
{{index}} <a href="" title="删除" @click.prevent="removeIndex(i)"><i class="icon remove"></i></a>
</div>
<div class="ui divider"></div>
</div>
<button class="ui button tiny" type="button" @click.prevent="addIndex()">+</button>
<p class="comment">在URL中只有目录没有文件名时默认查找的首页文件。</p>
</td>
</tr>
<tr>
<td>去除URL前缀</td>
<td>
<input type="text" v-model="rootConfig.stripPrefix" placeholder="/PREFIX"/>
<p class="comment">可以把请求的路径部分前缀去除后再查找文件,比如把 <span class="ui label tiny">/web/app/index.html</span> 去除前缀 <span class="ui label tiny">/web</span> 后就变成 <span class="ui label tiny">/app/index.html</span> </p>
</td>
</tr>
<tr>
<td>路径解码</td>
<td>
<div class="ui checkbox">
<input type="checkbox" v-model="rootConfig.decodePath"/>
<label></label>
</div>
<p class="comment">是否对请求路径进行URL解码比如把 <span class="ui label tiny">/Web+App+Browser.html</span> 解码成 <span class="ui label tiny">/Web App Browser.html</span> 再查找文件。</p>
</td>
</tr>
<tr>
<td>是否终止请求</td>
<td>
<div class="ui checkbox">
<input type="checkbox" v-model="rootConfig.isBreak"/>
<label></label>
</div>
<p class="comment">在找不到要访问的文件的情况下是否终止请求并返回404如果选择终止请求则不再尝试反向代理等设置。</p>
</td>
</tr>
</tbody>
</table>
<div class="margin"></div>
</div>`
})

View File

@@ -37,7 +37,10 @@
width: 4px;
}
.left-box.disabled {
opacity: 0.3;
opacity: 0.1;
}
.left-box.disabled:hover {
opacity: 1.0;
}
.left-box.tiny {
top: 10.5em;

File diff suppressed because one or more lines are too long

View File

@@ -49,7 +49,11 @@
}
.left-box.disabled {
opacity: 0.3;
opacity: 0.1;
}
.left-box.disabled:hover {
opacity: 1.0;
}
.left-box.tiny {

View File

@@ -1,5 +1,4 @@
{$layout}
{$template "/left_menu"}
<div class="right-box">
@@ -8,15 +7,8 @@
<div class="right-box tiny">
<form class="ui form" data-tea-success="success" data-tea-action="$">
<input type="hidden" name="webId" :value="webConfig.id"/>
<table class="ui table selectable definition">
<tr>
<td class="title">Web目录</td>
<td>
<input type="text" name="root" v-model="webConfig.root" ref="focus"/>
</td>
</tr>
</table>
<input type="hidden" name="webId" :value="webId"/>
<http-web-root-box :v-root-config="rootConfig" :v-is-location="true"></http-web-root-box>
<submit-btn></submit-btn>
</form>
</div>

View File

@@ -0,0 +1,11 @@
{$layout "layout_popup"}
<h3>添加首页文件</h3>
<form class="ui form" data-tea-action="$" data-tea-success="success">
<table class="ui table selectable definition">
<tr>
<td>首页文件名 *</td>
<td><input type="text" name="index" maxlength="200" ref="focus"/></td>
</tr>
</table>
<submit-btn></submit-btn>
</form>

View File

@@ -0,0 +1,3 @@
Tea.context(function () {
this.success = NotifyPopup
})

View File

@@ -4,16 +4,8 @@
<div class="right-box">
<form class="ui form" data-tea-success="success" data-tea-action="$">
<input type="hidden" name="serverId" :value="serverId"/>
<input type="hidden" name="webId" :value="webConfig.id"/>
<table class="ui table selectable definition">
<tr>
<td class="title">Web目录</td>
<td>
<input type="text" name="root" v-model="webConfig.root" ref="focus"/>
</td>
</tr>
</table>
<input type="hidden" name="webId" :value="webId"/>
<http-web-root-box :v-root-config="rootConfig"></http-web-root-box>
<submit-btn></submit-btn>
</form>
</div>