Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Help with running ld-relay as part of an application #133

Closed
simonkotwicz opened this issue Apr 6, 2021 · 2 comments · Fixed by #134
Closed

Help with running ld-relay as part of an application #133

simonkotwicz opened this issue Apr 6, 2021 · 2 comments · Fixed by #134

Comments

@simonkotwicz
Copy link
Contributor

simonkotwicz commented Apr 6, 2021

Describe the bug
Could you please tell me what I'm doing wrong? I'm trying to build ld-relay within an application.

To reproduce

func main() {
	r, err := relay.NewRelay(createRelayConfig(), ldlog.NewDefaultLoggers(), nil)
	if err != nil {
		log.Fatal(fmt.Sprintf("Error creating relay: %s", err))
	}

	router := mux.NewRouter()
	// router.HandleFunc("/test", handleFunc)
	router.PathPrefix("/relay").Handler(r)

	srv := &http.Server{
		Handler: router,
		Addr:    "127.0.0.1:8030",
	}
	srv.ListenAndServe()
}

func createRelayConfig() config.Config {
	var cfg config.Config
	cfg.Main.Port, _ = configtypes.NewOptIntGreaterThanZero(8030)
	cfg.Environment = map[string]*config.EnvConfig{
		"test": {
			SDKKey: config.SDKKey("-redacted-"),
		},
	}
	return cfg
}

Expected behavior
Expect curl http://localhost:8030/relay/status not to return a 404

@eli-darkly
Copy link
Contributor

I believe the problem has to do with how routers work in gorilla/mux. When you put your router with a path prefix in front of the Relay handler r in this way, router doesn't strip the prefix off of the path before passing it to r. So the Relay handler receives a request for /relay/status, rather than for /status which is what it expects the path to be.

I'm not sure if gorilla/mux has a way to say "pass this request on to another router while stripping the path prefix" when the other router (in this case r) was not created using .Subrouter(); it looks to me like their API kind of assumes that you know what all the router relationships will be ahead of time when you're creating them, which isn't the case here.

But Go does have a helper for this kind of thing: http.StripPrefix. Try this:

    router.PathPrefix("/relay").Handler(http.StripPrefix("/relay", r))

@simonkotwicz
Copy link
Contributor Author

thanks! I made a PR to update the README in case someone else has the same problem.

LaunchDarklyCI pushed a commit that referenced this issue Apr 23, 2021
(v6 - #1) better use of types for config validation + SDK credential abstraction
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants