-
Notifications
You must be signed in to change notification settings - Fork 0
/
application.go
60 lines (50 loc) · 1.29 KB
/
application.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package application
import (
"context"
"errors"
"fmt"
"log"
"net/http"
"time"
"github.com/kube-tarian/git-bridge/agent/api"
"github.com/kube-tarian/git-bridge/agent/pkg/clients"
"github.com/kube-tarian/git-bridge/agent/pkg/config"
"github.com/gin-gonic/gin"
)
type Application struct {
Config *config.Config
server *http.Server
conn *clients.NATSContext
}
func New(conf *config.Config, conn *clients.NATSContext) *Application {
app := &Application{
Config: conf,
conn: conn,
}
app.server = &http.Server{
Addr: fmt.Sprintf(":%d", conf.Port),
Handler: app.Routes(),
IdleTimeout: time.Minute,
ReadTimeout: 10 * time.Second,
WriteTimeout: 30 * time.Second,
}
return app
}
func (app *Application) Routes() *gin.Engine {
router := gin.New()
api.RegisterHandlers(router, app)
return router
}
func (app *Application) Start() {
log.Println("Starting server on port", app.Config.Port)
if err := app.server.ListenAndServe(); err != nil && errors.Is(err, http.ErrServerClosed) {
log.Fatalf("Server closed, readon: %v", err)
}
}
func (app *Application) Close() {
log.Printf("Closing the service gracefully")
app.conn.Close()
if err := app.server.Shutdown(context.Background()); err != nil {
log.Printf("Could not close the service gracefully: %v", err)
}
}