-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
95 lines (78 loc) · 1.56 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
package main
import (
"fmt"
"log"
"net/http"
"os"
"github.com/spf13/cobra"
_ "net/http/pprof"
)
var Cmd = &cobra.Command{
Use: "tenta",
Long: "Fast and easy local LAN proxy cache",
RunE: run,
}
var args struct {
debug bool
dataDir string
maxCacheAge int
cronSchedule string
httpPort int
}
func init() {
flags := Cmd.Flags()
flags.StringVar(
&args.dataDir,
"data-dir",
"data/",
"Directory to use for caching files",
)
flags.IntVar(
&args.maxCacheAge,
"max-cache-age",
0,
"Max age (in hours) of files. Value of 0 means no files will be deleted (default 0)",
)
flags.IntVar(
&args.httpPort,
"http-port",
8080,
"Port to use for the HTTP server",
)
flags.BoolVar(
&args.debug,
"debug",
false,
"Enable debug logging",
)
flags.StringVar(
&args.cronSchedule,
"cron-schedule",
"* */1 * * *",
"Cron schedule to use for cleaning up cache files",
)
Cmd.RegisterFlagCompletionFunc("output-format", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return []string{"json", "prom"}, cobra.ShellCompDirectiveDefault
})
}
func main() {
log.SetFlags(log.Flags() | log.Lshortfile)
if err := Cmd.Execute(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
os.Exit(0)
}
func run(cmd *cobra.Command, argv []string) error {
log.Println("Starting Tenta!")
if args.debug {
go func() {
log.Println("Starting pprof server on port 6060")
log.Println(http.ListenAndServe(":6060", nil))
}()
}
StartCron()
StartMetrics()
StartHTTP()
return nil
}