Creating Echo Hosts on the fly?


#1

Hey,

simple question. I’m using the subdomains example https://echo.labstack.com/cookbook/subdomains and it’s working fine. :smiling_face_with_three_hearts:

According to this recipe you need to declare routes and middleware for each different subdomains. Is there a way to make this more dynamic and smaller in size? :thinking:

For example I want to swap out code lines for general echo instance creation (echo.New()) and let echo create a master host containing host specific necessary parts on the fly, depending on the request.host.

Compiles successful, echo listening but sending empty response and panics on runtime:
echo: http: panic serving [::1]:59138: runtime error: index out of range

Cheers! :grin:


#2

Nevermind. I gave up with this idea and instead (before the main echo instance call) I’m now pre building as many echo instances as there are subdomains including all the desired options for every subdomain (e.g. proxy/path specifications) Working perfect :grin:

Anyone interested in how the solution looks like?

This is my new mainserver.go now:

package main

import (
	_ "github.com/go-sql-driver/mysql"
	"github.com/labstack/echo"
)

func main() {

	// func BuildEchoHosts() (hosts map[string]*Host) {}...
	configuredHosts := BuildEchoHosts()

	e := echo.New()

	e.Debug = true

	e.Any("/*", func(c echo.Context) (err error) {
		req := c.Request()
		res := c.Response()
		host := configuredHosts[req.Host]

		host.Echo.ServeHTTP(res, req)

		return
	})

	e.Logger.Fatal(e.Start(":80"))
}

My old mainserver.go can be found here: Help with Reverse Proxying a subpage/bundle reload

It’s pretty exciting that echo offers me a nice way to level up my programming skills as well as getting in touch with all the different techniques and stuff. Making me very happy to watch every idea becoming reality. Stunning. :star_struck:

Thanks to all contributing to this project! :smiling_face_with_three_hearts: