github.com/mundipagg/boleto-api@v0.0.0-20230620145841-3f9ec742599f/api/rest.go (about) 1 package api 2 3 import ( 4 "context" 5 stdlog "log" 6 "net/http" 7 "os" 8 "os/signal" 9 "syscall" 10 11 "github.com/gin-gonic/gin" 12 "github.com/mundipagg/boleto-api/config" 13 "github.com/mundipagg/boleto-api/db" 14 "github.com/mundipagg/boleto-api/log" 15 "github.com/mundipagg/boleto-api/queue" 16 ) 17 18 //InstallRestAPI "instala" e sobe o servico de rest 19 func InstallRestAPI() { 20 21 l := log.CreateLog() 22 l.Operation = "InstallAPI" 23 24 gin.SetMode(gin.ReleaseMode) 25 router := gin.New() 26 router.Use(gin.Recovery()) 27 useNewRelic(router) 28 29 if config.Get().DevMode && !config.Get().MockMode { 30 router.Use(gin.Logger()) 31 } 32 33 Base(router) 34 V1(router) 35 V2(router) 36 37 interrupt := make(chan os.Signal, 1) 38 signal.Notify(interrupt, syscall.SIGINT, syscall.SIGTERM) 39 40 server := &http.Server{ 41 Addr: config.Get().APIPort, 42 Handler: router, 43 } 44 45 go func() { 46 err := server.ListenAndServe() 47 if err != nil && err != http.ErrServerClosed { 48 interrupt <- syscall.SIGTERM 49 l.ErrorWithBasic("got an error when trying serve.ListAndServe()", "Error", err) 50 stdlog.Println("err: ", err) 51 } 52 }() 53 54 <-interrupt 55 l.InfoWithBasic("start shutdown server...", "Information", nil) 56 stdlog.Println("start shutdown server...") 57 58 // Server Shutdown 59 err := server.Shutdown(context.Background()) 60 if err != nil { 61 l.ErrorWithBasic("shutdown server with error", "Error", err) 62 } 63 64 // Close DB Connection 65 err = db.CloseConnection() 66 if err != nil { 67 l.ErrorWithBasic("error closing Mongodb connection", "Error", err) 68 } else { 69 l.InfoWithBasic("mongodb connection successfully closed", "Information", nil) 70 } 71 72 // Close RabbitMQ Connection 73 err = queue.CloseConnection() 74 if err != nil { 75 l.ErrorWithBasic("error closing rabbitmq connection", "Error", err) 76 } else { 77 l.InfoWithBasic("rabbitmq connection successfully closed", "Information", nil) 78 } 79 80 l.InfoWithBasic("shutdown completed", "Information", nil) 81 stdlog.Println("shutdown completed") 82 // time.Sleep(10 * time.Second) 83 }