-
Notifications
You must be signed in to change notification settings - Fork 10
/
run.go
96 lines (90 loc) · 2.52 KB
/
run.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// apcore is a server framework for implementing an ActivityPub application.
// Copyright (C) 2020 Cory Slep
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package apcore
import (
"flag"
"fmt"
"io"
"os"
"github.com/go-fed/apcore/app"
"github.com/go-fed/apcore/util"
)
// Run will launch the apcore server.
func Run(a app.Application) {
if !flag.Parsed() {
flag.Parse()
}
if flag.NArg() != 1 {
flag.Usage()
os.Exit(1)
}
// Check and prepare debug mode
if *devFlag {
util.InfoLogger.Info("Debug mode enabled")
if len(*infoLogFileFlag) > 0 {
util.InfoLogger.Warning("info_log_file flag ignored in debug mode")
}
if len(*errorLogFileFlag) > 0 {
util.InfoLogger.Warning("error_log_file flag ignored in debug mode")
}
} else {
// Prepare production logging
var il, el io.Writer = os.Stdout, os.Stderr
var err error
if len(*infoLogFileFlag) > 0 {
il, err = os.OpenFile(
*infoLogFileFlag,
os.O_CREATE|os.O_WRONLY|os.O_APPEND,
0660)
if err != nil {
util.ErrorLogger.Errorf("cannot open %s: %s", *infoLogFileFlag, err)
os.Exit(1)
}
}
if len(*errorLogFileFlag) > 0 {
el, err = os.OpenFile(
*errorLogFileFlag,
os.O_CREATE|os.O_WRONLY|os.O_APPEND,
0660)
if err != nil {
util.ErrorLogger.Errorf("cannot open %s: %s", *infoLogFileFlag, err)
os.Exit(1)
}
}
util.LogInfoTo(*systemLogFlag, il)
defer util.LogInfoToStdout()
util.LogErrorTo(*systemLogFlag, el)
defer util.LogErrorToStderr()
}
// Conduct the action
var action cmdAction
found := false
for _, v := range allActions {
if v.Name == flag.Arg(0) {
action = v
found = true
break
}
}
if !found {
fmt.Fprintf(os.Stderr, "Unknown action: %s\n", flag.Arg(0))
fmt.Fprintf(os.Stderr, "Available actions:\n%s", allActionsUsage())
os.Exit(1)
} else if err := action.Action(a); err != nil {
util.ErrorLogger.Errorf("error running %s: %s", flag.Arg(0), err)
os.Exit(1)
}
}