-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: migrate from Gin to Echo framework
- Migrated from Gin to Echo framework to improve performance and routing capabilities. - Updated `go.mod` and `go.sum` to include Echo and its dependencies. - Refactored route definitions and handlers to use Echo's API. - Improved error handling and assertions in test cases using Testify. - Introduced middleware for host-based routing with secure handling for unknown hosts.
- Loading branch information
1 parent
e2e9d50
commit b6f030f
Showing
4 changed files
with
93 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,63 @@ | ||
package hostroute | ||
|
||
import ( | ||
"fmt" | ||
"github.com/labstack/echo/v4" | ||
"net/http" | ||
) | ||
|
||
type HostConfig struct { | ||
Host string | ||
Prefix string | ||
RouterFactory func(e *echo.Group) | ||
} | ||
|
||
func createHostBasedRoutingMiddleware(hostConfigMap map[string]*HostConfig, genericHosts map[string]bool, secureAgainstUnknownHosts bool) echo.MiddlewareFunc { | ||
return func(next echo.HandlerFunc) echo.HandlerFunc { | ||
return func(c echo.Context) error { | ||
host := c.Request().Host | ||
|
||
if _, exists := hostConfigMap[host]; exists { | ||
return next(c) | ||
} | ||
|
||
if _, exists := genericHosts[host]; exists { | ||
return next(c) | ||
} | ||
|
||
if secureAgainstUnknownHosts { | ||
return c.String(http.StatusNotFound, "Unknown host") | ||
} | ||
|
||
return next(c) | ||
} | ||
} | ||
} | ||
|
||
func SetupHostBasedRoutes(e *echo.Echo, hostConfigs []HostConfig, genericHosts []string, secureAgainstUnknownHosts bool) { | ||
hostConfigMap := make(map[string]*HostConfig) | ||
genericHostsMap := stringSliceToMap(genericHosts) | ||
|
||
for i := range hostConfigs { | ||
group := e.Host(hostConfigs[i].Host) | ||
hostConfigs[i].RouterFactory(group) | ||
|
||
if hostConfigs[i].Prefix != "" { | ||
group = e.Group(fmt.Sprintf("/%s", hostConfigs[i].Prefix)) | ||
hostConfigs[i].RouterFactory(group) | ||
} | ||
|
||
hostConfigMap[hostConfigs[i].Host] = &hostConfigs[i] | ||
} | ||
|
||
e.Use(createHostBasedRoutingMiddleware(hostConfigMap, genericHostsMap, secureAgainstUnknownHosts)) | ||
|
||
} | ||
|
||
func stringSliceToMap(slice []string) map[string]bool { | ||
result := make(map[string]bool) | ||
for _, s := range slice { | ||
result[s] = true | ||
} | ||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters