2015-11-13 19:11:28 +00:00
|
|
|
// 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"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/julienschmidt/httprouter"
|
2016-01-27 19:07:58 +00:00
|
|
|
|
|
|
|
"github.com/coreos/clair/api/context"
|
|
|
|
"github.com/coreos/clair/api/v1"
|
2015-11-13 19:11:28 +00:00
|
|
|
)
|
|
|
|
|
2016-01-27 19:07:58 +00:00
|
|
|
// router is an HTTP router that forwards requests to the appropriate sub-router
|
|
|
|
// depending on the API version specified in the request URI.
|
|
|
|
type router map[string]*httprouter.Router
|
2015-11-13 19:11:28 +00:00
|
|
|
|
2016-01-27 19:07:58 +00:00
|
|
|
// Let's hope we never have more than 99 API versions.
|
|
|
|
const apiVersionLength = len("v99")
|
|
|
|
|
|
|
|
func newAPIHandler(ctx *context.RouteContext) http.Handler {
|
|
|
|
router := make(router)
|
2016-01-28 20:13:11 +00:00
|
|
|
router["/v1"] = v1.NewRouter(ctx)
|
2016-01-27 19:07:58 +00:00
|
|
|
return router
|
2015-11-13 19:11:28 +00:00
|
|
|
}
|
|
|
|
|
2016-01-27 19:07:58 +00:00
|
|
|
func (rtr router) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
2015-11-13 19:11:28 +00:00
|
|
|
urlStr := r.URL.String()
|
|
|
|
var version string
|
2016-01-27 19:07:58 +00:00
|
|
|
if len(urlStr) >= apiVersionLength {
|
|
|
|
version = urlStr[:apiVersionLength]
|
2015-11-13 19:11:28 +00:00
|
|
|
}
|
2016-01-27 19:07:58 +00:00
|
|
|
|
|
|
|
if router, _ := rtr[version]; router != nil {
|
2015-11-13 19:11:28 +00:00
|
|
|
// Remove the version number from the request path to let the router do its
|
|
|
|
// job but do not update the RequestURI
|
|
|
|
r.URL.Path = strings.Replace(r.URL.Path, version, "", 1)
|
|
|
|
router.ServeHTTP(w, r)
|
|
|
|
return
|
|
|
|
}
|
2016-01-27 19:07:58 +00:00
|
|
|
|
2016-01-28 20:13:11 +00:00
|
|
|
log.Infof("%s %d %s %s", http.StatusNotFound, r.Method, r.RequestURI, r.RemoteAddr)
|
2015-11-13 19:11:28 +00:00
|
|
|
http.NotFound(w, r)
|
|
|
|
}
|
|
|
|
|
2016-01-27 19:07:58 +00:00
|
|
|
func newHealthHandler(ctx *context.RouteContext) http.Handler {
|
2015-11-13 19:11:28 +00:00
|
|
|
router := httprouter.New()
|
2016-01-27 19:07:58 +00:00
|
|
|
router.GET("/health", context.HTTPHandler(getHealth, ctx))
|
2015-11-13 19:11:28 +00:00
|
|
|
return router
|
|
|
|
}
|
|
|
|
|
2016-02-04 20:52:44 +00:00
|
|
|
func getHealth(w http.ResponseWriter, r *http.Request, p httprouter.Params, ctx *context.RouteContext) (string, int) {
|
|
|
|
if ctx.Store.Ping() {
|
|
|
|
return "health", http.StatusOK
|
|
|
|
}
|
|
|
|
return "health", http.StatusInternalServerError
|
2015-11-13 19:11:28 +00:00
|
|
|
}
|