feat(proxy): Add reverse proxy feature
This commit is contained in:
43
app/server/target.go
Normal file
43
app/server/target.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"net/http/httputil"
|
||||
"net/url"
|
||||
|
||||
"git.secnex.io/secnex/api-gateway/config"
|
||||
"git.secnex.io/secnex/masterlog"
|
||||
)
|
||||
|
||||
type Targets map[string]*Target
|
||||
|
||||
type Target struct {
|
||||
ID string
|
||||
Name string
|
||||
URL *url.URL
|
||||
proxy *httputil.ReverseProxy
|
||||
}
|
||||
|
||||
func NewTargets(cfg *config.Configuration) Targets {
|
||||
targets := make(Targets)
|
||||
for _, target := range cfg.Targets {
|
||||
targets[target.ID] = NewTarget(&target)
|
||||
}
|
||||
return targets
|
||||
}
|
||||
|
||||
func NewTarget(target *config.Target) *Target {
|
||||
url, err := url.Parse(target.URL)
|
||||
if err != nil {
|
||||
masterlog.Error("Failed to parse target URL", map[string]interface{}{
|
||||
"error": err,
|
||||
"target": target.URL,
|
||||
})
|
||||
return nil
|
||||
}
|
||||
return &Target{
|
||||
ID: target.ID,
|
||||
Name: target.Name,
|
||||
URL: url,
|
||||
proxy: httputil.NewSingleHostReverseProxy(url),
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user