github.com/aacfactory/fns@v1.2.86-0.20240310083819-80d667fc0a17/clusters/proxy/shareds.go (about) 1 /* 2 * Copyright 2023 Wang Min Xiang 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 * 16 */ 17 18 package proxy 19 20 import ( 21 "bytes" 22 "github.com/aacfactory/errors" 23 "github.com/aacfactory/fns/commons/signatures" 24 "github.com/aacfactory/fns/shareds" 25 "github.com/aacfactory/fns/transports" 26 ) 27 28 var ( 29 sharedHandlerPath = append(handlerPathPrefix, []byte("/clusters/shared")...) 30 sharedHeader = []byte("X-Fns-Shared") 31 ) 32 33 type ClientFetcher func() transports.Client 34 35 func NewShared(client ClientFetcher, signature signatures.Signature) shareds.Shared { 36 return &Shared{ 37 lockers: shareds.LocalLockers(), 38 store: &Store{ 39 client: client, 40 signature: signature, 41 }, 42 } 43 } 44 45 type Shared struct { 46 lockers shareds.Lockers 47 store shareds.Store 48 } 49 50 func (shared *Shared) Construct(_ shareds.Options) (err error) { 51 return 52 } 53 54 func (shared *Shared) Lockers() (lockers shareds.Lockers) { 55 lockers = shared.lockers 56 return 57 } 58 59 func (shared *Shared) Store() (store shareds.Store) { 60 store = shared.store 61 return 62 } 63 64 func (shared *Shared) Close() {} 65 66 // +-------------------------------------------------------------------------------------------------------------------+ 67 68 func NewSharedHandler(shared shareds.Shared) transports.Handler { 69 return &SharedHandler{ 70 store: NewSharedStoreHandler(shared.Store()), 71 } 72 } 73 74 type SharedHandler struct { 75 store transports.Handler 76 } 77 78 func (handler *SharedHandler) Handle(w transports.ResponseWriter, r transports.Request) { 79 kind := r.Header().Get(sharedHeader) 80 if bytes.Equal(kind, sharedHeaderStoreValue) { 81 handler.store.Handle(w, r) 82 } else { 83 w.Failed(errors.Warning("fns: X-Fns-Shared is required")) 84 } 85 return 86 }