-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.go
73 lines (60 loc) · 1.71 KB
/
app.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
61
62
63
64
65
66
67
68
69
70
71
72
73
package app
import (
"log"
"net/http"
"os"
"github.com/generationtux/brizo/app/routes"
"github.com/generationtux/brizo/database"
"github.com/generationtux/brizo/database/migrations"
)
// Server is a function that starts an HTTP listener and handles requests using the provided handler
type Server func(string, http.Handler) error
// ChecksHealth interface for health checking components
type ChecksHealth func() error
// RunsMigrations interface for running migrations
type RunsMigrations func() error
// Application top level properties and methods for running Brizo
type Application struct {
serverListener Server
serverHandler http.Handler
healthChecks []ChecksHealth
migrator RunsMigrations
shouldMigrate []interface{}
}
// New creates a new application instance
func New() *Application {
brizo := new(Application)
brizo.serverListener = http.ListenAndServe
brizo.serverHandler = routes.BuildRouter()
brizo.healthChecks = []ChecksHealth{
database.Health,
}
brizo.migrator = migrations.Run
return brizo
}
// Server starts the HTTP server for the app
func (app *Application) Server() error {
address := getAddress()
log.Println("Starting server on " + address)
return app.serverListener(getAddress(), app.serverHandler)
}
// Initialize makes sure the app is ready to run
func (app *Application) Initialize() error {
var err error
for _, check := range app.healthChecks {
if err = check(); err != nil {
return err
}
}
log.Println("Running database migrations...")
return app.migrator()
}
// getAddress gets the port the app should listen on
// @todo refactor to cli flag
func getAddress() string {
port := os.Getenv("BRIZO_PORT")
if port == "" {
port = "8080"
}
return ":" + port
}