mirror of
https://github.com/goharbor/harbor.git
synced 2024-12-18 14:47:38 +01:00
Merge pull request #10202 from ywk253100/191209_registry
Add the registry handlers without the implementation detail
This commit is contained in:
commit
fbd402fe02
22
src/server/middleware/middleware.go
Normal file
22
src/server/middleware/middleware.go
Normal file
@ -0,0 +1,22 @@
|
||||
// Copyright Project Harbor 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 middleware
|
||||
|
||||
import "net/http"
|
||||
|
||||
// Middleware receives a handler and returns another handler
|
||||
// the returned handler can do some customized task according to
|
||||
// the requirement
|
||||
type Middleware func(http.Handler) http.Handler
|
32
src/server/v2.0/registry/catalog/catalog.go
Normal file
32
src/server/v2.0/registry/catalog/catalog.go
Normal file
@ -0,0 +1,32 @@
|
||||
// Copyright Project Harbor 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 catalog
|
||||
|
||||
import "net/http"
|
||||
|
||||
// NewHandler returns the handler to handler catalog request
|
||||
func NewHandler() http.Handler {
|
||||
return &handler{}
|
||||
}
|
||||
|
||||
type handler struct{}
|
||||
|
||||
func (h *handler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||
// TODO implement
|
||||
}
|
||||
|
||||
type catalogResponse struct {
|
||||
Repositories []string `json:"repositories"`
|
||||
}
|
65
src/server/v2.0/registry/handler.go
Normal file
65
src/server/v2.0/registry/handler.go
Normal file
@ -0,0 +1,65 @@
|
||||
// Copyright Project Harbor 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 registry
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httputil"
|
||||
"net/url"
|
||||
|
||||
"github.com/goharbor/harbor/src/server/v2.0/registry/catalog"
|
||||
"github.com/goharbor/harbor/src/server/v2.0/registry/manifest"
|
||||
"github.com/goharbor/harbor/src/server/v2.0/registry/tag"
|
||||
"github.com/gorilla/mux"
|
||||
)
|
||||
|
||||
// New return the registry instance to handle the registry APIs
|
||||
func New(url *url.URL) http.Handler {
|
||||
// TODO add a director to add the basic auth for docker registry
|
||||
// TODO customize the reverse proxy to improve the performance?
|
||||
proxy := httputil.NewSingleHostReverseProxy(url)
|
||||
|
||||
// create the root rooter
|
||||
rootRouter := mux.NewRouter()
|
||||
rootRouter.StrictSlash(true)
|
||||
|
||||
// handle catalog
|
||||
rootRouter.Path("/v2/_catalog").Methods(http.MethodGet).Handler(catalog.NewHandler())
|
||||
|
||||
// handle list tag
|
||||
rootRouter.Path("/v2/{name}/tags/list").Methods(http.MethodGet).Handler(tag.NewHandler())
|
||||
|
||||
// handle manifest
|
||||
// TODO maybe we should split it into several sub routers based on the method
|
||||
manifestRouter := rootRouter.Path("/v2/{name}/manifests/{reference}").Subrouter()
|
||||
manifestRouter.NewRoute().Methods(http.MethodGet, http.MethodHead, http.MethodPut, http.MethodDelete).
|
||||
Handler(manifest.NewHandler(proxy))
|
||||
|
||||
// handle blob
|
||||
// as we need to apply middleware to the blob requests, so create a sub router to handle the blob APIs
|
||||
blobRouter := rootRouter.Path("/v2/{name}/blobs/").Subrouter()
|
||||
blobRouter.NewRoute().Methods(http.MethodGet, http.MethodHead, http.MethodPost, http.MethodPut, http.MethodPatch, http.MethodDelete).
|
||||
Handler(proxy)
|
||||
|
||||
// all other APIs are proxy to the backend docker registry
|
||||
rootRouter.PathPrefix("/").Handler(proxy)
|
||||
|
||||
// register middlewares
|
||||
// TODO add auth middleware
|
||||
// TODO apply the existing middlewares
|
||||
// rootRouter.Use(mux.MiddlewareFunc(middleware))
|
||||
|
||||
return rootRouter
|
||||
}
|
64
src/server/v2.0/registry/manifest/manifest.go
Normal file
64
src/server/v2.0/registry/manifest/manifest.go
Normal file
@ -0,0 +1,64 @@
|
||||
// Copyright Project Harbor 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 manifest
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httputil"
|
||||
)
|
||||
|
||||
// NewHandler returns the handler to handler manifest requests
|
||||
func NewHandler(proxy *httputil.ReverseProxy) http.Handler {
|
||||
return &handler{
|
||||
proxy: proxy,
|
||||
}
|
||||
}
|
||||
|
||||
type handler struct {
|
||||
proxy *httputil.ReverseProxy
|
||||
}
|
||||
|
||||
func (h *handler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||
switch req.Method {
|
||||
case http.MethodHead:
|
||||
h.head(w, req)
|
||||
case http.MethodGet:
|
||||
h.get(w, req)
|
||||
case http.MethodDelete:
|
||||
h.delete(w, req)
|
||||
case http.MethodPut:
|
||||
h.put(w, req)
|
||||
}
|
||||
}
|
||||
|
||||
// make sure the artifact exist before proxying the request to the backend registry
|
||||
func (h *handler) head(w http.ResponseWriter, req *http.Request) {
|
||||
// TODO check the existence
|
||||
h.proxy.ServeHTTP(w, req)
|
||||
}
|
||||
|
||||
// make sure the artifact exist before proxying the request to the backend registry
|
||||
func (h *handler) get(w http.ResponseWriter, req *http.Request) {
|
||||
// TODO check the existence
|
||||
h.proxy.ServeHTTP(w, req)
|
||||
}
|
||||
|
||||
func (h *handler) delete(w http.ResponseWriter, req *http.Request) {
|
||||
// TODO implement, just delete from database
|
||||
}
|
||||
|
||||
func (h *handler) put(w http.ResponseWriter, req *http.Request) {
|
||||
h.proxy.ServeHTTP(w, req)
|
||||
}
|
36
src/server/v2.0/registry/tag/tag.go
Normal file
36
src/server/v2.0/registry/tag/tag.go
Normal file
@ -0,0 +1,36 @@
|
||||
// Copyright Project Harbor 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 tag
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// NewHandler returns the handler to handle listing tag request
|
||||
func NewHandler() http.Handler {
|
||||
return &handler{}
|
||||
}
|
||||
|
||||
type handler struct{}
|
||||
|
||||
func (h *handler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||
// TODO implement
|
||||
// support pagination, this is required for OCI registry spec
|
||||
}
|
||||
|
||||
type listTagResponse struct {
|
||||
Name string `json:"name"`
|
||||
Tags []string `json:"tags"`
|
||||
}
|
Loading…
Reference in New Issue
Block a user