github.com/pavlo67/common@v0.5.3/common/auth/auth_server_http/starter.go (about) 1 package auth_server_http 2 3 import ( 4 "fmt" 5 6 "github.com/pavlo67/common/common/auth/auth_jwt" 7 8 "github.com/pavlo67/common/common/server_http" 9 "github.com/pkg/errors" 10 11 "github.com/pavlo67/common/common" 12 "github.com/pavlo67/common/common/auth" 13 "github.com/pavlo67/common/common/config" 14 "github.com/pavlo67/common/common/joiner" 15 "github.com/pavlo67/common/common/logger" 16 "github.com/pavlo67/common/common/starter" 17 ) 18 19 const InterfaceKey joiner.InterfaceKey = "auth_server_http" 20 21 func Starter() starter.Operator { 22 return &authServerHTTPStarter{} 23 } 24 25 var _ starter.Operator = &authServerHTTPStarter{} 26 27 type authServerHTTPStarter struct { 28 authKey joiner.InterfaceKey 29 authJWTKey joiner.InterfaceKey 30 31 interfaceKey joiner.InterfaceKey 32 } 33 34 // ------------------------------------------------------------------------------------------------ 35 36 var l logger.Operator 37 var authOp, authJWTOp auth.Operator 38 39 func (ashs *authServerHTTPStarter) Name() string { 40 return logger.GetCallInfo().PackageName 41 } 42 43 const onRun = "on authServerHTTPStarter.Run()" 44 45 func (ashs *authServerHTTPStarter) Run(_ *config.Envs, options common.Map, joinerOp joiner.Operator, l_ logger.Operator) error { 46 47 l = l_ 48 49 ashs.authKey = joiner.InterfaceKey(options.StringDefault("auth_key", string(auth.InterfaceKey))) 50 ashs.authJWTKey = joiner.InterfaceKey(options.StringDefault("auth_jwt_key", string(auth_jwt.InterfaceKey))) 51 ashs.interfaceKey = joiner.InterfaceKey(options.StringDefault("interface_key", string(InterfaceKey))) 52 53 // middleware ------------------------------------------------------- 54 55 authJWTOp, _ = joinerOp.Interface(ashs.authJWTKey).(auth.Operator) 56 if authJWTOp == nil { 57 return fmt.Errorf(onRun+": no auth.Operator with key %s", ashs.authJWTKey) 58 } 59 60 middleware, err := OnRequestMiddleware(authJWTOp) 61 if err != nil || middleware == nil { 62 return fmt.Errorf(onRun+": can't create server_http.OnRequestMiddleware(authJWTOp), got %#v, %s", middleware, err) 63 } 64 65 srvOp, _ := joinerOp.Interface(server_http.InterfaceKey).(server_http.Operator) 66 if srvOp == nil { 67 return fmt.Errorf(onRun+": no server_http.Operator with key %s", server_http.InterfaceKey) 68 } 69 70 if err = srvOp.HandleMiddleware(middleware); err != nil { 71 return errors.Wrap(err, onRun) 72 } 73 74 // endpoints -------------------------------------------------------- 75 76 if authOp, _ = joinerOp.Interface(ashs.authKey).(auth.Operator); authOp == nil { 77 return fmt.Errorf(onRun+": no auth.Operator with key %s", ashs.authKey) 78 } 79 80 return Endpoints.Join(joinerOp) 81 }