DomainEdit.vue 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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 {message} from 'ant-design-vue'
  11. import config from '@/api/config'
  12. import RightSettings from '@/views/domain/components/RightSettings.vue'
  13. const {$gettext, interpolate} = useGettext()
  14. const route = useRoute()
  15. const router = useRouter()
  16. const name = ref(route.params.name.toString())
  17. watch(route, () => {
  18. name.value = route.params?.name?.toString() ?? ''
  19. })
  20. const update = ref(0)
  21. const ngx_config: any = reactive({
  22. name: '',
  23. upstreams: [],
  24. servers: []
  25. })
  26. const cert_info_map: any = reactive({})
  27. const auto_cert = ref(false)
  28. const enabled = ref(false)
  29. const configText = ref('')
  30. const ok = ref(false)
  31. const advance_mode_ref = ref(false)
  32. const saving = ref(false)
  33. const filename = ref('')
  34. const parse_error_status = ref(false)
  35. const parse_error_message = ref('')
  36. const data = 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. data.value = r
  65. Object.assign(ngx_config, r.tokenized)
  66. Object.assign(cert_info_map, r.cert_info)
  67. }
  68. function init() {
  69. if (name.value) {
  70. domain.get(name.value).then((r: any) => {
  71. handle_response(r)
  72. }).catch(handle_parse_error)
  73. } else {
  74. history_chatgpt_record.value = []
  75. }
  76. }
  77. function handle_parse_error(r: any) {
  78. console.error(r)
  79. if (r?.error === 'nginx_config_syntax_error') {
  80. parse_error_status.value = true
  81. parse_error_message.value = r.message
  82. config.get('sites-available/' + name.value).then(r => {
  83. configText.value = r.config
  84. })
  85. } else {
  86. message.error($gettext(r?.message ?? 'Server error'))
  87. }
  88. }
  89. function on_mode_change(advanced: boolean) {
  90. domain.advance_mode(name.value, {advanced}).then(() => {
  91. advance_mode.value = advanced
  92. if (advanced) {
  93. build_config()
  94. } else {
  95. return ngx.tokenize_config(configText.value).then((r: any) => {
  96. Object.assign(ngx_config, r)
  97. }).catch(handle_parse_error)
  98. }
  99. })
  100. }
  101. function build_config() {
  102. return ngx.build_config(ngx_config).then((r: any) => {
  103. configText.value = r.content
  104. })
  105. }
  106. const save = async () => {
  107. saving.value = true
  108. if (!advance_mode.value) {
  109. try {
  110. await build_config()
  111. } catch (e) {
  112. saving.value = false
  113. message.error($gettext('Failed to save, syntax error(s) was detected in the configuration.'))
  114. return
  115. }
  116. }
  117. await domain.save(name.value, {
  118. name: filename.value || name.value,
  119. content: configText.value, overwrite: true
  120. }).then(r => {
  121. handle_response(r)
  122. router.push({
  123. path: '/domain/' + filename.value,
  124. query: route.query
  125. })
  126. message.success($gettext('Saved successfully'))
  127. }).catch(handle_parse_error).finally(() => {
  128. saving.value = false
  129. })
  130. }
  131. provide('save_site_config', save)
  132. provide('configText', configText)
  133. provide('ngx_config', ngx_config)
  134. provide('history_chatgpt_record', history_chatgpt_record)
  135. provide('enabled', enabled)
  136. provide('name', name)
  137. provide('filename', filename)
  138. provide('data', data)
  139. </script>
  140. <template>
  141. <a-row :gutter="16">
  142. <a-col :xs="24" :sm="24" :md="18">
  143. <a-card :bordered="false">
  144. <template #title>
  145. <span style="margin-right: 10px">{{ interpolate($gettext('Edit %{n}'), {n: name}) }}</span>
  146. <a-tag color="blue" v-if="enabled">
  147. {{ $gettext('Enabled') }}
  148. </a-tag>
  149. <a-tag color="orange" v-else>
  150. {{ $gettext('Disabled') }}
  151. </a-tag>
  152. </template>
  153. <template #extra>
  154. <div class="mode-switch">
  155. <div class="switch">
  156. <a-switch size="small" :disabled="parse_error_status"
  157. :checked="advance_mode" @change="on_mode_change"/>
  158. </div>
  159. <template v-if="advance_mode">
  160. <div>{{ $gettext('Advance Mode') }}</div>
  161. </template>
  162. <template v-else>
  163. <div>{{ $gettext('Basic Mode') }}</div>
  164. </template>
  165. </div>
  166. </template>
  167. <transition name="slide-fade">
  168. <div v-if="advance_mode" key="advance">
  169. <div class="parse-error-alert-wrapper" v-if="parse_error_status">
  170. <a-alert :message="$gettext('Nginx Configuration Parse Error')"
  171. :description="parse_error_message"
  172. type="error"
  173. show-icon
  174. />
  175. </div>
  176. <div>
  177. <code-editor v-model:content="configText"/>
  178. </div>
  179. </div>
  180. <div class="domain-edit-container" key="basic" v-else>
  181. <ngx-config-editor
  182. ref="ngx_config_editor"
  183. :ngx_config="ngx_config"
  184. :cert_info="cert_info_map"
  185. v-model:auto_cert="auto_cert"
  186. :enabled="enabled"
  187. @callback="save()"
  188. />
  189. </div>
  190. </transition>
  191. </a-card>
  192. </a-col>
  193. <a-col class="col-right" :xs="24" :sm="24" :md="6">
  194. <right-settings/>
  195. </a-col>
  196. <footer-tool-bar>
  197. <a-space>
  198. <a-button @click="$router.push('/domain/list')">
  199. <translate>Back</translate>
  200. </a-button>
  201. <a-button type="primary" @click="save" :loading="saving">
  202. <translate>Save</translate>
  203. </a-button>
  204. </a-space>
  205. </footer-tool-bar>
  206. </a-row>
  207. </template>
  208. <style lang="less">
  209. </style>
  210. <style lang="less" scoped>
  211. .col-right {
  212. position: relative;
  213. }
  214. .ant-card {
  215. margin: 10px 0;
  216. box-shadow: unset;
  217. }
  218. .mode-switch {
  219. display: flex;
  220. .switch {
  221. display: flex;
  222. align-items: center;
  223. margin-right: 5px;
  224. }
  225. }
  226. .parse-error-alert-wrapper {
  227. margin-bottom: 20px;
  228. }
  229. .domain-edit-container {
  230. max-width: 800px;
  231. margin: 0 auto;
  232. }
  233. .slide-fade-enter-active {
  234. transition: all .3s ease-in-out;
  235. }
  236. .slide-fade-leave-active {
  237. transition: all .3s cubic-bezier(1.0, 0.5, 0.8, 1.0);
  238. }
  239. .slide-fade-enter-from, .slide-fade-enter-to, .slide-fade-leave-to
  240. /* .slide-fade-leave-active for below version 2.1.8 */ {
  241. transform: translateX(10px);
  242. opacity: 0;
  243. }
  244. .location-block {
  245. }
  246. .directive-params-wrapper {
  247. margin: 10px 0;
  248. }
  249. .tab-content {
  250. padding: 10px;
  251. }
  252. </style>