store.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. import type { CertificateInfo } from '@/api/cert'
  2. import type { Stream } from '@/api/stream'
  3. import type { CheckedType } from '@/types'
  4. import config from '@/api/config'
  5. import ngx from '@/api/ngx'
  6. import stream from '@/api/stream'
  7. import { useNgxConfigStore } from '@/components/NgxConfigEditor'
  8. import { ConfigStatus } from '@/constants'
  9. export const useStreamEditorStore = defineStore('streamEditor', () => {
  10. const name = ref('')
  11. const advanceMode = ref(false)
  12. const parseErrorStatus = ref(false)
  13. const parseErrorMessage = ref('')
  14. const data = ref({}) as Ref<Stream>
  15. const loading = ref(true)
  16. const saving = ref(false)
  17. const autoCert = ref(false)
  18. const certInfoMap = ref({}) as Ref<Record<number, CertificateInfo[]>>
  19. const filename = ref('')
  20. const filepath = ref('')
  21. const status = ref(ConfigStatus.Disabled)
  22. const ngxConfigStore = useNgxConfigStore()
  23. const { ngxConfig, configText, curServerIdx, curServer, curServerDirectives, curDirectivesMap } = storeToRefs(ngxConfigStore)
  24. async function init(_name: string) {
  25. loading.value = true
  26. name.value = _name
  27. await nextTick()
  28. if (name.value) {
  29. try {
  30. const r = await stream.getItem(encodeURIComponent(name.value))
  31. handleResponse(r)
  32. }
  33. catch (error) {
  34. handleParseError(error as { error?: string, message: string })
  35. }
  36. }
  37. loading.value = false
  38. }
  39. async function buildConfig() {
  40. return ngx.build_config(ngxConfig.value).then(r => {
  41. configText.value = r.content
  42. })
  43. }
  44. async function save() {
  45. saving.value = true
  46. try {
  47. if (!advanceMode.value) {
  48. await buildConfig()
  49. }
  50. const response = await stream.updateItem(encodeURIComponent(name.value), {
  51. content: configText.value,
  52. overwrite: true,
  53. env_group_id: data.value.env_group_id,
  54. sync_node_ids: data.value.sync_node_ids,
  55. post_action: 'reload_nginx',
  56. })
  57. handleResponse(response)
  58. }
  59. catch (error) {
  60. handleParseError(error as { error?: string, message: string })
  61. }
  62. finally {
  63. saving.value = false
  64. }
  65. }
  66. function handleParseError(e: { error?: string, message: string }) {
  67. console.error(e)
  68. parseErrorStatus.value = true
  69. parseErrorMessage.value = e.message
  70. config.getItem(`streams-available/${encodeURIComponent(name.value)}`).then(r => {
  71. configText.value = r.content
  72. })
  73. }
  74. async function handleResponse(r: Stream) {
  75. if (r.advanced)
  76. advanceMode.value = true
  77. status.value = r.status
  78. parseErrorStatus.value = false
  79. parseErrorMessage.value = ''
  80. filename.value = r.name
  81. filepath.value = r.filepath
  82. configText.value = r.config
  83. data.value = r
  84. Object.assign(ngxConfig, r.tokenized)
  85. const ngxConfigStore = useNgxConfigStore()
  86. if (r.tokenized)
  87. ngxConfigStore.setNgxConfig(r.tokenized)
  88. }
  89. async function handleModeChange(advanced: CheckedType) {
  90. loading.value = true
  91. try {
  92. await stream.advance_mode(encodeURIComponent(name.value), { advanced: advanced as boolean })
  93. advanceMode.value = advanced as boolean
  94. if (advanced) {
  95. await buildConfig()
  96. }
  97. else {
  98. let r = await stream.getItem(encodeURIComponent(name.value))
  99. await handleResponse(r)
  100. r = await ngx.tokenize_config(configText.value)
  101. Object.assign(ngxConfig, {
  102. ...r,
  103. name: name.value,
  104. })
  105. }
  106. }
  107. // eslint-disable-next-line ts/no-explicit-any
  108. catch (e: any) {
  109. handleParseError(e)
  110. }
  111. loading.value = false
  112. }
  113. return {
  114. name,
  115. advanceMode,
  116. parseErrorStatus,
  117. parseErrorMessage,
  118. data,
  119. loading,
  120. saving,
  121. autoCert,
  122. certInfoMap,
  123. ngxConfig,
  124. curServerIdx,
  125. curServer,
  126. curServerDirectives,
  127. curDirectivesMap,
  128. filename,
  129. filepath,
  130. configText,
  131. status,
  132. init,
  133. save,
  134. handleModeChange,
  135. }
  136. })