2018-07-02 13:34:04 +02:00
|
|
|
package chartserver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2018-07-05 12:07:00 +02:00
|
|
|
"net/http"
|
2018-07-02 13:34:04 +02:00
|
|
|
"net/url"
|
2018-07-05 12:07:00 +02:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
clientTimeout = 10 * time.Second
|
|
|
|
maxIdleConnections = 10
|
|
|
|
idleConnectionTimeout = 30 * time.Second
|
2018-07-02 13:34:04 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
//Controller is used to handle flows of related requests based on the corresponding handlers
|
|
|
|
//A reverse proxy will be created and managed to proxy the related traffics between API and
|
|
|
|
//backend chart server
|
|
|
|
type Controller struct {
|
|
|
|
//The access endpoint of the backend chart repository server
|
|
|
|
backendServerAddr *url.URL
|
|
|
|
|
|
|
|
//To cover the server info and status requests
|
|
|
|
baseHandler *BaseHandler
|
|
|
|
|
|
|
|
//To cover the chart repository requests
|
|
|
|
repositoryHandler *RepositoryHandler
|
|
|
|
|
|
|
|
//To cover all the manipulation requests
|
|
|
|
manipulationHandler *ManipulationHandler
|
|
|
|
}
|
|
|
|
|
|
|
|
//NewController is constructor of the chartserver.Controller
|
|
|
|
func NewController(backendServer *url.URL) (*Controller, error) {
|
|
|
|
if backendServer == nil {
|
|
|
|
return nil, errors.New("failed to create chartserver.Controller: backend sever address is required")
|
|
|
|
}
|
|
|
|
|
2018-07-03 15:36:20 +02:00
|
|
|
//Use customized reverse proxy
|
|
|
|
proxy := NewProxyEngine(backendServer)
|
2018-07-02 13:34:04 +02:00
|
|
|
|
2018-07-05 12:07:00 +02:00
|
|
|
//Create http client with customized timeouts
|
|
|
|
client := &http.Client{
|
|
|
|
Timeout: clientTimeout,
|
|
|
|
Transport: &http.Transport{
|
|
|
|
MaxIdleConns: maxIdleConnections,
|
|
|
|
IdleConnTimeout: idleConnectionTimeout,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-07-02 13:34:04 +02:00
|
|
|
//Initialize chart operator for use
|
|
|
|
operator := &ChartOperator{}
|
|
|
|
|
|
|
|
return &Controller{
|
|
|
|
backendServerAddr: backendServer,
|
|
|
|
baseHandler: &BaseHandler{proxy},
|
2018-07-05 12:07:00 +02:00
|
|
|
repositoryHandler: &RepositoryHandler{
|
|
|
|
trafficProxy: proxy,
|
|
|
|
apiClient: client,
|
|
|
|
backendServerAddress: backendServer,
|
|
|
|
},
|
2018-07-02 13:34:04 +02:00
|
|
|
manipulationHandler: &ManipulationHandler{
|
|
|
|
trafficProxy: proxy,
|
|
|
|
chartOperator: operator,
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
//GetBaseHandler returns the reference of BaseHandler
|
|
|
|
func (c *Controller) GetBaseHandler() *BaseHandler {
|
|
|
|
return c.baseHandler
|
|
|
|
}
|
|
|
|
|
|
|
|
//GetRepositoryHandler returns the reference of RepositoryHandler
|
|
|
|
func (c *Controller) GetRepositoryHandler() *RepositoryHandler {
|
|
|
|
return c.repositoryHandler
|
|
|
|
}
|
|
|
|
|
|
|
|
//GetManipulationHandler returns the reference of ManipulationHandler
|
|
|
|
func (c *Controller) GetManipulationHandler() *ManipulationHandler {
|
|
|
|
return c.manipulationHandler
|
|
|
|
}
|