From 549d91fbb40ed3ce63ea4afd7ac5c771aa9f3580 Mon Sep 17 00:00:00 2001 From: Lukas Bachschwell Date: Wed, 7 Nov 2018 16:27:39 +0100 Subject: [PATCH] adding error handling and other things --- bee.json | 6 ++++++ controllers/base.go | 24 +++++++++++++++-------- controllers/baseAPI.go | 43 ++++++++++++++++++------------------------ controllers/error.go | 13 +++++++++++++ controllers/index.go | 14 ++++++++++++++ lastupdate.tmp | 2 +- routers/router.go | 2 ++ 7 files changed, 70 insertions(+), 34 deletions(-) create mode 100644 controllers/error.go create mode 100644 controllers/index.go diff --git a/bee.json b/bee.json index 381d414..9c1a1da 100644 --- a/bee.json +++ b/bee.json @@ -7,6 +7,12 @@ }, "cmd_args": [], "envs": [], + "dir_structure": { + "watch_all": true, + "controllers": "controllers", + "models": "models", + "others": ["services"] + }, "database": { "driver": "postgres", "conn": "postgres://postgres:postgre@127.0.0.1:5435/system?sslmode=disable" diff --git a/controllers/base.go b/controllers/base.go index a5c5e96..ba041a5 100644 --- a/controllers/base.go +++ b/controllers/base.go @@ -9,7 +9,8 @@ type JsonBasicResponse struct { Message string } -const JSON_ERROR int = 500 +const JSON_ERROR int = 400 +const JSON_INT_ERROR int = 500 const JSON_SUCCESS int = 200 // BaseController operations for BaseController @@ -17,14 +18,21 @@ type BaseController struct { beego.Controller } -func (this *BaseController) ServeJsonError(message string) { +func (c *BaseController) ServeJsonError(message string) { json := JsonBasicResponse{JSON_ERROR, message} - this.Data["json"] = &json - this.ServeJSON() + c.Data["json"] = &json + ///c.Ctx.ResponseWriter.WriteHeader(400) + c.ServeJSON() } -func (this *BaseController) ServeJsonSuccess(message string) { - json := JsonBasicResponse{JSON_SUCCESS, message} - this.Data["json"] = &json - this.ServeJSON() +func (c *BaseController) ServeJsonErrorWithCode(errorcode int, message string) { + json := JsonBasicResponse{errorcode, message} + c.Data["json"] = &json + c.ServeJSON() +} + +func (c *BaseController) ServeJsonSuccess(message string) { + json := JsonBasicResponse{JSON_SUCCESS, message} + c.Data["json"] = &json + c.ServeJSON() } diff --git a/controllers/baseAPI.go b/controllers/baseAPI.go index fa1270d..3a9742f 100644 --- a/controllers/baseAPI.go +++ b/controllers/baseAPI.go @@ -1,36 +1,29 @@ package controllers -import ( - //"fmt" - "github.com/juusechec/jwt-beego" -) - // BaseController operations for APIs type BaseAPIController struct { BaseController } func (this *BaseAPIController) Prepare() { - if this.Ctx.Input.Method() != "POST" { - this.ServeJsonError("Method not allowed") - } - //Lo que quieras hacer en todos los controladores - // O puede ser leído de una cabecera HEADER!! - tokenString := this.Ctx.Request.Header.Get("X-JWTtoken") - et := jwtbeego.EasyToken{} - valid, issuer, _ := et.ValidateToken(tokenString) - if !valid { - this.Ctx.Output.SetStatus(401) - this.ServeJsonError("Invalid Token") - } + /* + //Lo que quieras hacer en todos los controladores + // O puede ser leído de una cabecera HEADER!! + tokenString := this.Ctx.Request.Header.Get("X-JWTtoken") + et := jwtbeego.EasyToken{} + valid, issuer, _ := et.ValidateToken(tokenString) + if !valid { + this.Ctx.Output.SetStatus(401) + this.ServeJsonError("Invalid Token") + } + /* + userSession := this.GetSession("username") - userSession := this.GetSession("username") - - if userSession == nil || userSession != issuer { - this.Ctx.Output.SetStatus(401) - this.ServeJsonError("Invalid Session") - } - - return + if userSession == nil || userSession != issuer { + this.Ctx.Output.SetStatus(401) + this.ServeJsonError("Invalid Session") + } + */ + //return } diff --git a/controllers/error.go b/controllers/error.go new file mode 100644 index 0000000..a4d4aed --- /dev/null +++ b/controllers/error.go @@ -0,0 +1,13 @@ +package controllers + +type ErrorController struct { + BaseController +} + +func (c *ErrorController) Error404() { + c.ServeJsonErrorWithCode(404, "Not Found") +} + +func (c *ErrorController) Error500() { + c.ServeJsonErrorWithCode(500, "Internal Server Error") +} diff --git a/controllers/index.go b/controllers/index.go new file mode 100644 index 0000000..9f8fa0f --- /dev/null +++ b/controllers/index.go @@ -0,0 +1,14 @@ +package controllers + +// IndexController operations for Index +type IndexController struct { + BaseController +} + +func (c *IndexController) Get() { + c.ServeJsonSuccess("multitenant API") +} + +func (c *IndexController) Post() { + c.ServeJsonSuccess("multitenant API") +} diff --git a/lastupdate.tmp b/lastupdate.tmp index b0e5f9c..68ada61 100755 --- a/lastupdate.tmp +++ b/lastupdate.tmp @@ -1 +1 @@ -{"/Users/LB/go/src/multitenantStack/controllers":1541579833440000000} \ No newline at end of file +{"/Users/LB/go/src/multitenantStack/controllers":1541598684943144901} \ No newline at end of file diff --git a/routers/router.go b/routers/router.go index bf013d8..a40159d 100644 --- a/routers/router.go +++ b/routers/router.go @@ -46,5 +46,7 @@ func init() { ), ), ) + beego.Router("/", &controllers.IndexController{}) + beego.ErrorController(&controllers.ErrorController{}) beego.AddNamespace(ns) }