1 // Licensed under the Apache License, Version 2.0 (the "License"); you may not 2 // use this file except in compliance with the License. You may obtain a copy of 3 // the License at 4 // 5 // http://www.apache.org/licenses/LICENSE-2.0 6 // 7 // Unless required by applicable law or agreed to in writing, software 8 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 9 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 10 // License for the specific language governing permissions and limitations under 11 // the License. 12 13 //go:build !js 14 15 package server 16 17 import ( 18 "github.com/go-kivik/kivik/v4/x/server/auth" 19 "github.com/go-kivik/kivik/v4/x/server/config" 20 ) 21 22 // Option is a server option. 23 type Option interface { 24 apply(*Server) 25 } 26 27 type authHandlerOption []auth.Handler 28 29 func (h authHandlerOption) apply(s *Server) { 30 for _, handler := range h { 31 _, auth := handler.Init(&authService{s}) 32 s.authFuncs = append(s.authFuncs, auth) 33 } 34 } 35 36 // WithAuthHandlers adds the provided auth handlers to the server. May be 37 // specified more than once. Order is significant. Each auth request is passed 38 // through each handler in the order specified, until one returns a user 39 // context or an error. If no handlers are specified, the server will operate 40 // as a PERPETUAL ADMIN PARTY! 41 func WithAuthHandlers(h ...auth.Handler) Option { 42 return authHandlerOption(h) 43 } 44 45 type userStoreOption []auth.UserStore 46 47 func (s userStoreOption) apply(srv *Server) { 48 for _, store := range s { 49 srv.userStores = append(srv.userStores, store) 50 } 51 } 52 53 // WithUserStores adds the provided user stores to the server. May be specified 54 // more than once. Order is significant. Each user store is queried in the order 55 // specified, until one returns a user context or an error. 56 func WithUserStores(us ...auth.UserStore) Option { 57 return userStoreOption(us) 58 } 59 60 type configOption [1]config.Config 61 62 func (c configOption) apply(s *Server) { 63 s.config = c[0] 64 } 65 66 // WithConfig sets the server configuration. If not set, 67 // [github.com/go-kivik/kivik/v4/x/server/config.Default()] will be used. 68 func WithConfig(c config.Config) Option { 69 return configOption{c} 70 } 71