| 28 | ) |
| 29 | |
| 30 | func setup(ctx context.Context, config cfg, onClose func(err error)) (io.Closer, error) { |
| 31 | httpMux := http.NewServeMux() |
| 32 | httpMux.HandleFunc("/health", func(w http.ResponseWriter, _ *http.Request) { |
| 33 | w.WriteHeader(http.StatusOK) |
| 34 | _, _ = w.Write([]byte("ok")) |
| 35 | }) |
| 36 | closed := make(chan struct{}) |
| 37 | once := sync.OnceFunc(func() { close(closed) }) |
| 38 | httpMux.Handle(pluginsv1connect.NewPluginServiceHandler(&pluginService{func(context.Context) { |
| 39 | once() |
| 40 | }})) |
| 41 | setupCompleted := make(chan struct{}) |
| 42 | httpMux.Handle(resolverv1connect.NewResolverServiceHandler(&resolverService{ |
| 43 | handler: resolverv1.NewResolverHandler(config.plugin), |
| 44 | setupCompleted: setupCompleted, |
| 45 | registrationTimeout: config.registrationTimeout, |
| 46 | })) |
| 47 | ipc, c, err := ipc.NewClientIPC(config.Logger, config.conn, httpMux, func(err error) { |
| 48 | if errors.Is(err, io.EOF) { |
| 49 | config.Logger.Printf("Plugin runtime stopped, plugin %s is shutting down...", config.name) |
| 50 | err = nil // In the context of a plugin, the runtime shutting down IPC/plugin is not an error. |
| 51 | } |
| 52 | onClose(err) |
| 53 | }) |
| 54 | if err != nil { |
| 55 | return nil, err |
| 56 | } |
| 57 | runtimeCfg, err := doRegister(ctx, c, config.name, config.Config, config.registrationTimeout) |
| 58 | if err != nil { |
| 59 | ipc.Close() |
| 60 | return nil, err |
| 61 | } |
| 62 | go func() { |
| 63 | <-closed |
| 64 | ipc.Close() |
| 65 | }() |
| 66 | config.Logger.Printf("Started plugin (runtime: %s@%s) %s...", runtimeCfg.Engine, runtimeCfg.Version, config.name) |
| 67 | close(setupCompleted) |
| 68 | return ipc, nil |
| 69 | } |