47 lines
801 B
Go
47 lines
801 B
Go
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)
|
|
}
|