clair/api/api.go

73 lines
1.9 KiB
Go
Raw Normal View History

// Copyright 2018 clair authors
2015-11-13 19:11:28 +00:00
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package api
import (
"context"
2015-11-13 19:11:28 +00:00
"net/http"
"time"
log "github.com/sirupsen/logrus"
"github.com/coreos/clair/api/v3"
"github.com/coreos/clair/database"
2017-01-18 03:18:03 +00:00
"github.com/coreos/clair/pkg/stopper"
2015-11-13 19:11:28 +00:00
)
2016-01-27 19:07:58 +00:00
const timeoutResponse = `{"Error":{"Message":"Clair failed to respond within the configured timeout window.","Type":"Timeout"}}`
2015-11-13 19:11:28 +00:00
// Config is the configuration for the API service.
type Config struct {
Addr string
HealthAddr string
Timeout time.Duration
CertFile, KeyFile, CAFile string
}
func Run(cfg *Config, store database.Datastore) {
err := v3.ListenAndServe(cfg.Addr, cfg.CertFile, cfg.KeyFile, cfg.CAFile, store)
if err != nil {
log.WithError(err).Fatal("could not initialize gRPC server")
}
2015-11-13 19:11:28 +00:00
}
func RunHealth(cfg *Config, store database.Datastore, st *stopper.Stopper) {
2016-02-01 20:42:41 +00:00
defer st.End()
// Do not run the API service if there is no config.
if cfg == nil {
log.Info("health API service is disabled.")
return
}
log.WithField("addr", cfg.HealthAddr).Info("starting health API")
2015-11-13 19:11:28 +00:00
srv := http.Server{
Addr: cfg.HealthAddr,
Handler: http.TimeoutHandler(newHealthHandler(store), cfg.Timeout, timeoutResponse),
2015-11-13 19:11:28 +00:00
}
2016-01-27 19:07:58 +00:00
2015-11-13 19:11:28 +00:00
go func() {
<-st.Chan()
srv.Shutdown(context.TODO())
2015-11-13 19:11:28 +00:00
}()
err := srv.ListenAndServe()
if err != nil && err != http.ErrServerClosed {
log.Fatal(err)
2015-11-13 19:11:28 +00:00
}
log.Info("health API stopped")
2015-11-13 19:11:28 +00:00
}