|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "flag" |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + "strings" |
| 10 | + |
| 11 | + // Packages |
| 12 | + "github.com/mutablelogic/go-server/pkg/provider" |
| 13 | +) |
| 14 | + |
| 15 | +func main() { |
| 16 | + var pluginPath string |
| 17 | + name := filepath.Base(os.Args[0]) |
| 18 | + flags := flag.NewFlagSet(name, flag.ContinueOnError) |
| 19 | + flags.StringVar(&pluginPath, "plugin", "*.plugin", "Path to plugins") |
| 20 | + if err := flags.Parse(os.Args[1:]); err != nil { |
| 21 | + os.Exit(1) |
| 22 | + } |
| 23 | + |
| 24 | + // If the path is relative, then make it absolute to either the binary path |
| 25 | + // or the current working directory |
| 26 | + if !filepath.IsAbs(pluginPath) { |
| 27 | + if strings.HasPrefix(pluginPath, ".") || strings.HasPrefix(pluginPath, "..") { |
| 28 | + if wd, err := os.Getwd(); err == nil { |
| 29 | + pluginPath = filepath.Clean(filepath.Join(wd, pluginPath)) |
| 30 | + } |
| 31 | + } else if exec, err := os.Executable(); err == nil { |
| 32 | + pluginPath = filepath.Clean(filepath.Join(filepath.Dir(exec), pluginPath)) |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + // Create a new provider, load plugins |
| 37 | + provider, err := provider.New() |
| 38 | + if err != nil { |
| 39 | + fmt.Fprintln(os.Stderr, err) |
| 40 | + os.Exit(1) |
| 41 | + } |
| 42 | + if err := provider.LoadPluginsForPattern(pluginPath); err != nil { |
| 43 | + fmt.Fprintln(os.Stderr, err) |
| 44 | + os.Exit(1) |
| 45 | + } |
| 46 | + |
| 47 | + // Create configurations |
| 48 | + var result error |
| 49 | + for _, plugin := range []string{"logger", "httpserver", "router", "nginx-handler", "auth-handler", "tokenjar-handler"} { |
| 50 | + if _, err := provider.New(plugin); err != nil { |
| 51 | + result = errors.Join(result, err) |
| 52 | + } |
| 53 | + } |
| 54 | + if result != nil { |
| 55 | + fmt.Fprintln(os.Stderr, result) |
| 56 | + os.Exit(1) |
| 57 | + } |
| 58 | + |
| 59 | + // TODO: Set parameters from a JSON file |
| 60 | + provider.Set("logger.flags", []string{"default", "prefix"}) |
| 61 | +} |
| 62 | + |
| 63 | +/* |
| 64 | +{ |
| 65 | + "logger": { |
| 66 | + "flags": ["default", "prefix"] |
| 67 | + }, |
| 68 | + "nginx": { |
| 69 | + "binary": "/usr/local/bin/nginx", |
| 70 | + "data": "/var/run/nginx", |
| 71 | + "group": "www-data", |
| 72 | + }, |
| 73 | + httpserver": { |
| 74 | + "listen": "run/go-server.sock", |
| 75 | + "group": "www-data", |
| 76 | + "router": "${ router }", |
| 77 | + }, |
| 78 | + "router": { |
| 79 | + "services": { |
| 80 | + "nginx": { |
| 81 | + "service": "${ nginx }", |
| 82 | + "middleware": ["logger", "auth"] |
| 83 | + }, |
| 84 | + "auth": { |
| 85 | + "service": "${ auth }", |
| 86 | + "middleware": ["logger", "auth"] |
| 87 | + }, |
| 88 | + "router": { |
| 89 | + "service": "${ router }", |
| 90 | + "middleware": ["logger", "auth"] |
| 91 | + }, |
| 92 | + }, |
| 93 | + "auth": { |
| 94 | + "tokenjar": "${ tokenjar }", |
| 95 | + "tokenbytes": 16, |
| 96 | + "bearer": true, |
| 97 | + }, |
| 98 | + "tokenjar": { |
| 99 | + "data": "run", |
| 100 | + "writeinterval": "30s", |
| 101 | + }, |
| 102 | +} |
| 103 | +*/ |
0 commit comments