feat(proxy): Add reverse proxy feature
This commit is contained in:
46
app/server/api.go
Normal file
46
app/server/api.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"git.secnex.io/secnex/api-gateway/config"
|
||||
)
|
||||
|
||||
type Apis map[string]*Api
|
||||
|
||||
type Api struct {
|
||||
ID string
|
||||
Host *Host
|
||||
Target *Target
|
||||
domain string
|
||||
}
|
||||
|
||||
func NewApis(cfg *config.Configuration, hosts Hosts, targets Targets) Apis {
|
||||
apis := make(Apis)
|
||||
for _, api := range cfg.Apis {
|
||||
apis[api.ID] = NewApi(&api, hosts[api.Host], targets[api.Target])
|
||||
}
|
||||
return apis
|
||||
}
|
||||
|
||||
func NewApi(api *config.Api, host *Host, target *Target) *Api {
|
||||
return &Api{
|
||||
ID: api.ID,
|
||||
Host: host,
|
||||
Target: target,
|
||||
domain: host.Domain,
|
||||
}
|
||||
}
|
||||
|
||||
func (as Apis) GetApi(domain string) *Api {
|
||||
for _, api := range as {
|
||||
if api.domain == domain {
|
||||
return api
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *Api) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
a.Target.proxy.ServeHTTP(w, r)
|
||||
}
|
||||
Reference in New Issue
Block a user