|
| 1 | +package service |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "github.com/1Panel-dev/1Panel/agent/app/dto/request" |
| 7 | + "github.com/1Panel-dev/1Panel/agent/app/repo" |
| 8 | + "github.com/1Panel-dev/1Panel/agent/app/task" |
| 9 | + "github.com/1Panel-dev/1Panel/agent/constant" |
| 10 | + "github.com/1Panel-dev/1Panel/agent/i18n" |
| 11 | + "sort" |
| 12 | +) |
| 13 | + |
| 14 | +func (w WebsiteService) BatchOpWebsite(req request.BatchWebsiteOp) error { |
| 15 | + websites, _ := websiteRepo.List(repo.WithByIDs(req.IDs)) |
| 16 | + opTask, err := task.NewTaskWithOps(i18n.GetMsgByKey("Status"), task.TaskBatch, task.TaskScopeWebsite, req.TaskID, 0) |
| 17 | + if err != nil { |
| 18 | + return err |
| 19 | + } |
| 20 | + sort.SliceStable(websites, func(i, j int) bool { |
| 21 | + if websites[i].Type == constant.Subsite && websites[j].Type != constant.Subsite { |
| 22 | + return true |
| 23 | + } |
| 24 | + if websites[i].Type != constant.Subsite && websites[j].Type == constant.Subsite { |
| 25 | + return false |
| 26 | + } |
| 27 | + return false |
| 28 | + }) |
| 29 | + opWebsiteTask := func(t *task.Task) error { |
| 30 | + for _, web := range websites { |
| 31 | + msg := fmt.Sprintf("%s %s", i18n.GetMsgByKey(req.Operate), web.PrimaryDomain) |
| 32 | + switch req.Operate { |
| 33 | + case constant.StopWeb, constant.StartWeb: |
| 34 | + if err := opWebsite(&web, req.Operate); err != nil { |
| 35 | + t.LogFailedWithErr(msg, err) |
| 36 | + continue |
| 37 | + } |
| 38 | + _ = websiteRepo.Save(context.Background(), &web) |
| 39 | + case "delete": |
| 40 | + if err := w.DeleteWebsite(request.WebsiteDelete{ |
| 41 | + ID: web.ID, |
| 42 | + }); err != nil { |
| 43 | + t.LogFailedWithErr(msg, err) |
| 44 | + continue |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + t.LogSuccess(msg) |
| 49 | + } |
| 50 | + return nil |
| 51 | + } |
| 52 | + opTask.AddSubTask("", opWebsiteTask, nil) |
| 53 | + |
| 54 | + go func() { |
| 55 | + _ = opTask.Execute() |
| 56 | + }() |
| 57 | + return nil |
| 58 | +} |
| 59 | + |
| 60 | +func (w WebsiteService) BatchSetGroup(req request.BatchWebsiteGroup) error { |
| 61 | + websites, _ := websiteRepo.List(repo.WithByIDs(req.IDs)) |
| 62 | + for _, web := range websites { |
| 63 | + web.WebsiteGroupID = req.GroupID |
| 64 | + if err := websiteRepo.Save(context.Background(), &web); err != nil { |
| 65 | + return err |
| 66 | + } |
| 67 | + } |
| 68 | + return nil |
| 69 | +} |
| 70 | + |
| 71 | +func (w WebsiteService) BatchSetHttps(ctx context.Context, req request.BatchWebsiteHttps) error { |
| 72 | + websites, _ := websiteRepo.List(repo.WithByIDs(req.IDs)) |
| 73 | + opTask, err := task.NewTaskWithOps(i18n.GetMsgByKey("SSL"), task.TaskBatch, task.TaskScopeWebsite, req.TaskID, 0) |
| 74 | + if err != nil { |
| 75 | + return err |
| 76 | + } |
| 77 | + websiteHttpsOp := request.WebsiteHTTPSOp{ |
| 78 | + Enable: true, |
| 79 | + WebsiteSSLID: req.WebsiteSSLID, |
| 80 | + Type: req.Type, |
| 81 | + PrivateKey: req.PrivateKey, |
| 82 | + Certificate: req.Certificate, |
| 83 | + PrivateKeyPath: req.PrivateKeyPath, |
| 84 | + CertificatePath: req.CertificatePath, |
| 85 | + ImportType: req.ImportType, |
| 86 | + HttpConfig: req.HttpConfig, |
| 87 | + SSLProtocol: req.SSLProtocol, |
| 88 | + Algorithm: req.Algorithm, |
| 89 | + Hsts: req.Hsts, |
| 90 | + HstsIncludeSubDomains: req.HstsIncludeSubDomains, |
| 91 | + HttpsPorts: req.HttpsPorts, |
| 92 | + Http3: req.Http3, |
| 93 | + } |
| 94 | + opWebsiteTask := func(t *task.Task) error { |
| 95 | + for _, web := range websites { |
| 96 | + if web.Type == constant.Stream { |
| 97 | + continue |
| 98 | + } |
| 99 | + websiteHttpsOp.WebsiteID = web.ID |
| 100 | + msg := fmt.Sprintf("%s [%s] %s", i18n.GetMsgByKey("Set"), web.PrimaryDomain, i18n.GetMsgByKey("SSL")) |
| 101 | + if _, err := w.OpWebsiteHTTPS(ctx, websiteHttpsOp); err != nil { |
| 102 | + t.LogFailedWithErr(msg, err) |
| 103 | + continue |
| 104 | + } |
| 105 | + t.LogSuccess(msg) |
| 106 | + } |
| 107 | + return nil |
| 108 | + } |
| 109 | + |
| 110 | + opTask.AddSubTask("", opWebsiteTask, nil) |
| 111 | + go func() { |
| 112 | + _ = opTask.Execute() |
| 113 | + }() |
| 114 | + return nil |
| 115 | +} |
0 commit comments