-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.go
69 lines (60 loc) · 1.52 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
// Package lib contains all project logic.
package lib
import (
"fmt"
"gopkg.in/alecthomas/kingpin.v2"
)
const (
appName = "Fly"
desc = "Lightweight server with markdown support"
portMsg = "Port number is required 8080 is default port"
version = "Fly Server version v0.0.1"
)
var hash string
var log *Logger
// App models current application
type App struct {
app *kingpin.Application
Log *Logger
Config *Config
*Server
}
// NewApp provides a new App struct with its initializated fields
func NewApp() (application *App) {
log = GetLogger()
config, _ := NewConfig()
application = &App{
app: kingpin.New(appName, desc),
Config: config,
Log: log,
Server: &Server{
Index: config.StringDefault("index", "index.md"),
Debug: config.BooleanDefault("debug", true),
Path: config.StringDefault("path", ""),
},
}
//log = application.Log
log.Infof("Reading %v - Is debug: %v", application.Index, application.Debug)
kingpin.Version(application.Version())
application.required()
return
}
// Version function returns the application version
func (a *App) Version() (v string) {
v = fmt.Sprintf("%v %s", version, hash)
a.app.Version(v)
return
}
// Parse function process the arguments
func (a *App) Parse(args []string) {
kingpin.MustParse(a.app.Parse(args))
}
func (a *App) required() {
a.app.Flag("port", portMsg).Short('P').Default("8080").StringVar(&a.Port)
}
// Run is invoked to start the server application logic.
func (a *App) Run() {
if err := a.Server.Run(); err != nil {
panic(err)
}
}