Skip to content

Commit

Permalink
feat: forced use of advanced editor #73
Browse files Browse the repository at this point in the history
  • Loading branch information
0xJacky committed Apr 11, 2023
1 parent 8905575 commit defd814
Show file tree
Hide file tree
Showing 10 changed files with 508 additions and 34 deletions.
4 changes: 4 additions & 0 deletions frontend/src/api/domain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ class Domain extends Curd {
duplicate(name: string, data: any) {
return http.post(this.baseUrl + '/' + name + '/duplicate', data)
}

advance_mode(name: string, data: any) {
return http.post(this.baseUrl + '/' + name + '/advance', data)
}
}

const domain = new Domain('/domain')
Expand Down
62 changes: 39 additions & 23 deletions frontend/src/views/domain/DomainEdit.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ import {computed, provide, reactive, ref, watch} from 'vue'
import {useRoute, useRouter} from 'vue-router'
import domain from '@/api/domain'
import ngx from '@/api/ngx'
import {message} from 'ant-design-vue'
import {message, Modal} from 'ant-design-vue'
import config from '@/api/config'
import ChatGPT from '@/components/ChatGPT/ChatGPT.vue'
const {$gettext, interpolate} = useGettext()
const route = useRoute()
Expand All @@ -25,7 +24,7 @@ watch(route, () => {
const update = ref(0)
const ngx_config = reactive({
const ngx_config: any = reactive({
name: '',
upstreams: [],
servers: []
Expand Down Expand Up @@ -57,6 +56,10 @@ const history_chatgpt_record = ref([])
function handle_response(r: any) {
if (r.advanced) {
advance_mode.value = true
}
Object.keys(cert_info_map).forEach(v => {
delete cert_info_map[v]
})
Expand Down Expand Up @@ -95,14 +98,17 @@ function handle_parse_error(r: any) {
throw r
}
function on_mode_change(advance_mode: boolean) {
if (advance_mode) {
build_config()
} else {
return ngx.tokenize_config(configText.value).then((r: any) => {
Object.assign(ngx_config, r)
}).catch(handle_parse_error)
}
function on_mode_change(advanced: boolean) {
domain.advance_mode(name.value, {advanced}).then(() => {
advance_mode.value = advanced
if (advanced) {
build_config()
} else {
return ngx.tokenize_config(configText.value).then((r: any) => {
Object.assign(ngx_config, r)
}).catch(handle_parse_error)
}
})
}
function build_config() {
Expand Down Expand Up @@ -140,6 +146,7 @@ const save = async () => {
}
function enable() {
enabled.value = true
domain.enable(name.value).then(() => {
message.success($gettext('Enabled successfully'))
enabled.value = true
Expand All @@ -149,6 +156,7 @@ function enable() {
}
function disable() {
enabled.value = false
domain.disable(name.value).then(() => {
message.success($gettext('Disabled successfully'))
enabled.value = false
Expand All @@ -158,11 +166,18 @@ function disable() {
}
function on_change_enabled(checked: boolean) {
if (checked) {
enable()
} else {
disable()
}
Modal.confirm({
title: checked ? $gettext('Do you want to enable this site?') : $gettext('Do you want to disable this site?'),
mask: false,
centered: true,
async onOk() {
if (checked) {
enable()
} else {
disable()
}
}
})
}
const editor_md = computed(() => history_chatgpt_record?.value?.length > 1 ? 16 : 24)
Expand All @@ -187,7 +202,7 @@ provide('save_site_config', save)
<div class="mode-switch">
<div class="switch">
<a-switch size="small" :disabled="parse_error_status"
v-model:checked="advance_mode" @change="on_mode_change"/>
:checked="advance_mode" @change="on_mode_change"/>
</div>
<template v-if="advance_mode">
<div>{{ $gettext('Advance Mode') }}</div>
Expand All @@ -198,6 +213,13 @@ provide('save_site_config', save)
</div>
</template>

<a-form-item :label="$gettext('Enabled')">
<a-switch :checked="enabled" @change="on_change_enabled"/>
</a-form-item>
<a-form-item :label="$gettext('Name')">
<a-input v-model:value="filename"/>
</a-form-item>

<transition name="slide-fade">
<div v-if="advance_mode" key="advance">
<div class="parse-error-alert-wrapper" v-if="parse_error_status">
Expand All @@ -213,12 +235,6 @@ provide('save_site_config', save)
</div>

<div class="domain-edit-container" key="basic" v-else>
<a-form-item :label="$gettext('Enabled')">
<a-switch v-model:checked="enabled" @change="on_change_enabled"/>
</a-form-item>
<a-form-item :label="$gettext('Name')">
<a-input v-model:value="filename"/>
</a-form-item>
<ngx-config-editor
ref="ngx_config_editor"
:ngx_config="ngx_config"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ server {
listen 80;
listen [::]:80;
server_name homework.jackyu.cn;
# rewrite ^(.*)$ https://$host$1 permanent;
# rewrite ^(.*)$ https://$host$1 permanent;
return 307 https://$server_name$request_uri;
location /.well-known/acme-challenge {
proxy_set_header Host $host;
Expand All @@ -17,7 +17,7 @@ server {
server_name homework.jackyu.cn;
ssl_certificate /etc/nginx/ssl/homework.jackyu.cn/fullchain.cer;
ssl_certificate_key /etc/nginx/ssl/homework.jackyu.cn/private.key;
# rewrite ^(.*)$ https://$host$1 permanent;
# rewrite ^(.*)$ https://$host$1 permanent;
return 307 https://$server_name$request_uri;
location /.well-known/acme-challenge {
proxy_set_header Host $host;
Expand Down
78 changes: 70 additions & 8 deletions server/api/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,39 @@ func GetDomain(c *gin.Context) {
enabled = false
}

g := query.ChatGPTLog
chatgpt, err := g.Where(g.Name.Eq(path)).FirstOrCreate()

if err != nil {
ErrHandler(c, err)
return
}

s := query.Site
site, err := s.Where(s.Path.Eq(path)).FirstOrInit()

if err != nil {
ErrHandler(c, err)
return
}

if site.Advanced {
origContent, err := os.ReadFile(path)
if err != nil {
ErrHandler(c, err)
return
}

c.JSON(http.StatusOK, gin.H{
"advanced": site.Advanced,
"enabled": enabled,
"name": name,
"config": string(origContent),
"chatgpt_messages": chatgpt.Content,
})
return
}

c.Set("maybe_error", "nginx_config_syntax_error")
config, err := nginx.ParseNgxConfig(path)

Expand Down Expand Up @@ -130,17 +163,10 @@ func GetDomain(c *gin.Context) {

certModel, _ := model.FirstCert(name)

g := query.ChatGPTLog
chatgpt, err := g.Where(g.Name.Eq(path)).FirstOrCreate()

if err != nil {
ErrHandler(c, err)
return
}

c.Set("maybe_error", "nginx_config_syntax_error")

c.JSON(http.StatusOK, gin.H{
"advanced": site.Advanced,
"enabled": enabled,
"name": name,
"config": config.FmtCode(),
Expand Down Expand Up @@ -190,6 +216,9 @@ func SaveDomain(c *gin.Context) {
// rename the config file if needed
if name != json.Name {
newPath := nginx.GetConfPath("sites-available", json.Name)
s := query.Site
_, err = s.Where(s.Path.Eq(path)).Update(s.Path, newPath)

// check if dst file exists, do not rename
if helper.FileExists(newPath) {
c.JSON(http.StatusNotAcceptable, gin.H{
Expand Down Expand Up @@ -448,3 +477,36 @@ func DuplicateSite(c *gin.Context) {
"dst": dst,
})
}

func DomainEditByAdvancedMode(c *gin.Context) {
var json struct {
Advanced bool `json:"advanced"`
}

if !BindAndValid(c, &json) {
return
}

name := c.Param("name")
path := nginx.GetConfPath("sites-available", name)

s := query.Site

_, err := s.Where(s.Path.Eq(path)).FirstOrCreate()
if err != nil {
ErrHandler(c, err)
return
}

_, err = s.Where(s.Path.Eq(path)).Update(s.Advanced, json.Advanced)

if err != nil {
ErrHandler(c, err)
return
}

c.JSON(http.StatusOK, gin.H{
"message": "ok",
})

}
1 change: 1 addition & 0 deletions server/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func GenerateAllModel() []any {
AuthToken{},
Cert{},
ChatGPTLog{},
Site{},
}
}

Expand Down
7 changes: 7 additions & 0 deletions server/model/site.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package model

type Site struct {
Model
Path string `json:"path"`
Advanced bool `json:"advanced"`
}
2 changes: 1 addition & 1 deletion server/pkg/nginx/build_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
func buildComments(orig string, indent int) (content string) {
scanner := bufio.NewScanner(strings.NewReader(orig))
for scanner.Scan() {
content += strings.Repeat("\t", indent) + "# " + scanner.Text() + "\n"
content += strings.Repeat("\t", indent) + "# " + strings.TrimSpace(scanner.Text()) + "\n"
}
content = strings.TrimLeft(content, "\n")
return
Expand Down
8 changes: 8 additions & 0 deletions server/query/gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit defd814

Please # to comment.