DomainEdit.vue 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. <script setup lang="ts">
  2. import FooterToolBar from '@/components/FooterToolbar/FooterToolBar.vue'
  3. import CodeEditor from '@/components/CodeEditor/CodeEditor.vue'
  4. import NgxConfigEditor from '@/views/domain/ngx_conf/NgxConfigEditor'
  5. import {useGettext} from 'vue3-gettext'
  6. import {computed, provide, reactive, ref, watch} from 'vue'
  7. import {useRoute, useRouter} from 'vue-router'
  8. import domain from '@/api/domain'
  9. import ngx from '@/api/ngx'
  10. import Modal from 'ant-design-vue/lib/modal'
  11. import {message} from 'ant-design-vue'
  12. import config from '@/api/config'
  13. import ChatGPT from '@/components/ChatGPT/ChatGPT.vue'
  14. const {$gettext, interpolate} = useGettext()
  15. const route = useRoute()
  16. const router = useRouter()
  17. const name = ref(route.params.name.toString())
  18. watch(route, () => {
  19. name.value = route.params?.name?.toString() ?? ''
  20. })
  21. const update = ref(0)
  22. const ngx_config: any = reactive({
  23. name: '',
  24. upstreams: [],
  25. servers: []
  26. })
  27. const cert_info_map: any = reactive({})
  28. const auto_cert = ref(false)
  29. const enabled = ref(false)
  30. const configText = ref('')
  31. const ok = ref(false)
  32. const advance_mode_ref = ref(false)
  33. const saving = ref(false)
  34. const filename = ref('')
  35. const parse_error_status = ref(false)
  36. const parse_error_message = ref('')
  37. init()
  38. const advance_mode = computed({
  39. get() {
  40. return advance_mode_ref.value || parse_error_status.value
  41. },
  42. set(v: boolean) {
  43. advance_mode_ref.value = v
  44. }
  45. })
  46. const history_chatgpt_record = ref([])
  47. function handle_response(r: any) {
  48. if (r.advanced) {
  49. advance_mode.value = true
  50. }
  51. if (r.advanced) {
  52. advance_mode.value = true
  53. }
  54. Object.keys(cert_info_map).forEach(v => {
  55. delete cert_info_map[v]
  56. })
  57. parse_error_status.value = false
  58. parse_error_message.value = ''
  59. filename.value = r.name
  60. configText.value = r.config
  61. enabled.value = r.enabled
  62. auto_cert.value = r.auto_cert
  63. history_chatgpt_record.value = r.chatgpt_messages
  64. Object.assign(ngx_config, r.tokenized)
  65. Object.assign(cert_info_map, r.cert_info)
  66. }
  67. function init() {
  68. if (name.value) {
  69. domain.get(name.value).then((r: any) => {
  70. handle_response(r)
  71. }).catch(handle_parse_error)
  72. } else {
  73. history_chatgpt_record.value = []
  74. }
  75. }
  76. function handle_parse_error(r: any) {
  77. if (r?.error === 'nginx_config_syntax_error') {
  78. parse_error_status.value = true
  79. parse_error_message.value = r.message
  80. config.get('sites-available/' + name.value).then(r => {
  81. configText.value = r.config
  82. })
  83. } else {
  84. message.error($gettext(r?.message ?? 'Server error'))
  85. }
  86. throw r
  87. }
  88. function on_mode_change(advanced: boolean) {
  89. domain.advance_mode(name.value, {advanced}).then(() => {
  90. advance_mode.value = advanced
  91. if (advanced) {
  92. build_config()
  93. } else {
  94. return ngx.tokenize_config(configText.value).then((r: any) => {
  95. Object.assign(ngx_config, r)
  96. }).catch(handle_parse_error)
  97. }
  98. })
  99. }
  100. function build_config() {
  101. return ngx.build_config(ngx_config).then((r: any) => {
  102. configText.value = r.content
  103. })
  104. }
  105. const save = async () => {
  106. saving.value = true
  107. if (!advance_mode.value) {
  108. try {
  109. await build_config()
  110. } catch (e) {
  111. saving.value = false
  112. message.error($gettext('Failed to save, syntax error(s) was detected in the configuration.'))
  113. return
  114. }
  115. }
  116. await domain.save(name.value, {
  117. name: filename.value || name.value,
  118. content: configText.value, overwrite: true
  119. }).then(r => {
  120. handle_response(r)
  121. router.push({
  122. path: '/domain/' + filename.value,
  123. query: route.query
  124. })
  125. message.success($gettext('Saved successfully'))
  126. }).catch(handle_parse_error).finally(() => {
  127. saving.value = false
  128. })
  129. }
  130. function enable() {
  131. domain.enable(name.value).then(() => {
  132. message.success($gettext('Enabled successfully'))
  133. enabled.value = true
  134. }).catch(r => {
  135. message.error(interpolate($gettext('Failed to enable %{msg}'), {msg: r.message ?? ''}), 10)
  136. })
  137. }
  138. function disable() {
  139. domain.disable(name.value).then(() => {
  140. message.success($gettext('Disabled successfully'))
  141. enabled.value = false
  142. }).catch(r => {
  143. message.error(interpolate($gettext('Failed to disable %{msg}'), {msg: r.message ?? ''}))
  144. })
  145. }
  146. function on_change_enabled(checked: boolean) {
  147. Modal.confirm({
  148. title: checked ? $gettext('Do you want to enable this site?') : $gettext('Do you want to disable this site?'),
  149. mask: false,
  150. centered: true,
  151. okText: $gettext('OK'),
  152. cancelText: $gettext('Cancel'),
  153. async onOk() {
  154. if (checked) {
  155. enable()
  156. } else {
  157. disable()
  158. }
  159. }
  160. })
  161. }
  162. const editor_md = computed(() => history_chatgpt_record?.value?.length > 1 ? 16 : 24)
  163. const chat_md = computed(() => history_chatgpt_record?.value?.length > 1 ? 8 : 24)
  164. provide('save_site_config', save)
  165. </script>
  166. <template>
  167. <a-row :gutter="16">
  168. <a-col :xs="24" :sm="24" :md="editor_md">
  169. <a-card :bordered="false">
  170. <template #title>
  171. <span style="margin-right: 10px">{{ interpolate($gettext('Edit %{n}'), {n: name}) }}</span>
  172. <a-tag color="blue" v-if="enabled">
  173. {{ $gettext('Enabled') }}
  174. </a-tag>
  175. <a-tag color="orange" v-else>
  176. {{ $gettext('Disabled') }}
  177. </a-tag>
  178. </template>
  179. <template #extra>
  180. <div class="mode-switch">
  181. <div class="switch">
  182. <a-switch size="small" :disabled="parse_error_status"
  183. :checked="advance_mode" @change="on_mode_change"/>
  184. </div>
  185. <template v-if="advance_mode">
  186. <div>{{ $gettext('Advance Mode') }}</div>
  187. </template>
  188. <template v-else>
  189. <div>{{ $gettext('Basic Mode') }}</div>
  190. </template>
  191. </div>
  192. </template>
  193. <a-form-item :label="$gettext('Enabled')">
  194. <a-switch :checked="enabled" @change="on_change_enabled"/>
  195. </a-form-item>
  196. <a-form-item :label="$gettext('Name')">
  197. <a-input v-model:value="filename"/>
  198. </a-form-item>
  199. <transition name="slide-fade">
  200. <div v-if="advance_mode" key="advance">
  201. <div class="parse-error-alert-wrapper" v-if="parse_error_status">
  202. <a-alert :message="$gettext('Nginx Configuration Parse Error')"
  203. :description="parse_error_message"
  204. type="error"
  205. show-icon
  206. />
  207. </div>
  208. <div>
  209. <code-editor v-model:content="configText"/>
  210. </div>
  211. </div>
  212. <div class="domain-edit-container" key="basic" v-else>
  213. <ngx-config-editor
  214. ref="ngx_config_editor"
  215. :ngx_config="ngx_config"
  216. :cert_info="cert_info_map"
  217. v-model:auto_cert="auto_cert"
  218. :enabled="enabled"
  219. @callback="save()"
  220. />
  221. </div>
  222. </transition>
  223. </a-card>
  224. </a-col>
  225. <a-col class="col-right" :xs="24" :sm="24" :md="chat_md">
  226. <chat-g-p-t :content="configText" :path="ngx_config.file_name"
  227. v-model:history_messages="history_chatgpt_record"/>
  228. </a-col>
  229. <footer-tool-bar>
  230. <a-space>
  231. <a-button @click="$router.push('/domain/list')">
  232. <translate>Back</translate>
  233. </a-button>
  234. <a-button type="primary" @click="save" :loading="saving">
  235. <translate>Save</translate>
  236. </a-button>
  237. </a-space>
  238. </footer-tool-bar>
  239. </a-row>
  240. </template>
  241. <style lang="less">
  242. </style>
  243. <style lang="less" scoped>
  244. .col-right {
  245. position: relative;
  246. }
  247. .ant-card {
  248. margin: 10px 0;
  249. box-shadow: unset;
  250. }
  251. .mode-switch {
  252. display: flex;
  253. .switch {
  254. display: flex;
  255. align-items: center;
  256. margin-right: 5px;
  257. }
  258. }
  259. .parse-error-alert-wrapper {
  260. margin-bottom: 20px;
  261. }
  262. .domain-edit-container {
  263. max-width: 800px;
  264. margin: 0 auto;
  265. }
  266. .slide-fade-enter-active {
  267. transition: all .3s ease-in-out;
  268. }
  269. .slide-fade-leave-active {
  270. transition: all .3s cubic-bezier(1.0, 0.5, 0.8, 1.0);
  271. }
  272. .slide-fade-enter-from, .slide-fade-enter-to, .slide-fade-leave-to
  273. /* .slide-fade-leave-active for below version 2.1.8 */ {
  274. transform: translateX(10px);
  275. opacity: 0;
  276. }
  277. .location-block {
  278. }
  279. .directive-params-wrapper {
  280. margin: 10px 0;
  281. }
  282. .tab-content {
  283. padding: 10px;
  284. }
  285. </style>