Skip to content

Commit

Permalink
*: refactor & do initial work towards PostgreSQL implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Quentin-M authored and jzelinskie committed Feb 24, 2016
1 parent 1a0f4a0 commit 2c150b0
Show file tree
Hide file tree
Showing 72 changed files with 2,397 additions and 4,392 deletions.
92 changes: 0 additions & 92 deletions Godeps/Godeps.json

This file was deleted.

5 changes: 0 additions & 5 deletions Godeps/Readme

This file was deleted.

27 changes: 23 additions & 4 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,36 @@ import (
"time"

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

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

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

// 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, st *utils.Stopper) {
func Run(config *config.APIConfig, env *Env, st *utils.Stopper) {
defer st.End()

// Do not run the API service if there is no config.
Expand All @@ -60,7 +79,7 @@ func Run(config *config.APIConfig, st *utils.Stopper) {
Server: &http.Server{
Addr: ":" + strconv.Itoa(config.Port),
TLSConfig: tlsConfig,
Handler: NewVersionRouter(config.Timeout),
Handler: NewVersionRouter(config.Timeout, env),
},
}
listenAndServeWithStopper(srv, st, config.CertFile, config.KeyFile)
Expand All @@ -69,7 +88,7 @@ func Run(config *config.APIConfig, st *utils.Stopper) {

// 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, st *utils.Stopper) {
func RunHealth(config *config.APIConfig, env *Env, st *utils.Stopper) {
defer st.End()

// Do not run the API service if there is no config.
Expand All @@ -84,7 +103,7 @@ func RunHealth(config *config.APIConfig, st *utils.Stopper) {
NoSignalHandling: true, // We want to use our own Stopper
Server: &http.Server{
Addr: ":" + strconv.Itoa(config.HealthPort),
Handler: NewHealthRouter(),
Handler: NewHealthRouter(env),
},
}
listenAndServeWithStopper(srv, st, "", "")
Expand Down
109 changes: 109 additions & 0 deletions api/handlers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// 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 api

import (
"net/http"
"strconv"

"github.com/julienschmidt/httprouter"

"github.com/coreos/clair/database"
"github.com/coreos/clair/health"
httputils "github.com/coreos/clair/utils/http"
"github.com/coreos/clair/worker"
)

// Version is an integer representing the API version.
const Version = 1

// POSTLayersParameters represents the expected parameters for POSTLayers.
type POSTLayersParameters struct {
Name, Path, ParentName string
}

// GETVersions returns API and Engine versions.
func GETVersions(w http.ResponseWriter, r *http.Request, _ httprouter.Params, _ *Env) {
httputils.WriteHTTP(w, http.StatusOK, struct {
APIVersion string
EngineVersion string
}{
APIVersion: strconv.Itoa(Version),
EngineVersion: strconv.Itoa(worker.Version),
})
}

// GETHealth sums up the health of all the registered services.
func GETHealth(w http.ResponseWriter, r *http.Request, _ httprouter.Params, _ *Env) {
globalHealth, statuses := health.Healthcheck()

httpStatus := http.StatusOK
if !globalHealth {
httpStatus = http.StatusServiceUnavailable
}

httputils.WriteHTTP(w, httpStatus, statuses)
return
}

// POSTLayers analyzes a layer and returns the engine version that has been used
// for the analysis.
func POSTLayers(w http.ResponseWriter, r *http.Request, _ httprouter.Params, e *Env) {
var parameters POSTLayersParameters
if s, err := httputils.ParseHTTPBody(r, &parameters); err != nil {
httputils.WriteHTTPError(w, s, err)
return
}

// Process data.
if err := worker.Process(e.Datastore, parameters.Name, parameters.ParentName, parameters.Path); err != nil {
httputils.WriteHTTPError(w, 0, err)
return
}

// Get engine version and return.
httputils.WriteHTTP(w, http.StatusCreated, struct{ Version string }{Version: strconv.Itoa(worker.Version)})
}

// DELETELayers deletes the specified layer and any child layers that are
// dependent on the specified layer.
func DELETELayers(w http.ResponseWriter, r *http.Request, p httprouter.Params, e *Env) {
if err := e.Datastore.DeleteLayer(p.ByName("id")); err != nil {
httputils.WriteHTTPError(w, 0, err)
return
}
httputils.WriteHTTP(w, http.StatusNoContent, nil)
}

// GETLayers returns informations about an existing layer, optionally with its features
// and vulnerabilities.
func GETLayers(w http.ResponseWriter, r *http.Request, p httprouter.Params, e *Env) {
withFeatures := false
withVulnerabilities := false
if r.URL.Query().Get("withFeatures") == "true" {
withFeatures = true
}
if r.URL.Query().Get("withVulnerabilities") == "true" {
withFeatures = true
withVulnerabilities = true
}

layer, err := e.Datastore.FindLayer(p.ByName("id"), withFeatures, withVulnerabilities)
if err != nil {
httputils.WriteHTTPError(w, 0, err)
return
}
httputils.WriteHTTP(w, http.StatusOK, struct{ Layer database.Layer }{Layer: layer})
}
9 changes: 3 additions & 6 deletions api/wrappers/log.go → api/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,15 @@
// limitations under the License.

// Package wrappers contains httprouter.Handle wrappers that are used in the API.
package wrappers
package api

import (
"net/http"
"time"

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

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

type logWriter struct {
http.ResponseWriter
status int
Expand Down Expand Up @@ -61,8 +58,8 @@ func (lw *logWriter) Status() int {
return lw.status
}

// Log wraps a http.HandlerFunc and logs the API call
func Log(fn httprouter.Handle) httprouter.Handle {
// Logger wraps an Handler and logs the API call
func Logger(fn httprouter.Handle) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
lw := &logWriter{ResponseWriter: w}
start := time.Now()
Expand Down
55 changes: 0 additions & 55 deletions api/logic/general.go

This file was deleted.

0 comments on commit 2c150b0

Please sign in to comment.