0
Fork 0
mirror of https://github.com/project-zot/zot.git synced 2025-01-06 22:40:28 -05:00
zot/pkg/api/controller.go

70 lines
1.6 KiB
Go
Raw Normal View History

2019-06-20 18:36:40 -05:00
package api
import (
"crypto/tls"
"crypto/x509"
"fmt"
"io/ioutil"
"net"
"net/http"
"github.com/anuvu/zot/pkg/storage"
"github.com/gorilla/mux"
2019-06-20 18:36:40 -05:00
"github.com/rs/zerolog"
)
type Controller struct {
Config *Config
Router *mux.Router
2019-06-20 18:36:40 -05:00
ImageStore *storage.ImageStore
Log zerolog.Logger
Server *http.Server
}
func NewController(config *Config) *Controller {
return &Controller{Config: config, Log: NewLogger(config)}
}
func (c *Controller) Run() error {
engine := mux.NewRouter()
engine.Use(Logger(c.Log))
2019-06-20 18:36:40 -05:00
c.Router = engine
_ = NewRouteHandler(c)
c.Log.Info().Interface("params", c.Config).Msg("configuration settings")
c.ImageStore = storage.NewImageStore(c.Config.Storage.RootDirectory, c.Log)
addr := fmt.Sprintf("%s:%s", c.Config.HTTP.Address, c.Config.HTTP.Port)
server := &http.Server{Addr: addr, Handler: c.Router}
c.Server = server
// Create the listener
l, err := net.Listen("tcp", addr)
if err != nil {
return err
}
clientAuth := tls.VerifyClientCertIfGiven
if c.Config.HTTP.Auth.HTPasswd.Path == "" {
clientAuth = tls.RequireAndVerifyClientCert
}
2019-06-20 18:36:40 -05:00
if c.Config.HTTP.TLS.Key != "" && c.Config.HTTP.TLS.Cert != "" {
if c.Config.HTTP.TLS.CACert != "" {
caCert, err := ioutil.ReadFile(c.Config.HTTP.TLS.CACert)
if err != nil {
panic(err)
}
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM(caCert)
server.TLSConfig = &tls.Config{
ClientAuth: clientAuth,
2019-06-20 18:36:40 -05:00
ClientCAs: caCertPool,
}
}
return server.ServeTLS(l, c.Config.HTTP.TLS.Cert, c.Config.HTTP.TLS.Key)
}
return server.Serve(l)
}