DomainEdit.vue 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. <script setup lang="ts">
  2. import { message } from 'ant-design-vue'
  3. import FooterToolBar from '@/components/FooterToolbar/FooterToolBar.vue'
  4. import CodeEditor from '@/components/CodeEditor/CodeEditor.vue'
  5. import NgxConfigEditor from '@/views/domain/ngx_conf/NgxConfigEditor.vue'
  6. import type { Site } from '@/api/domain'
  7. import domain from '@/api/domain'
  8. import type { NgxConfig } from '@/api/ngx'
  9. import ngx from '@/api/ngx'
  10. import config from '@/api/config'
  11. import RightSettings from '@/views/domain/components/RightSettings.vue'
  12. import type { CertificateInfo } from '@/api/cert'
  13. import type { ChatComplicationMessage } from '@/api/openai'
  14. import type { CheckedType } from '@/types'
  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 ngx_config: NgxConfig = reactive({
  22. name: '',
  23. upstreams: [],
  24. servers: [],
  25. })
  26. const cert_info_map: Record<string, CertificateInfo> = reactive({})
  27. const auto_cert = ref(false)
  28. const enabled = ref(false)
  29. const configText = ref('')
  30. const advance_mode_ref = ref(false)
  31. const saving = ref(false)
  32. const filename = ref('')
  33. const parse_error_status = ref(false)
  34. const parse_error_message = ref('')
  35. const data = ref({})
  36. init()
  37. const advance_mode = computed({
  38. get() {
  39. return advance_mode_ref.value || parse_error_status.value
  40. },
  41. set(v: boolean) {
  42. advance_mode_ref.value = v
  43. },
  44. })
  45. const history_chatgpt_record = ref([]) as Ref<ChatComplicationMessage[]>
  46. function handle_response(r: Site) {
  47. if (r.advanced)
  48. advance_mode.value = true
  49. if (r.advanced)
  50. advance_mode.value = true
  51. Object.keys(cert_info_map).forEach((v: string) => {
  52. delete cert_info_map[v]
  53. })
  54. parse_error_status.value = false
  55. parse_error_message.value = ''
  56. filename.value = r.name
  57. configText.value = r.config
  58. enabled.value = r.enabled
  59. auto_cert.value = r.auto_cert
  60. history_chatgpt_record.value = r.chatgpt_messages
  61. data.value = r
  62. Object.assign(ngx_config, r.tokenized)
  63. Object.assign(cert_info_map, r.cert_info)
  64. }
  65. function init() {
  66. if (name.value) {
  67. domain.get(name.value).then(r => {
  68. handle_response(r)
  69. }).catch(handle_parse_error)
  70. }
  71. else {
  72. history_chatgpt_record.value = []
  73. }
  74. }
  75. function handle_parse_error(e: { error?: string; message: string }) {
  76. console.error(e)
  77. parse_error_status.value = true
  78. parse_error_message.value = e.message
  79. config.get(`sites-available/${name.value}`).then(r => {
  80. configText.value = r.content
  81. })
  82. }
  83. function on_mode_change(advanced: CheckedType) {
  84. domain.advance_mode(name.value, { advanced: advanced as boolean }).then(() => {
  85. advance_mode.value = advanced as boolean
  86. if (advanced) {
  87. build_config()
  88. }
  89. else {
  90. // eslint-disable-next-line promise/no-nesting
  91. return ngx.tokenize_config(configText.value).then(r => {
  92. Object.assign(ngx_config, r)
  93. // eslint-disable-next-line promise/no-nesting
  94. }).catch(handle_parse_error)
  95. }
  96. })
  97. }
  98. async function build_config() {
  99. return ngx.build_config(ngx_config).then(r => {
  100. configText.value = r.content
  101. })
  102. }
  103. const save = async () => {
  104. saving.value = true
  105. if (!advance_mode.value) {
  106. try {
  107. await build_config()
  108. }
  109. catch (e) {
  110. saving.value = false
  111. message.error($gettext('Failed to save, syntax error(s) was detected in the configuration.'))
  112. return
  113. }
  114. }
  115. return domain.save(name.value, {
  116. name: filename.value || name.value,
  117. content: configText.value,
  118. 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. provide('save_config', save)
  131. provide('configText', configText)
  132. provide('ngx_config', ngx_config)
  133. provide('history_chatgpt_record', history_chatgpt_record)
  134. provide('enabled', enabled)
  135. provide('name', name)
  136. provide('filename', filename)
  137. provide('data', data)
  138. </script>
  139. <template>
  140. <ARow :gutter="16">
  141. <ACol
  142. :xs="24"
  143. :sm="24"
  144. :md="18"
  145. >
  146. <ACard :bordered="false">
  147. <template #title>
  148. <span style="margin-right: 10px">{{ $gettext('Edit %{n}', { n: name }) }}</span>
  149. <ATag
  150. v-if="enabled"
  151. color="blue"
  152. >
  153. {{ $gettext('Enabled') }}
  154. </ATag>
  155. <ATag
  156. v-else
  157. color="orange"
  158. >
  159. {{ $gettext('Disabled') }}
  160. </ATag>
  161. </template>
  162. <template #extra>
  163. <div class="mode-switch">
  164. <div class="switch">
  165. <ASwitch
  166. size="small"
  167. :disabled="parse_error_status"
  168. :checked="advance_mode"
  169. @change="on_mode_change"
  170. />
  171. </div>
  172. <template v-if="advance_mode">
  173. <div>{{ $gettext('Advance Mode') }}</div>
  174. </template>
  175. <template v-else>
  176. <div>{{ $gettext('Basic Mode') }}</div>
  177. </template>
  178. </div>
  179. </template>
  180. <Transition name="slide-fade">
  181. <div
  182. v-if="advance_mode"
  183. key="advance"
  184. >
  185. <div
  186. v-if="parse_error_status"
  187. class="parse-error-alert-wrapper"
  188. >
  189. <AAlert
  190. :message="$gettext('Nginx Configuration Parse Error')"
  191. :description="parse_error_message"
  192. type="error"
  193. show-icon
  194. />
  195. </div>
  196. <div>
  197. <CodeEditor v-model:content="configText" />
  198. </div>
  199. </div>
  200. <div
  201. v-else
  202. key="basic"
  203. class="domain-edit-container"
  204. >
  205. <NgxConfigEditor
  206. v-model:auto-cert="auto_cert"
  207. :cert-info="cert_info_map"
  208. :enabled="enabled"
  209. @callback="save"
  210. />
  211. </div>
  212. </Transition>
  213. </ACard>
  214. </ACol>
  215. <ACol
  216. class="col-right"
  217. :xs="24"
  218. :sm="24"
  219. :md="6"
  220. >
  221. <RightSettings />
  222. </ACol>
  223. <FooterToolBar>
  224. <ASpace>
  225. <AButton @click="$router.push('/domain/list')">
  226. {{ $gettext('Back') }}
  227. </AButton>
  228. <AButton
  229. type="primary"
  230. :loading="saving"
  231. @click="save"
  232. >
  233. {{ $gettext('Save') }}
  234. </AButton>
  235. </ASpace>
  236. </FooterToolBar>
  237. </ARow>
  238. </template>
  239. <style lang="less">
  240. </style>
  241. <style lang="less" scoped>
  242. .col-right {
  243. position: relative;
  244. }
  245. .ant-card {
  246. margin: 10px 0;
  247. box-shadow: unset;
  248. }
  249. .mode-switch {
  250. display: flex;
  251. .switch {
  252. display: flex;
  253. align-items: center;
  254. margin-right: 5px;
  255. }
  256. }
  257. .parse-error-alert-wrapper {
  258. margin-bottom: 20px;
  259. }
  260. .domain-edit-container {
  261. max-width: 800px;
  262. margin: 0 auto;
  263. }
  264. .slide-fade-enter-active {
  265. transition: all .3s ease-in-out;
  266. }
  267. .slide-fade-leave-active {
  268. transition: all .3s cubic-bezier(1.0, 0.5, 0.8, 1.0);
  269. }
  270. .slide-fade-enter-from, .slide-fade-enter-to, .slide-fade-leave-to
  271. /* .slide-fade-leave-active for below version 2.1.8 */ {
  272. transform: translateX(10px);
  273. opacity: 0;
  274. }
  275. .directive-params-wrapper {
  276. margin: 10px 0;
  277. }
  278. .tab-content {
  279. padding: 10px;
  280. }
  281. </style>