仰望星辰工作室

miaopanel

miaopanel/ internal/plugins/nginx.go 1.2 KB · 41 行 原始文件
1package plugins
2
3import (
4 "context"
5 "os"
6 "path/filepath"
7
8 "github.com/miaopanel/miaopanel/internal/webserver"
9)
10
11// applyPluginNginx writes the fragment shipped in nginx.conf, if any. The
12// manager validates and rolls back on failure like a site configuration.
13func (s *Service) applyPluginNginx(ctx context.Context, slug string) error {
14 src := filepath.Join(s.cfg.PluginDir(slug), "nginx.conf")
15 data, err := os.ReadFile(src)
16 if os.IsNotExist(err) {
17 return nil
18 }
19 if err != nil {
20 return err
21 }
22 if s.nginx == nil || !s.nginx.Available() {
23 // Preview mode (Windows/macOS): keep the fragment for debugging.
24 return os.WriteFile(s.cfg.PluginConfPath(slug), data, 0o644)
25 }
26 return s.nginx.Apply(ctx, &webserver.Render{
27 Files: map[string][]byte{s.cfg.PluginConfPath(slug): data},
28 })
29}
30
31// removePluginNginx deletes the fragment and reloads nginx once.
32func (s *Service) removePluginNginx(ctx context.Context, slug string) error {
33 path := s.cfg.PluginConfPath(slug)
34 if _, err := os.Stat(path); os.IsNotExist(err) {
35 return nil
36 }
37 if s.nginx == nil || !s.nginx.Available() {
38 return os.Remove(path)
39 }
40 return s.nginx.Apply(ctx, &webserver.Render{Remove: []string{path}})
41}