mirror of
https://github.com/0xJacky/nginx-ui.git
synced 2025-05-12 10:55:51 +02:00
wip: modify config file name
This commit is contained in:
parent
7d4999f83a
commit
f6a5f350ad
2 changed files with 290 additions and 233 deletions
|
@ -4,8 +4,8 @@ import CodeEditor from '@/components/CodeEditor/CodeEditor.vue'
|
||||||
|
|
||||||
import NgxConfigEditor from '@/views/domain/ngx_conf/NgxConfigEditor'
|
import NgxConfigEditor from '@/views/domain/ngx_conf/NgxConfigEditor'
|
||||||
import {useGettext} from 'vue3-gettext'
|
import {useGettext} from 'vue3-gettext'
|
||||||
import {reactive, ref} from 'vue'
|
import {reactive, ref, watch} from 'vue'
|
||||||
import {useRoute} from 'vue-router'
|
import {useRoute, useRouter} from 'vue-router'
|
||||||
import domain from '@/api/domain'
|
import domain from '@/api/domain'
|
||||||
import ngx from '@/api/ngx'
|
import ngx from '@/api/ngx'
|
||||||
import {message} from 'ant-design-vue'
|
import {message} from 'ant-design-vue'
|
||||||
|
@ -14,8 +14,13 @@ import {message} from 'ant-design-vue'
|
||||||
const {$gettext, interpolate} = useGettext()
|
const {$gettext, interpolate} = useGettext()
|
||||||
|
|
||||||
const route = useRoute()
|
const route = useRoute()
|
||||||
|
const router = useRouter()
|
||||||
|
|
||||||
const name = ref(route.params.name.toString())
|
const name = ref(route.params.name.toString())
|
||||||
|
watch(route, () => {
|
||||||
|
name.value = route.params.name.toString()
|
||||||
|
})
|
||||||
|
|
||||||
const update = ref(0)
|
const update = ref(0)
|
||||||
|
|
||||||
const ngx_config = reactive({
|
const ngx_config = reactive({
|
||||||
|
@ -32,6 +37,7 @@ const configText = ref('')
|
||||||
const ok = ref(false)
|
const ok = ref(false)
|
||||||
const advance_mode = ref(false)
|
const advance_mode = ref(false)
|
||||||
const saving = ref(false)
|
const saving = ref(false)
|
||||||
|
const filename = ref('')
|
||||||
|
|
||||||
init()
|
init()
|
||||||
|
|
||||||
|
@ -40,7 +46,7 @@ function handle_response(r: any) {
|
||||||
Object.keys(cert_info_map).forEach(v => {
|
Object.keys(cert_info_map).forEach(v => {
|
||||||
delete cert_info_map[v]
|
delete cert_info_map[v]
|
||||||
})
|
})
|
||||||
|
filename.value = r.name
|
||||||
configText.value = r.config
|
configText.value = r.config
|
||||||
enabled.value = r.enabled
|
enabled.value = r.enabled
|
||||||
auto_cert.value = r.auto_cert
|
auto_cert.value = r.auto_cert
|
||||||
|
@ -85,9 +91,9 @@ const save = async () => {
|
||||||
await build_config()
|
await build_config()
|
||||||
}
|
}
|
||||||
|
|
||||||
domain.save(name.value, {content: configText.value}).then(r => {
|
domain.save(name.value, {name: filename.value, content: configText.value}).then(r => {
|
||||||
handle_response(r)
|
handle_response(r)
|
||||||
|
router.push('/domain/' + filename.value)
|
||||||
message.success($gettext('Saved successfully'))
|
message.success($gettext('Saved successfully'))
|
||||||
|
|
||||||
}).catch((e: any) => {
|
}).catch((e: any) => {
|
||||||
|
@ -159,6 +165,9 @@ function on_change_enabled(checked: boolean) {
|
||||||
<a-form-item :label="$gettext('Enabled')">
|
<a-form-item :label="$gettext('Enabled')">
|
||||||
<a-switch v-model:checked="enabled" @change="on_change_enabled"/>
|
<a-switch v-model:checked="enabled" @change="on_change_enabled"/>
|
||||||
</a-form-item>
|
</a-form-item>
|
||||||
|
<a-form-item :label="$gettext('Name')">
|
||||||
|
<a-input v-model:value="filename"/>
|
||||||
|
</a-form-item>
|
||||||
<ngx-config-editor
|
<ngx-config-editor
|
||||||
ref="ngx_config_editor"
|
ref="ngx_config_editor"
|
||||||
:ngx_config="ngx_config"
|
:ngx_config="ngx_config"
|
||||||
|
|
|
@ -77,7 +77,15 @@ type CertificateInfo struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetDomain(c *gin.Context) {
|
func GetDomain(c *gin.Context) {
|
||||||
|
rewriteName, ok := c.Get("rewriteConfigFileName")
|
||||||
|
|
||||||
name := c.Param("name")
|
name := c.Param("name")
|
||||||
|
|
||||||
|
// for modify filename
|
||||||
|
if ok {
|
||||||
|
name = rewriteName.(string)
|
||||||
|
}
|
||||||
|
|
||||||
path := filepath.Join(nginx.GetNginxConfPath("sites-available"), name)
|
path := filepath.Join(nginx.GetNginxConfPath("sites-available"), name)
|
||||||
|
|
||||||
enabled := true
|
enabled := true
|
||||||
|
@ -130,19 +138,59 @@ func GetDomain(c *gin.Context) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func EditDomain(c *gin.Context) {
|
func EditDomain(c *gin.Context) {
|
||||||
var err error
|
|
||||||
name := c.Param("name")
|
name := c.Param("name")
|
||||||
request := make(gin.H)
|
|
||||||
err = c.BindJSON(&request)
|
if name == "" {
|
||||||
|
c.JSON(http.StatusNotAcceptable, gin.H{
|
||||||
|
"message": "param name is empty",
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var json struct {
|
||||||
|
Name string `json:"name" binding:"required"`
|
||||||
|
Content string `json:"content"`
|
||||||
|
}
|
||||||
|
|
||||||
|
if !BindAndValid(c, &json) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
path := filepath.Join(nginx.GetNginxConfPath("sites-available"), name)
|
path := filepath.Join(nginx.GetNginxConfPath("sites-available"), name)
|
||||||
|
|
||||||
err = os.WriteFile(path, []byte(request["content"].(string)), 0644)
|
err := os.WriteFile(path, []byte(json.Content), 0644)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ErrHandler(c, err)
|
ErrHandler(c, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
enabledConfigFilePath := filepath.Join(nginx.GetNginxConfPath("sites-enabled"), name)
|
enabledConfigFilePath := filepath.Join(nginx.GetNginxConfPath("sites-enabled"), name)
|
||||||
|
// rename the config file if needed
|
||||||
|
if name != json.Name {
|
||||||
|
newPath := filepath.Join(nginx.GetNginxConfPath("sites-available"), json.Name)
|
||||||
|
// recreate soft link
|
||||||
|
log.Println(enabledConfigFilePath)
|
||||||
|
if _, err = os.Stat(enabledConfigFilePath); err == nil {
|
||||||
|
log.Println(enabledConfigFilePath)
|
||||||
|
_ = os.Remove(enabledConfigFilePath)
|
||||||
|
enabledConfigFilePath = filepath.Join(nginx.GetNginxConfPath("sites-enabled"), json.Name)
|
||||||
|
err = os.Symlink(newPath, enabledConfigFilePath)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
ErrHandler(c, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
err = os.Rename(path, newPath)
|
||||||
|
if err != nil {
|
||||||
|
ErrHandler(c, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
name = json.Name
|
||||||
|
c.Set("rewriteConfigFileName", name)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
enabledConfigFilePath = filepath.Join(nginx.GetNginxConfPath("sites-enabled"), name)
|
||||||
if _, err = os.Stat(enabledConfigFilePath); err == nil {
|
if _, err = os.Stat(enabledConfigFilePath); err == nil {
|
||||||
// Test nginx configuration
|
// Test nginx configuration
|
||||||
err = nginx.TestNginxConf()
|
err = nginx.TestNginxConf()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue