From 8c5ee4871907ad4bb468768876c9215231b5abb6 Mon Sep 17 00:00:00 2001 From: 0xJacky Date: Mon, 15 May 2023 14:02:46 +0800 Subject: [PATCH] fix: fail to save locations #116 --- frontend/src/views/domain/DomainAdd.vue | 7 ++- frontend/src/views/domain/SiteDuplicate.vue | 2 +- server/api/template.go | 1 + server/internal/nginx/type.go | 66 ++++++++++----------- 4 files changed, 40 insertions(+), 36 deletions(-) diff --git a/frontend/src/views/domain/DomainAdd.vue b/frontend/src/views/domain/DomainAdd.vue index 24d18571..96c4039d 100644 --- a/frontend/src/views/domain/DomainAdd.vue +++ b/frontend/src/views/domain/DomainAdd.vue @@ -5,7 +5,7 @@ import NgxConfigEditor from '@/views/domain/ngx_conf/NgxConfigEditor.vue' import {useGettext} from 'vue3-gettext' import domain from '@/api/domain' import ngx from '@/api/ngx' -import {computed, provide, reactive, ref} from 'vue' +import {computed, onMounted, provide, reactive, ref} from 'vue' import {message} from 'ant-design-vue' import {useRouter} from 'vue-router' @@ -29,7 +29,10 @@ const auto_cert = ref(false) const update = ref(0) -init() + +onMounted(() => { + init() +}) function init() { domain.get_template().then(r => { diff --git a/frontend/src/views/domain/SiteDuplicate.vue b/frontend/src/views/domain/SiteDuplicate.vue index 8daa4644..8add958c 100644 --- a/frontend/src/views/domain/SiteDuplicate.vue +++ b/frontend/src/views/domain/SiteDuplicate.vue @@ -76,7 +76,7 @@ function onSubmit() { }).catch(e => { notification.error({ message: $gettext('Duplicate failed'), - description: $gettext(e.message) + description: $gettext(e?.message ?? 'Server error') }) }) }) diff --git a/server/api/template.go b/server/api/template.go index 804728c5..cab7921e 100644 --- a/server/api/template.go +++ b/server/api/template.go @@ -32,6 +32,7 @@ func GetTemplate(c *gin.Context) { Directive: "index", }, }, + Locations: []*nginx.NgxLocation{}, }, }, } diff --git a/server/internal/nginx/type.go b/server/internal/nginx/type.go index c436c350..5ff5851f 100644 --- a/server/internal/nginx/type.go +++ b/server/internal/nginx/type.go @@ -1,64 +1,64 @@ package nginx import ( - "github.com/tufanbarisyildirim/gonginx" - "path" - "strings" + "github.com/tufanbarisyildirim/gonginx" + "path" + "strings" ) type NgxConfig struct { - FileName string `json:"file_name"` - Name string `json:"name"` - Upstreams []*NgxUpstream `json:"upstreams"` - Servers []*NgxServer `json:"servers"` - Custom string `json:"custom"` - c *gonginx.Config + FileName string `json:"file_name"` + Name string `json:"name"` + Upstreams []*NgxUpstream `json:"upstreams,omitempty"` + Servers []*NgxServer `json:"servers,omitempty"` + Custom string `json:"custom"` + c *gonginx.Config } type NgxServer struct { - Directives []*NgxDirective `json:"directives"` - Locations []*NgxLocation `json:"locations"` - Comments string `json:"comments"` + Directives []*NgxDirective `json:"directives,omitempty"` + Locations []*NgxLocation `json:"locations,omitempty"` + Comments string `json:"comments"` } type NgxUpstream struct { - Name string `json:"name"` - Directives []*NgxDirective `json:"directives"` - Comments string `json:"comments"` + Name string `json:"name"` + Directives []*NgxDirective `json:"directives,omitempty"` + Comments string `json:"comments"` } type NgxDirective struct { - Directive string `json:"directive"` - Params string `json:"params"` - Comments string `json:"comments"` + Directive string `json:"directive"` + Params string `json:"params"` + Comments string `json:"comments"` } type NgxLocation struct { - Path string `json:"path"` - Content string `json:"content"` - Comments string `json:"comments"` + Path string `json:"path"` + Content string `json:"content"` + Comments string `json:"comments"` } func (d *NgxDirective) Orig() string { - return d.Directive + " " + d.Params + return d.Directive + " " + d.Params } func (d *NgxDirective) TrimParams() { - d.Params = strings.TrimRight(strings.TrimSpace(d.Params), ";") - return + d.Params = strings.TrimRight(strings.TrimSpace(d.Params), ";") + return } func NewNgxServer() *NgxServer { - return &NgxServer{ - Locations: make([]*NgxLocation, 0), - Directives: make([]*NgxDirective, 0), - } + return &NgxServer{ + Locations: make([]*NgxLocation, 0), + Directives: make([]*NgxDirective, 0), + } } func NewNgxConfig(filename string) *NgxConfig { - return &NgxConfig{ - FileName: filename, - Upstreams: make([]*NgxUpstream, 0), - Name: path.Base(filename), - } + return &NgxConfig{ + FileName: filename, + Upstreams: make([]*NgxUpstream, 0), + Name: path.Base(filename), + } }