-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
120 lines (100 loc) · 3.35 KB
/
main.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package main
/*
gouploadserver [options] [path]
ex: gouploadserver -p 8081 .
options:
-p or --port Port to use (defaults to 8080)
*/
import (
"flag"
"fmt"
"os"
"runtime"
"strings"
"time"
"github.com/guilhermerodrigues680/gouploadserver/app"
"github.com/sirupsen/logrus"
)
const Version = "v0.0.2-alpha.0"
var portFlag = flag.Int("port", 8000, "Port to use")
var watchMemUsageFlag = flag.Bool("watch-mem", false, "Watch memory usage")
var devFlag = flag.Bool("dev", false, "Use development settings")
var keepOriginalUploadFileNameFlag = flag.Bool("keep-upload-filename", false, "Keep original upload file name: Use 'filename.ext' instead of 'filename<-random>.ext'")
var showVersionFlag = flag.Bool("version", false, "Show version number and quit")
var spaFlag = flag.Bool("spa", false, "Return to all files not found /index.html")
var pathArg string
func main() {
// usage: flag -h or --help
flag.Usage = func() {
fmt.Fprintln(flag.CommandLine.Output(), "")
fmt.Fprintln(flag.CommandLine.Output(), "Usage: gouploadserver [options] [path]")
fmt.Fprintln(flag.CommandLine.Output(), "[path] defaults to ./")
fmt.Fprintln(flag.CommandLine.Output(), "Options are:")
flag.VisitAll(func(f *flag.Flag) {
fmt.Fprintf(flag.CommandLine.Output(), " --%-24v %v (default %v)\n", f.Name, f.Usage, f.DefValue)
})
fmt.Fprintf(flag.CommandLine.Output(), " --%-24v %v\n", "help", "Display usage information (this message)")
fmt.Fprintf(flag.CommandLine.Output(), " -%-25v %v\n", "h", "Display usage information (this message) (shorthand)")
fmt.Fprintln(flag.CommandLine.Output(), "")
fmt.Fprintln(flag.CommandLine.Output(), "Powered By: guilhermerodrigues680")
}
// parses the command-line flags
flag.Parse()
if *showVersionFlag {
fmt.Fprintf(flag.CommandLine.Output(), "gouploadserver %s\n", Version)
fmt.Fprintln(flag.CommandLine.Output(), "\nPowered By: guilhermerodrigues680")
os.Exit(0)
}
logger := getLogger(*devFlag)
logger.Trace(strings.Join(os.Args, " "))
if *devFlag {
flag.VisitAll(func(f *flag.Flag) {
logger.Debugf("--%v (value %v) (default %v)", f.Name, f.Value, f.DefValue)
})
}
if *watchMemUsageFlag {
go func() {
for {
time.Sleep(time.Second)
PrintMemUsage(logger.WithField("log", "memstats"))
}
}()
}
wd := flag.Arg(0)
if wd == "" {
cwd, err := os.Getwd()
if err != nil {
logger.Fatal(err)
}
wd = cwd
}
err := app.Run(wd, *portFlag, *keepOriginalUploadFileNameFlag, *spaFlag, logger.WithField("app", "run"))
if err != nil {
logger.Fatal(err)
}
}
func getLogger(development bool) *logrus.Logger {
logger := logrus.New()
logger.SetFormatter(&logrus.TextFormatter{
ForceColors: true,
FullTimestamp: true,
DisableLevelTruncation: development,
})
if development {
logger.SetLevel(logrus.TraceLevel) // log all
} else {
logger.SetLevel(logrus.InfoLevel) // log only info and above
}
logger.SetOutput(os.Stdout) // Output to stdout instead of the default stderr
return logger
}
func PrintMemUsage(logger *logrus.Entry) {
// For info, see: https://golang.org/pkg/runtime/#MemStats
bToMb := func(b uint64) uint64 {
return b / 1024 / 1024
}
var m runtime.MemStats
runtime.ReadMemStats(&m)
logger.Infof("Alloc = %v MiB\tHeapAlloc = %v MiB\tTotalAlloc = %v MiB\tSys = %v MiB\tNumGC = %v",
bToMb(m.Alloc), bToMb(m.HeapAlloc), bToMb(m.TotalAlloc), bToMb(m.Sys), m.NumGC)
}