Skip to content

Commit

Permalink
api: add initial work on the new API
Browse files Browse the repository at this point in the history
  • Loading branch information
jzelinskie committed Feb 24, 2016
1 parent b3ddfbc commit 822ac7a
Show file tree
Hide file tree
Showing 10 changed files with 323 additions and 355 deletions.
44 changes: 11 additions & 33 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// Package api provides a RESTful HTTP API, enabling external apps to interact
// with clair.
package api

import (
Expand All @@ -25,37 +23,17 @@ import (
"strconv"
"time"

"github.com/coreos/pkg/capnslog"
"github.com/julienschmidt/httprouter"
"github.com/prometheus/common/log"
"github.com/tylerb/graceful"

"github.com/coreos/clair/api/context"
"github.com/coreos/clair/config"
"github.com/coreos/clair/database"
"github.com/coreos/clair/utils"
)

var log = capnslog.NewPackageLogger("github.com/coreos/clair", "api")
const timeoutResponse = `{"Error":{"Message":"Clair failed to respond within the configured timeout window.","Type":"Timeout"}}`

// Env stores the environment used by the API.
type Env struct {
Datastore database.Datastore
}

// Handle adds a fourth parameter to httprouter.Handle: a pointer to *Env,
// allowing us to pass our environment to the handler.
type Handle func(http.ResponseWriter, *http.Request, httprouter.Params, *Env)

// WrapHandle encloses a Handle into a httprouter.Handle to make it usable by
// httprouter.
func WrapHandle(fn Handle, e *Env) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
fn(w, r, p, e)
}
}

// Run launches the main API, which exposes every possible interactions
// with clair.
func Run(config *config.APIConfig, env *Env, st *utils.Stopper) {
func Run(config *config.APIConfig, ctx *context.RouteContext, st *utils.Stopper) {
defer st.End()

// Do not run the API service if there is no config.
Expand All @@ -79,18 +57,16 @@ func Run(config *config.APIConfig, env *Env, st *utils.Stopper) {
Server: &http.Server{
Addr: ":" + strconv.Itoa(config.Port),
TLSConfig: tlsConfig,
Handler: NewVersionRouter(config.Timeout, env),
Handler: http.TimeoutHandler(newAPIHandler(ctx), config.Timeout, timeoutResponse),
},
}

listenAndServeWithStopper(srv, st, config.CertFile, config.KeyFile)

log.Info("main API stopped")
}

// RunHealth launches the Health API, which only exposes a method to fetch
// Clair's health without any security or authentication mechanism.
func RunHealth(config *config.APIConfig, env *Env, st *utils.Stopper) {
defer st.End()

func RunHealth(config *config.APIConfig, ctx *context.RouteContext, st *utils.Stopper) {
// Do not run the API service if there is no config.
if config == nil {
log.Infof("health API service is disabled.")
Expand All @@ -103,10 +79,12 @@ func RunHealth(config *config.APIConfig, env *Env, st *utils.Stopper) {
NoSignalHandling: true, // We want to use our own Stopper
Server: &http.Server{
Addr: ":" + strconv.Itoa(config.HealthPort),
Handler: NewHealthRouter(env),
Handler: http.TimeoutHandler(newHealthHandler(ctx), config.Timeout, timeoutResponse),
},
}

listenAndServeWithStopper(srv, st, "", "")

log.Info("health API stopped")
}

Expand Down
45 changes: 45 additions & 0 deletions api/context/context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright 2015 clair authors
//
// 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 context

import (
"fmt"
"net/http"

"github.com/coreos/pkg/capnslog"
"github.com/julienschmidt/httprouter"

"github.com/coreos/clair/database"
)

var log = capnslog.NewPackageLogger("github.com/coreos/clair", "api")

type Handler func(http.ResponseWriter, *http.Request, httprouter.Params, *RouteContext) int

func HTTPHandler(handler Handler, ctx *RouteContext) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
status := handler(w, r, p, ctx)
statusStr := fmt.Sprintf("%d", status)
if status == 0 {
statusStr = "???"
}

log.Infof("%s %s %s %s", statusStr, r.Method, r.RequestURI, r.RemoteAddr)
}
}

type RouteContext struct {
Store database.Datastore
}
102 changes: 0 additions & 102 deletions api/handlers.go

This file was deleted.

69 changes: 0 additions & 69 deletions api/logger.go

This file was deleted.

0 comments on commit 822ac7a

Please sign in to comment.