Created
October 10, 2019 06:24
-
-
Save iyusa/1a58e7650e221b6784f75c9d84cecdf5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"database/sql" | |
"fmt" | |
"log" | |
"net/http" | |
"./client" | |
"./common" | |
"./model" | |
"github.com/gin-gonic/gin" | |
// swaggerFiles "github.com/swaggo/files" | |
// ginSwagger "github.com/swaggo/gin-swagger" | |
_ "./docs" | |
) | |
// @title BNI Virtual Account (Debit) | |
// @version 1.0 | |
// @description This is a gateway server to BNI VA | |
// @contact.name Iyus A | |
// @contact.url http://ussi.co.id | |
// @contact.email iyusa@yahoo.com | |
// @host petstore.swagger.io | |
// @BasePath /api/debet | |
func main() { | |
common.Ensure(common.Configs.Load("bniva.yaml")) | |
common.Ensure(model.Initialize()) | |
router := gin.Default() | |
api := router.Group("/api/debet") | |
{ | |
api.GET("/invoice", getInvoice) | |
api.POST("/invoice", createInvoice) | |
api.PUT("/invoice", updateInvoice) | |
api.POST("/withdrawal", withdrawal) | |
} | |
router.GET("/", getHome) | |
router.GET("/api/debet", getHome) | |
// swagger thing | |
// url := ginSwagger.URL("http://localhost:2019/api/swagger/doc.json") // The url pointing to API definition | |
// router.GET("/api/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler, url)) | |
fmt.Printf("Starting BNI Gateway version %s at port %d\n", common.Version, common.Configs.General.Port) | |
router.Run(common.Configs.GetPort()) | |
} | |
func getHome(c *gin.Context) { | |
c.String(200, "BNI VA Debet api version %s is running ... ", common.Version) | |
} | |
func getInvoice(c *gin.Context) { | |
// get json request | |
var req client.InquiryInvoiceRequest | |
err := c.ShouldBind(&req) | |
if err != nil { | |
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) | |
log.Println(err.Error()) | |
return | |
} | |
// execute to client | |
resp, err := client.InquiryInvoice(&req) | |
// handle error response | |
if err != nil { | |
c.JSON(http.StatusBadGateway, gin.H{"error": err.Error()}) | |
log.Println(err.Error()) | |
return | |
} | |
// return success response | |
c.JSON(http.StatusOK, resp) | |
} | |
func createInvoice(c *gin.Context) { | |
// get json request | |
var req client.InvoiceRequest | |
err := c.ShouldBind(&req) | |
if err != nil { | |
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) | |
log.Println(err.Error()) | |
return | |
} | |
// execute to client | |
resp, err := client.CreateInvoice(&req) | |
// handle error response | |
if err != nil { | |
c.JSON(http.StatusBadGateway, gin.H{"error": err.Error()}) | |
log.Println(err.Error()) | |
return | |
} | |
// return success response | |
c.JSON(http.StatusOK, resp) | |
} | |
func updateInvoice(c *gin.Context) { | |
// get json request | |
var req client.UpdateInvoiceRequest | |
err := c.ShouldBind(&req) | |
if err != nil { | |
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) | |
log.Println(err.Error()) | |
return | |
} | |
// execute to client | |
resp, err := client.UpdateInvoice(&req) | |
// handle error response | |
if err != nil { | |
c.JSON(http.StatusBadGateway, gin.H{"error": err.Error()}) | |
log.Println(err.Error()) | |
return | |
} | |
// return success response | |
c.JSON(http.StatusOK, resp) | |
} | |
// helper | |
// catch error handler | |
func catch(c *gin.Context) { | |
r := recover() | |
if r != nil { | |
err := r.(error) | |
c.JSON(http.StatusBadGateway, gin.H{"error": err.Error()}) | |
log.Println(err.Error()) | |
} | |
} | |
func validate(err error) { | |
if err != nil { | |
panic(err) | |
} | |
} | |
func logIfError(err error, msg string) { | |
log.Printf("%s: %v\n", msg, err) | |
} | |
func ns(value string) sql.NullString { | |
if value == "" { | |
return sql.NullString{String: "", Valid: false} | |
} | |
return sql.NullString{String: value, Valid: true} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment