This commit is contained in:
crusader 2018-04-10 23:30:45 +09:00
parent a658ab86c5
commit 882a1dddf3
2 changed files with 25 additions and 26 deletions

View File

@ -3,8 +3,6 @@ package server
import ( import (
cdr "git.loafle.net/commons/di-go/registry" cdr "git.loafle.net/commons/di-go/registry"
"git.loafle.net/commons/logging-go" "git.loafle.net/commons/logging-go"
crpj "git.loafle.net/commons/rpc-go/protocol/json"
crr "git.loafle.net/commons/rpc-go/registry"
cswf "git.loafle.net/commons/server-go/web/fasthttp" cswf "git.loafle.net/commons/server-go/web/fasthttp"
oca "git.loafle.net/overflow/commons-go/annotation" oca "git.loafle.net/overflow/commons-go/annotation"
"git.loafle.net/overflow/member_gateway_rest/config" "git.loafle.net/overflow/member_gateway_rest/config"
@ -12,23 +10,16 @@ import (
) )
func NewServer(_config *config.Config) *cswf.Server { func NewServer(_config *config.Config) *cswf.Server {
serverCodec := crpj.NewServerCodec()
rpcRegistry := crr.NewRPCRegistry()
services, err := cdr.GetInstancesByAnnotationType(oca.ServiceAnnotationType) services, err := cdr.GetInstancesByAnnotationType(oca.RESTServiceAnnotationType)
if nil != err { if nil != err {
logging.Logger().Panic(err) logging.Logger().Panic(err)
} }
for _, _service := range services {
if err := rpcRegistry.RegisterService(_service, ""); nil != err {
logging.Logger().Panic(err)
}
}
webappS := &servlet.WebappServlets{} webappS := &servlet.WebappServlets{}
webappS.ServerCodec = serverCodec if err := webappS.RegisterRESTServices(services); nil != err {
webappS.RPCInvoker = rpcRegistry logging.Logger().Panic(err)
}
sh := &ServerHandlers{ sh := &ServerHandlers{
ServerHandlers: *_config.ServerHandler, ServerHandlers: *_config.ServerHandler,

View File

@ -52,15 +52,19 @@ func init() {
} }
type MemberService struct { type MemberService struct {
cda.TypeAnnotation `annotation:"@overflow:Service()"` cda.TypeAnnotation `annotation:"@overflow:RESTService()"`
_Signin cda.MethodAnnotation `annotation:"@overflow:RequestMapping(method='POST', entry='/account/signin', params='[signinID, signinPW]')"`
_SigninByCookie cda.MethodAnnotation `annotation:"@overflow:RequestMapping(method='POST', entry='/account/signin_cookie', params='[authToken]')"`
_Signup cda.MethodAnnotation `annotation:"@overflow:RequestMapping(method='POST', entry='/account/signup', params='[member]')"`
} }
func (ms *MemberService) Signin(servletCtx server.ServletCtx, ctx *fasthttp.RequestCtx, signinID string, signinPW string) (string, error) { func (ms *MemberService) Signin(servletCtx server.ServletCtx, ctx *fasthttp.RequestCtx, signinID string, signinPW string) error {
gRPCCtx := context.Background() gRPCCtx := context.Background()
r, err := grpc.Exec(gRPCCtx, "MemberService.signin", signinID, signinPW) r, err := grpc.Exec(gRPCCtx, "MemberService.signin", signinID, signinPW)
if nil != err { if nil != err {
return "", err return err
} }
token := jwt.New(jwt.SigningMethodRS512) token := jwt.New(jwt.SigningMethodRS512)
@ -80,13 +84,13 @@ func (ms *MemberService) Signin(servletCtx server.ServletCtx, ctx *fasthttp.Requ
/* Sign the token with our secret */ /* Sign the token with our secret */
tokenString, err := token.SignedString(signKey) tokenString, err := token.SignedString(signKey)
if nil != err { if nil != err {
return "", err return err
} }
var domainMember interface{} var domainMember interface{}
err = json.Unmarshal([]byte(r), &domainMember) err = json.Unmarshal([]byte(r), &domainMember)
if nil != err { if nil != err {
return "", err return err
} }
signInResult := &SignInResult{ signInResult := &SignInResult{
@ -96,13 +100,15 @@ func (ms *MemberService) Signin(servletCtx server.ServletCtx, ctx *fasthttp.Requ
buf, err := json.Marshal(signInResult) buf, err := json.Marshal(signInResult)
if nil != err { if nil != err {
return "", err return err
} }
return string(buf), nil ctx.SetBody(buf)
return nil
} }
func (ms *MemberService) SigninByCookie(servletCtx server.ServletCtx, ctx *fasthttp.RequestCtx, authToken string) (string, error) { func (ms *MemberService) SigninByCookie(servletCtx server.ServletCtx, ctx *fasthttp.RequestCtx, authToken string) error {
token, err := jwt.Parse(authToken, func(token *jwt.Token) (interface{}, error) { token, err := jwt.Parse(authToken, func(token *jwt.Token) (interface{}, error) {
// Don't forget to validate the alg is what you expect: // Don't forget to validate the alg is what you expect:
@ -114,14 +120,14 @@ func (ms *MemberService) SigninByCookie(servletCtx server.ServletCtx, ctx *fasth
return verifyKey, nil return verifyKey, nil
}) })
if nil != err { if nil != err {
return "", err return err
} }
var ok bool var ok bool
var claims jwt.MapClaims var claims jwt.MapClaims
if claims, ok = token.Claims.(jwt.MapClaims); !ok || !token.Valid { if claims, ok = token.Claims.(jwt.MapClaims); !ok || !token.Valid {
logging.Logger().Warnf("Token is not valid %v", token) logging.Logger().Warnf("Token is not valid %v", token)
return "", fmt.Errorf("authToken is not valid") return fmt.Errorf("authToken is not valid")
} }
params := []string{claims["sub"].(string)} params := []string{claims["sub"].(string)}
@ -129,12 +135,14 @@ func (ms *MemberService) SigninByCookie(servletCtx server.ServletCtx, ctx *fasth
gRPCCtx := context.Background() gRPCCtx := context.Background()
r, err := grpc.Exec(gRPCCtx, "DomainMemberService.readByMemberEmail", params...) r, err := grpc.Exec(gRPCCtx, "DomainMemberService.readByMemberEmail", params...)
return r, err ctx.SetBody([]byte(r))
return err
} }
func (ms *MemberService) Register(servletCtx server.ServletCtx, ctx *fasthttp.RequestCtx, id string, pw string) (string, error) { func (ms *MemberService) Signup(servletCtx server.ServletCtx, ctx *fasthttp.RequestCtx, id string, pw string) error {
return "dkfksddfk", nil return nil
} }
type SignInResult struct { type SignInResult struct {