1
1
package service
2
2
3
3
import (
4
+ "context"
4
5
"fmt"
6
+ "github.com/1Panel-dev/1Panel/backend/app/dto/request"
5
7
"io"
6
8
"os"
7
9
"os/exec"
@@ -22,6 +24,9 @@ type IAIToolService interface {
22
24
Create (name string ) error
23
25
Delete (name string ) error
24
26
LoadDetail (name string ) (string , error )
27
+ BindDomain (req dto.OllamaBindDomain ) error
28
+ GetBindDomain (req dto.OllamaBindDomainReq ) (* dto.OllamaBindDomainRes , error )
29
+ UpdateBindDomain (req dto.OllamaBindDomain ) error
25
30
}
26
31
27
32
func NewIAIToolService () IAIToolService {
@@ -192,3 +197,98 @@ func (u *AIToolService) Delete(name string) error {
192
197
}
193
198
return nil
194
199
}
200
+
201
+ func (u * AIToolService ) BindDomain (req dto.OllamaBindDomain ) error {
202
+ nginxInstall , _ := getAppInstallByKey (constant .AppOpenresty )
203
+ if nginxInstall .ID == 0 {
204
+ return buserr .New ("ErrOpenrestyInstall" )
205
+ }
206
+ createWebsiteReq := request.WebsiteCreate {
207
+ PrimaryDomain : req .Domain ,
208
+ Alias : strings .ToLower (req .Domain ),
209
+ Type : constant .Deployment ,
210
+ AppType : constant .InstalledApp ,
211
+ AppInstallID : req .AppInstallID ,
212
+ }
213
+ websiteService := NewIWebsiteService ()
214
+ if err := websiteService .CreateWebsite (createWebsiteReq ); err != nil {
215
+ return err
216
+ }
217
+ website , err := websiteRepo .GetFirst (websiteRepo .WithAlias (strings .ToLower (req .Domain )))
218
+ if err != nil {
219
+ return err
220
+ }
221
+ if err = ConfigAllowIPs (req .AllowIPs , website ); err != nil {
222
+ return err
223
+ }
224
+ if req .SSLID > 0 {
225
+ sslReq := request.WebsiteHTTPSOp {
226
+ WebsiteID : website .ID ,
227
+ Enable : true ,
228
+ Type : "existed" ,
229
+ WebsiteSSLID : req .SSLID ,
230
+ HttpConfig : "HTTPSOnly" ,
231
+ }
232
+ if _ , err = websiteService .OpWebsiteHTTPS (context .Background (), sslReq ); err != nil {
233
+ return err
234
+ }
235
+ }
236
+ return nil
237
+ }
238
+
239
+ func (u * AIToolService ) GetBindDomain (req dto.OllamaBindDomainReq ) (* dto.OllamaBindDomainRes , error ) {
240
+ install , err := appInstallRepo .GetFirst (commonRepo .WithByID (req .AppInstallID ))
241
+ if err != nil {
242
+ return nil , err
243
+ }
244
+ res := & dto.OllamaBindDomainRes {}
245
+ website , _ := websiteRepo .GetFirst (websiteRepo .WithAppInstallId (install .ID ))
246
+ if website .ID == 0 {
247
+ return res , nil
248
+ }
249
+ res .WebsiteID = website .ID
250
+ res .Domain = website .PrimaryDomain
251
+ if website .WebsiteSSLID > 0 {
252
+ res .SSLID = website .WebsiteSSLID
253
+ }
254
+ res .AllowIPs = GetAllowIps (website )
255
+ return res , nil
256
+ }
257
+
258
+ func (u * AIToolService ) UpdateBindDomain (req dto.OllamaBindDomain ) error {
259
+ nginxInstall , _ := getAppInstallByKey (constant .AppOpenresty )
260
+ if nginxInstall .ID == 0 {
261
+ return buserr .New ("ErrOpenrestyInstall" )
262
+ }
263
+ websiteService := NewIWebsiteService ()
264
+ website , err := websiteRepo .GetFirst (commonRepo .WithByID (req .WebsiteID ))
265
+ if err != nil {
266
+ return err
267
+ }
268
+ if err = ConfigAllowIPs (req .AllowIPs , website ); err != nil {
269
+ return err
270
+ }
271
+ if req .SSLID > 0 {
272
+ sslReq := request.WebsiteHTTPSOp {
273
+ WebsiteID : website .ID ,
274
+ Enable : true ,
275
+ Type : "existed" ,
276
+ WebsiteSSLID : req .SSLID ,
277
+ HttpConfig : "HTTPSOnly" ,
278
+ }
279
+ if _ , err = websiteService .OpWebsiteHTTPS (context .Background (), sslReq ); err != nil {
280
+ return err
281
+ }
282
+ return nil
283
+ }
284
+ if website .WebsiteSSLID > 0 && req .SSLID == 0 {
285
+ sslReq := request.WebsiteHTTPSOp {
286
+ WebsiteID : website .ID ,
287
+ Enable : false ,
288
+ }
289
+ if _ , err = websiteService .OpWebsiteHTTPS (context .Background (), sslReq ); err != nil {
290
+ return err
291
+ }
292
+ }
293
+ return nil
294
+ }
0 commit comments