nginx.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import request from "./request.ts";
  2. import {BaseResp, INginxCerts, IServerHost} from "../models/api.ts";
  3. import {INginx, INginxServer} from "../models/nginx.ts";
  4. import {createServer, createServerHost} from "../pages/nginx/utils/nginx.ts";
  5. type RefreshHttpData = {
  6. id: number
  7. httpConf: string
  8. httpData: string
  9. }
  10. export const NginxApis= {
  11. findAll: () => request.get<BaseResp<INginx[]>>('/nginx'),
  12. // eslint-disable-next-line @typescript-eslint/ban-ts-comment
  13. // @ts-ignore
  14. updateOrAdd: (data: Partial<INginx>) => {
  15. if (data.id){
  16. return request.post<BaseResp<INginx>>(`/nginx/${data.id}`, data, { disableErrorMsg: true, timeout: 60000 } as any)
  17. }else {
  18. return request.post<BaseResp<INginx>>('/nginx', data, { disableErrorMsg: true, timeout: 60000 } as any)
  19. }
  20. },
  21. /**
  22. * 同步配置文件到本地,仅需要传递id 和httpConf, httpData 三个参数
  23. * @param nginx
  24. */
  25. refreshHttp: (nginx: RefreshHttpData) => {
  26. return request.post(`/nginx/${nginx.id}/http/refresh`, nginx, { timeout: 60000 })
  27. },
  28. getNginx: (id:number | string) => request.get<BaseResp<{nginx: INginx, servers: IServerHost[]}>>(`/nginx/${id}`),
  29. delNginx: (id:number) => request.delete(`/nginx/${id}`),
  30. status: (id:number) => request.post(`/nginx/${id}/status`, { }, { timeout: 60000 }),
  31. startNginx: (id:number) => request.post(`/nginx/${id}/start`, { }, { timeout: 60000 }),
  32. stopNginx: (id:number) => request.post(`/nginx/${id}/stop`, { }, { timeout: 60000 }),
  33. /**
  34. * 不更改配置文件,仅保存数据,方便某些特殊情况,一直手动修改配置文件
  35. * @param nginx
  36. * @param server
  37. */
  38. // add or update
  39. updateServer: (nginx: INginx,server: Partial<INginxServer>) => {
  40. const serverHost: Partial<IServerHost> = createServerHost(nginx,server);
  41. // eslint-disable-next-line @typescript-eslint/ban-ts-comment
  42. // @ts-ignore
  43. return request.post<BaseResp<IServerHost>>(`/nginx/${nginx.id}/server`,serverHost, { disableErrorMsg: true } as any)
  44. .then(({data})=>{
  45. if (data.data){
  46. return createServer(data.data)
  47. }
  48. return Promise.reject(data)
  49. })
  50. },
  51. /**
  52. * 更改配置文件,保存数据
  53. * @param nginx
  54. * @param server
  55. */
  56. refreshServer: (server: Partial<IServerHost>) => {
  57. return request.post(`/nginx/${server.nginxId}/server/refresh`, server, { timeout: 60000 })
  58. },
  59. deleteServer: (nginxId: number,server: INginxServer) => request.delete(`/nginx/${nginxId}/server`,{ data: { id: server.id}}),
  60. /**
  61. * 获取证书信息,不传name,则返回所有证书文件信息,传了name,则返回该证书的内容
  62. * @param id
  63. * @param name
  64. */
  65. getCerts: (id: number,name?: string) => request.get(`/nginx/${id}/certs`, { params: { name }}),
  66. /**
  67. * 保存证书信息
  68. * @param id
  69. * @param data
  70. */
  71. saveCerts: (id: number,data: INginxCerts) => request.post(`/nginx/${id}/certs`, data),
  72. delCerts: (nginxId: number,id: number) => request.delete(`/nginx/${nginxId}/certs`, { params: { id } }),
  73. /**
  74. * 从配置的数据目录中同步
  75. * @param id
  76. * @param name
  77. */
  78. syncCerts: (id: number) => request.post(`/nginx/${id}/certs/sync`),
  79. }
  80. export type IDeployReq ={
  81. key: string
  82. nginxId: number
  83. /**
  84. * 部署目录,资源部署目录,一般是root+name 或者是alias
  85. */
  86. dir: string
  87. /**
  88. * 是否清空文件夹再部署
  89. */
  90. clear?: boolean
  91. cmd?: string
  92. }
  93. /**
  94. * 文件上传
  95. */
  96. export const uploadApis = {
  97. uploadFile: (entry: FileSystemFileEntry, id: string) => {
  98. return new Promise<File>((resolve, reject) => {
  99. entry.file(function (f){
  100. resolve(f)
  101. },function (err){
  102. reject(err)
  103. })
  104. }).then(file=>{
  105. const formData = new FormData()
  106. formData.append("file", file)
  107. formData.append("Path", entry.fullPath)
  108. formData.append("Key", id)
  109. return request.post('/file',formData, {
  110. withCredentials: true,
  111. headers: {
  112. 'Content-type' : 'multipart/form-data'
  113. }
  114. })
  115. })
  116. },
  117. deploy:(data: IDeployReq)=>request.post(`/nginx/${data.nginxId}/file/deploy`, data, {timeout: 120000}),
  118. }