From 46cfc967a9670ae7244dff261c8eedd04a8b6296 Mon Sep 17 00:00:00 2001 From: crusader Date: Tue, 6 Mar 2018 12:23:57 +0900 Subject: [PATCH] ing --- commons/error_response.go | 22 ++++++++++++++++++++++ config.json | 2 +- module/member/signin.go | 23 +++++++++-------------- 3 files changed, 32 insertions(+), 15 deletions(-) create mode 100644 commons/error_response.go diff --git a/commons/error_response.go b/commons/error_response.go new file mode 100644 index 0000000..4084c95 --- /dev/null +++ b/commons/error_response.go @@ -0,0 +1,22 @@ +package commons + +import ( + "encoding/json" + + "github.com/valyala/fasthttp" +) + +func SendRESTResponse(ctx *fasthttp.RequestCtx, response interface{}) { + ctx.SetContentType("application/javascript") + + jRes, _ := json.Marshal(response) + ctx.SetBody(jRes) +} + +func SendRESTError(ctx *fasthttp.RequestCtx, statusCode int, err error) { + ctx.SetContentType("application/javascript") + ctx.SetStatusCode(statusCode) + + jRes, _ := json.Marshal(err) + ctx.SetBody(jRes) +} diff --git a/config.json b/config.json index b117ce5..cfbf6d5 100644 --- a/config.json +++ b/config.json @@ -11,7 +11,7 @@ "signingKey": "tWB0lUXiCwX4U3qsJZcZ10mKvEH793RHkTJDbDuZVshQTk4uNB6ck59UQ96lhsRi4XNUiEnlIbP8XYQMPabeNtERX3iyHeDcwocgUVAor1nkAajYeq1gNyJszGpMhEOT" }, "gRPC": { - "addr": "192.168.1.50:50006", + "addr": "127.0.0.1:50006", "tls": false, "pool": { "maxIdle": 1, diff --git a/module/member/signin.go b/module/member/signin.go index bdb57c6..c8692bb 100644 --- a/module/member/signin.go +++ b/module/member/signin.go @@ -6,8 +6,8 @@ import ( "time" "encoding/json" - "log" + "git.loafle.net/overflow/overflow_server_app/commons" "git.loafle.net/overflow/overflow_server_app/config" "git.loafle.net/overflow/overflow_server_app/external/grpc" jwt "github.com/dgrijalva/jwt-go" @@ -16,15 +16,15 @@ import ( func SignIn(ctx *fasthttp.RequestCtx) { var err error - //signinId := string(ctx.FormValue("signinId")) - //signinPw := string(ctx.FormValue("signinPw")) + var webParams map[string]interface{} - var webParams []interface{} webBytes := ctx.PostBody() err = json.Unmarshal(webBytes, &webParams) if err != nil { - fmt.Fprintf(ctx, "Err!!!!: %s\n", err) + commons.SendRESTError(ctx, fasthttp.StatusBadRequest, err) + // fmt.Fprintf(ctx, "Err!!!!: %s\n", err) + return } length := len(webParams) @@ -33,16 +33,15 @@ func SignIn(ctx *fasthttp.RequestCtx) { fmt.Println("eeee") } - jsonMap := webParams[0].(map[string]interface{}) - signinId := jsonMap["signinId"].(string) - signinPw := jsonMap["signinPw"].(string) + signinId := webParams["signinId"].(string) + signinPw := webParams["signinPw"].(string) params := []string{signinId, signinPw} gRPCCtx := context.Background() r, err := grpc.Exec(gRPCCtx, "MemberService.signin", params) if nil != err { - fmt.Fprintf(ctx, "%v", err) + commons.SendRESTError(ctx, fasthttp.StatusBadRequest, err) return } @@ -66,9 +65,5 @@ func SignIn(ctx *fasthttp.RequestCtx) { jwtCookie.SetValue(tokenString) ctx.Response.Header.SetCookie(&jwtCookie) - //fmt.Fprintf(ctx, "Welcome!!!!: %s\n", r) - ctx.SetContentType("application/javascript") - log.Printf("M:%s", r) - ctx.SetBody([]byte(r)) - + commons.SendRESTResponse(ctx, r) }