Browse Source

fix: fail to save locations #116

0xJacky 2 years ago
parent
commit
8c5ee48719

+ 5 - 2
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 => {

+ 1 - 1
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')
                         })
                     })
                 })

+ 1 - 0
server/api/template.go

@@ -32,6 +32,7 @@ func GetTemplate(c *gin.Context) {
 						Directive: "index",
 					},
 				},
+				Locations: []*nginx.NgxLocation{},
 			},
 		},
 	}

+ 33 - 33
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),
+	}
 }