Core

/app/cmd/server.go (1.5 KB)

 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
package cmd

import (
"context"
"fmt"
"runtime"

"github.com/muesli/coral"
"github.com/pkg/errors"
"github.com/valyala/fasthttp"

"{{{ .Package }}}/app"
"{{{ .Package }}}/app/controller"
"{{{ .Package }}}/app/controller/routes"
"{{{ .Package }}}/app/util"
)

const keyServer = "server"

func serverCmd() *coral.Command {
short := fmt.Sprintf("Starts the http server on port %d (by default)", util.AppPort)
f := func(*coral.Command, []string) error { return startServer(_flags) }
ret := &coral.Command{Use: keyServer, Short: short, RunE: f}
return ret
}

func startServer(flags *Flags) error {
if err := initIfNeeded(); err != nil {
return errors.Wrap(err, "error initializing application")
}

st, r, logger, err := loadServer(flags, _logger)
if err != nil {
return err
}

_, err = listenandserve(util.AppName, flags.Address, flags.Port, r)
if err != nil {
return err
}

err = st.Close(context.Background(), logger)
if err != nil {
return errors.Wrap(err, "unable to close application")
}

return nil
}

func loadServer(flags *Flags, logger util.Logger) (*app.State, fasthttp.RequestHandler, util.Logger, error) {
st, err := buildDefaultAppState(flags, logger)
if err != nil {
return nil, nil, logger, err
}
logger.Infof("started %s v%s using address [%s:%d] on %s:%s", util.AppName, _buildInfo.Version, flags.Address, flags.Port, runtime.GOOS, runtime.GOARCH)

controller.SetAppState(st, logger)
r := routes.AppRoutes(st, logger)

return st, r, logger, nil
}