adding error handling and other things
This commit is contained in:
		
							
								
								
									
										6
									
								
								bee.json
									
									
									
									
									
								
							
							
						
						
									
										6
									
								
								bee.json
									
									
									
									
									
								
							| @@ -7,6 +7,12 @@ | |||||||
|   }, |   }, | ||||||
|   "cmd_args": [], |   "cmd_args": [], | ||||||
|   "envs": [], |   "envs": [], | ||||||
|  |   "dir_structure": { | ||||||
|  |     "watch_all": true, | ||||||
|  |     "controllers": "controllers", | ||||||
|  |     "models": "models", | ||||||
|  |     "others": ["services"] | ||||||
|  |   }, | ||||||
|   "database": { |   "database": { | ||||||
|     "driver": "postgres", |     "driver": "postgres", | ||||||
|     "conn": "postgres://postgres:postgre@127.0.0.1:5435/system?sslmode=disable" |     "conn": "postgres://postgres:postgre@127.0.0.1:5435/system?sslmode=disable" | ||||||
|   | |||||||
| @@ -9,7 +9,8 @@ type JsonBasicResponse struct { | |||||||
| 	Message string | 	Message string | ||||||
| } | } | ||||||
|  |  | ||||||
| const JSON_ERROR int = 500 | const JSON_ERROR int = 400 | ||||||
|  | const JSON_INT_ERROR int = 500 | ||||||
| const JSON_SUCCESS int = 200 | const JSON_SUCCESS int = 200 | ||||||
|  |  | ||||||
| // BaseController operations for BaseController | // BaseController operations for BaseController | ||||||
| @@ -17,14 +18,21 @@ type BaseController struct { | |||||||
| 	beego.Controller | 	beego.Controller | ||||||
| } | } | ||||||
|  |  | ||||||
| func (this *BaseController) ServeJsonError(message string) { | func (c *BaseController) ServeJsonError(message string) { | ||||||
| 	json := JsonBasicResponse{JSON_ERROR, message} | 	json := JsonBasicResponse{JSON_ERROR, message} | ||||||
| 	this.Data["json"] = &json | 	c.Data["json"] = &json | ||||||
| 	this.ServeJSON() | 	///c.Ctx.ResponseWriter.WriteHeader(400) | ||||||
|  | 	c.ServeJSON() | ||||||
| } | } | ||||||
|  |  | ||||||
| func (this *BaseController) ServeJsonSuccess(message string) { | func (c *BaseController) ServeJsonErrorWithCode(errorcode int, message string) { | ||||||
| 	json := JsonBasicResponse{JSON_SUCCESS, message} | 	json := JsonBasicResponse{errorcode, message} | ||||||
| 	this.Data["json"] = &json | 	c.Data["json"] = &json | ||||||
| 	this.ServeJSON() | 	c.ServeJSON() | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func (c *BaseController) ServeJsonSuccess(message string) { | ||||||
|  | 	json := JsonBasicResponse{JSON_SUCCESS, message} | ||||||
|  | 	c.Data["json"] = &json | ||||||
|  | 	c.ServeJSON() | ||||||
| } | } | ||||||
|   | |||||||
| @@ -1,36 +1,29 @@ | |||||||
| package controllers | package controllers | ||||||
|  |  | ||||||
| import ( |  | ||||||
| 	//"fmt" |  | ||||||
| 	"github.com/juusechec/jwt-beego" |  | ||||||
| ) |  | ||||||
|  |  | ||||||
| // BaseController operations for APIs | // BaseController operations for APIs | ||||||
| type BaseAPIController struct { | type BaseAPIController struct { | ||||||
| 	BaseController | 	BaseController | ||||||
| } | } | ||||||
|  |  | ||||||
| func (this *BaseAPIController) Prepare() { | 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!! | 		//Lo que quieras hacer en todos los controladores | ||||||
| 	tokenString := this.Ctx.Request.Header.Get("X-JWTtoken") | 		// O puede ser leído de una cabecera HEADER!! | ||||||
| 	et := jwtbeego.EasyToken{} | 		tokenString := this.Ctx.Request.Header.Get("X-JWTtoken") | ||||||
| 	valid, issuer, _ := et.ValidateToken(tokenString) | 		et := jwtbeego.EasyToken{} | ||||||
| 	if !valid { | 		valid, issuer, _ := et.ValidateToken(tokenString) | ||||||
| 		this.Ctx.Output.SetStatus(401) | 		if !valid { | ||||||
| 		this.ServeJsonError("Invalid Token") | 			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) | ||||||
| 	if userSession == nil || userSession != issuer { | 				this.ServeJsonError("Invalid Session") | ||||||
| 		this.Ctx.Output.SetStatus(401) | 			} | ||||||
| 		this.ServeJsonError("Invalid Session") | 	*/ | ||||||
| 	} | 	//return | ||||||
|  |  | ||||||
| 	return |  | ||||||
| } | } | ||||||
|   | |||||||
							
								
								
									
										13
									
								
								controllers/error.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										13
									
								
								controllers/error.go
									
									
									
									
									
										Normal file
									
								
							| @@ -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") | ||||||
|  | } | ||||||
							
								
								
									
										14
									
								
								controllers/index.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								controllers/index.go
									
									
									
									
									
										Normal file
									
								
							| @@ -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") | ||||||
|  | } | ||||||
| @@ -1 +1 @@ | |||||||
| {"/Users/LB/go/src/multitenantStack/controllers":1541579833440000000} | {"/Users/LB/go/src/multitenantStack/controllers":1541598684943144901} | ||||||
| @@ -46,5 +46,7 @@ func init() { | |||||||
| 			), | 			), | ||||||
| 		), | 		), | ||||||
| 	) | 	) | ||||||
|  | 	beego.Router("/", &controllers.IndexController{}) | ||||||
|  | 	beego.ErrorController(&controllers.ErrorController{}) | ||||||
| 	beego.AddNamespace(ns) | 	beego.AddNamespace(ns) | ||||||
| } | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user