1
0
Fork 0
mirror of https://github.com/imjasonh/kontain.me synced 2026-07-22 15:51:12 +00:00

resolve TODOs in pkg/serve

This commit is contained in:
Jason Hall 2020-12-31 07:44:38 -05:00
parent d0af4eaa9b
commit ce2d2cc45c
7 changed files with 122 additions and 108 deletions

View file

@ -1,20 +1,7 @@
// Copyright 2020 Google LLC All Rights Reserved.
//
// 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 main
import (
"context"
"fmt"
"log"
"net/http"
@ -28,9 +15,15 @@ import (
)
func main() {
ctx := context.Background()
st, err := serve.NewStorage(ctx)
if err != nil {
log.Fatalf("serve.NewStorage: %v", err)
}
http.Handle("/v2/", &server{
info: log.New(os.Stdout, "I ", log.Ldate|log.Ltime|log.Lshortfile),
error: log.New(os.Stderr, "E ", log.Ldate|log.Ltime|log.Lshortfile),
info: log.New(os.Stdout, "I ", log.Ldate|log.Ltime|log.Lshortfile),
error: log.New(os.Stderr, "E ", log.Ldate|log.Ltime|log.Lshortfile),
storage: st,
})
http.Handle("/", http.RedirectHandler("https://github.com/imjasonh/kontain.me/blob/master/cmd/mirror", http.StatusSeeOther))
@ -46,6 +39,7 @@ func main() {
type server struct {
info, error *log.Logger
storage *serve.Storage
}
func (s *server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
@ -73,6 +67,7 @@ func (s *server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// mirror.kontain.me/ubuntu -> mirror ubuntu and serve
func (s *server) serveMirrorManifest(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
path := strings.TrimPrefix(r.URL.Path, "/v2/")
parts := strings.Split(path, "/")
@ -102,7 +97,7 @@ func (s *server) serveMirrorManifest(w http.ResponseWriter, r *http.Request) {
serve.Error(w, err)
return
}
if err := serve.BlobExists(d.Digest.String()); err == nil {
if err := s.storage.BlobExists(ctx, d.Digest.String()); err == nil {
serve.Blob(w, r, d.Digest.String())
return
} else {
@ -121,8 +116,8 @@ func (s *server) serveMirrorManifest(w http.ResponseWriter, r *http.Request) {
serve.Error(w, err)
return
}
if err := serve.Index(w, r, idx); err != nil {
s.error.Printf("ERROR (serve.Index): %v", err)
if err := s.storage.ServeIndex(w, r, idx); err != nil {
s.error.Printf("ERROR (storage.ServeIndex): %v", err)
serve.Error(w, err)
return
}
@ -135,8 +130,8 @@ func (s *server) serveMirrorManifest(w http.ResponseWriter, r *http.Request) {
serve.Error(w, err)
return
}
if err := serve.Manifest(w, r, img); err != nil {
s.error.Printf("ERROR (serve.Manifest): %v", err)
if err := s.storage.ServeManifest(w, r, img); err != nil {
s.error.Printf("ERROR (storage.ServeManifest): %v", err)
serve.Error(w, err)
return
}