2018-09-11 02:49:24 +00:00
|
|
|
// 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 (
|
2018-09-06 20:07:13 +00:00
|
|
|
"context"
|
2015-11-13 19:11:28 +00:00
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
2017-05-04 17:21:25 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2015-11-24 05:18:11 +00:00
|
|
|
|
2017-08-16 21:26:53 +00:00
|
|
|
"github.com/coreos/clair/api/v3"
|
2017-01-27 01:14:44 +00:00
|
|
|
"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
|
|
|
|
2017-01-27 01:14:44 +00:00
|
|
|
// Config is the configuration for the API service.
|
|
|
|
type Config struct {
|
2017-08-18 15:34:19 +00:00
|
|
|
Addr string
|
|
|
|
HealthAddr string
|
2017-01-27 01:14:44 +00:00
|
|
|
Timeout time.Duration
|
|
|
|
CertFile, KeyFile, CAFile string
|
|
|
|
}
|
|
|
|
|
2017-08-18 15:34:19 +00:00
|
|
|
func Run(cfg *Config, store database.Datastore) {
|
2018-09-11 02:49:24 +00:00
|
|
|
err := v3.ListenAndServe(cfg.Addr, cfg.CertFile, cfg.KeyFile, cfg.CAFile, store)
|
2017-06-05 14:37:29 +00:00
|
|
|
if err != nil {
|
2018-09-11 02:49:24 +00:00
|
|
|
log.WithError(err).Fatal("could not initialize gRPC server")
|
2017-06-05 14:37:29 +00:00
|
|
|
}
|
2015-11-13 19:11:28 +00:00
|
|
|
}
|
|
|
|
|
2017-01-27 01:14:44 +00:00
|
|
|
func RunHealth(cfg *Config, store database.Datastore, st *stopper.Stopper) {
|
2016-02-01 20:42:41 +00:00
|
|
|
defer st.End()
|
|
|
|
|
2015-12-07 21:38:50 +00:00
|
|
|
// Do not run the API service if there is no config.
|
2017-01-27 01:14:44 +00:00
|
|
|
if cfg == nil {
|
2017-05-04 17:21:25 +00:00
|
|
|
log.Info("health API service is disabled.")
|
2015-12-07 21:38:50 +00:00
|
|
|
return
|
|
|
|
}
|
2017-08-18 15:34:19 +00:00
|
|
|
log.WithField("addr", cfg.HealthAddr).Info("starting health API")
|
2015-11-13 19:11:28 +00:00
|
|
|
|
2018-09-06 20:07:13 +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()
|
2018-09-06 20:07:13 +00:00
|
|
|
srv.Shutdown(context.TODO())
|
2015-11-13 19:11:28 +00:00
|
|
|
}()
|
|
|
|
|
2018-09-06 20:07:13 +00:00
|
|
|
err := srv.ListenAndServe()
|
|
|
|
if err != nil && err != http.ErrServerClosed {
|
|
|
|
log.Fatal(err)
|
2015-11-13 19:11:28 +00:00
|
|
|
}
|
|
|
|
|
2018-09-06 20:07:13 +00:00
|
|
|
log.Info("health API stopped")
|
2015-11-13 19:11:28 +00:00
|
|
|
}
|