|
| 1 | +// Copyright 2023 The Kubegems Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package api |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "net/http" |
| 20 | + |
| 21 | + "kubegems.io/library/rest/listen" |
| 22 | +) |
| 23 | + |
| 24 | +type API struct { |
| 25 | + tls tlsfiles |
| 26 | + filter FilterHolder |
| 27 | + plugins []Plugin |
| 28 | + mux Router |
| 29 | +} |
| 30 | + |
| 31 | +type tlsfiles struct { |
| 32 | + crt string |
| 33 | + key string |
| 34 | +} |
| 35 | + |
| 36 | +func NewAPI() *API { |
| 37 | + return &API{ |
| 38 | + mux: NewMux(), |
| 39 | + filter: NewFilters(), |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +type Predicate func(r *http.Request) bool |
| 44 | + |
| 45 | +func (m *API) Filter(pattern string, filters ...Filter) *API { |
| 46 | + if err := m.filter.Register(pattern, filters...); err != nil { |
| 47 | + panic(err) |
| 48 | + } |
| 49 | + return m |
| 50 | +} |
| 51 | + |
| 52 | +func (m *API) Route(route Route) *API { |
| 53 | + if err := m.mux.HandleRoute(&route); err != nil { |
| 54 | + panic(err) |
| 55 | + } |
| 56 | + for _, plugin := range m.plugins { |
| 57 | + if err := plugin.OnRoute(&route); err != nil { |
| 58 | + panic(err) |
| 59 | + } |
| 60 | + } |
| 61 | + return m |
| 62 | +} |
| 63 | + |
| 64 | +func (m *API) NotFound(handler http.Handler) *API { |
| 65 | + m.mux.SetNotFound(handler) |
| 66 | + return m |
| 67 | +} |
| 68 | + |
| 69 | +func (m *API) Register(prefix string, modules ...Module) *API { |
| 70 | + rg := NewGroup(prefix) |
| 71 | + for _, module := range modules { |
| 72 | + rg = rg.SubGroup(module.Routes()...) |
| 73 | + } |
| 74 | + for _, methods := range rg.Build() { |
| 75 | + for _, route := range methods { |
| 76 | + m.Route(route) |
| 77 | + } |
| 78 | + } |
| 79 | + return m |
| 80 | +} |
| 81 | + |
| 82 | +func (m *API) Build() http.Handler { |
| 83 | + return http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) { |
| 84 | + m.filter.Process(resp, req, m.mux) |
| 85 | + }) |
| 86 | +} |
| 87 | + |
| 88 | +func (m *API) TLS(cert, key string) *API { |
| 89 | + m.tls = tlsfiles{crt: cert, key: key} |
| 90 | + return m |
| 91 | +} |
| 92 | + |
| 93 | +func (m *API) Serve(ctx context.Context, listenaddr string) error { |
| 94 | + return listen.ServeContext(ctx, listenaddr, m.Build(), m.tls.crt, m.tls.key) |
| 95 | +} |
| 96 | + |
| 97 | +func (m *API) Plugin(plugin ...Plugin) *API { |
| 98 | + for _, p := range plugin { |
| 99 | + if err := p.Install(m); err != nil { |
| 100 | + panic(err) |
| 101 | + } |
| 102 | + m.plugins = append(m.plugins, p) |
| 103 | + } |
| 104 | + return m |
| 105 | +} |
0 commit comments