-
Notifications
You must be signed in to change notification settings - Fork 9
/
distribution.go
77 lines (65 loc) · 2.04 KB
/
distribution.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Copyright 2023 sigma
//
// 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 distribution
import (
"errors"
"fmt"
"net/http"
"sort"
"github.com/labstack/echo/v4"
"github.com/rs/zerolog/log"
"github.com/go-sigma/sigma/pkg/consts"
)
var (
// ErrNext continue next router
ErrNext = fmt.Errorf("continue next router")
)
// All handles the all request
func All(c echo.Context) error {
c.Response().Header().Set(consts.APIVersionKey, consts.APIVersionValue)
for index, factory := range routerFactories {
err := factory.Value.Initialize(c)
if err != nil {
if errors.Is(err, ErrNext) {
continue
}
return fmt.Errorf("failed to initialize router factory index(%d): %v", index, err)
}
return nil
}
log.Error().Str("Uri", c.Request().RequestURI).Str("Method", c.Request().Method).Msg("Uri cannot match any route")
return c.NoContent(http.StatusMethodNotAllowed)
}
// Factory is the interface for the storage router factory
type Factory interface {
Initialize(ctx echo.Context) error
}
type Item struct {
Key int
Value Factory
}
var routerFactories = make([]Item, 0, 10)
// RegisterRouterFactory registers a new router factory
func RegisterRouterFactory(factory Factory, index int) error {
for _, router := range routerFactories {
if router.Key == index {
return fmt.Errorf("router %d already registered", index)
}
}
routerFactories = append(routerFactories, Item{Key: index, Value: factory})
sort.SliceStable(routerFactories, func(i, j int) bool {
return routerFactories[i].Key < routerFactories[j].Key
})
return nil
}