-
Notifications
You must be signed in to change notification settings - Fork 0
/
srv.go
159 lines (138 loc) · 4.17 KB
/
srv.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// srv.go is the main entry-point into the server application.
package main
import (
"fmt"
"os"
"path/filepath"
"github.com/gin-gonic/gin"
"github.com/urfave/cli"
"github.com/tfwio/srv/fsindex/config"
"github.com/tfwio/srv/util"
)
// Configuration variables
var (
configuration config.Configuration
mCli cli.App
xCounter int32
fCounter int32
xpCounter *int32
)
func main() {
initializeCli()
}
func misterActionFunction(*cli.Context) error {
initialize(true)
return nil
}
func initializeCli() {
// mCli := cli.NewApp()
mCli.Name = filepath.Base(os.Args[0])
mCli.Authors = []cli.Author{{Name: "tfw; et alia" /*, Email: "tfwroble@gmail.com"}, cli.Author{Name: "Et al."*/}}
mCli.Version = "v0.0.0a"
mCli.Copyright = "github.com/tfwio/srv\n\n This is free, open-source software.\n disclaimer: use at own risk."
mCli.Action = func(*cli.Context) error {
initialize(true)
return nil
}
mCli.Commands = []cli.Command{
{
Name: "run",
Action: func(*cli.Context) error {
initialize(true)
return nil
},
Usage: "Runs the server.",
Description: "Default operation.",
Aliases: []string{"go"},
Flags: []cli.Flag{},
},
{
Name: "make-conf",
Description: "Generate configuration file: <[file-path].json>.",
Usage: fmt.Sprintf("%s make-conf <[file-path].json>", filepath.Base(os.Args[0])),
Flags: []cli.Flag{},
Action: func(clictx *cli.Context) error {
if clictx.NArg() == 0 {
fmt.Println("- supply a file-name to generate.\nI.E. \"conf.json\"")
os.Exit(0)
return nil
}
fmt.Printf("- found %s\n", util.Abs(clictx.Args().First()))
thearg := clictx.Args().First()
input := util.Abs(clictx.Args().First())
if util.FileExists(input) {
fmt.Printf("- please delete the file (%s) before calling this command\n", thearg)
os.Exit(0)
return nil
}
configuration.InitializeDefaults(defaultConfPath, defaultConfTarget)
configuration.ToJSON(input)
return nil
},
}}
mCli.Flags = []cli.Flag{
&cli.BoolFlag{
Name: "tls",
Destination: &config.UseTLS,
Usage: "Sets TLS on. This only works if/when tls is set in conf.json to false, and if you have valid tls cert/key files wired into the configuration.",
}, &cli.StringFlag{
Name: "host",
Destination: &config.UseHost,
Usage: "UseHost is identifies the host to use to over-ride JSON config.",
}, &cli.UintFlag{
Name: "port",
Destination: &config.UsePORT,
Value: 5500,
Usage: "UseHost is identifies the host to use to over-ride JSON config.",
}, &cli.StringFlag{
Name: "conf",
Usage: "Points to a custom configuration file.",
Value: config.DefaultConfigFile,
Destination: &config.DefaultConfigFile,
}}
mCli.Run(os.Args)
}
// initializeJSONConf loads JSON configuration and
// sets our data source. No file indexing.
//
// From here we can execute database operations.
func initializeJSONConf() {
configuration.InitializeDefaults(defaultConfPath, defaultConfTarget)
configuration.FromJSON(config.DefaultConfigFile) // loads (or creates conf.json and terminates application)
configuration.TLS = configuration.DoTLS()
if config.UseHost != "" {
configuration.Host = config.UseHost
}
if config.UsePORT != defaultPort {
configuration.Port = fmt.Sprintf(":%d", config.UsePORT)
}
}
// initialize can be called with or without starting the server.
// First, this function loads JSON conf followed by
// building up all the file-indexes and finally running
// the gin-server.
func initialize(andServe bool) {
initializeJSONConf()
gin.SetMode(gin.ReleaseMode)
router := gin.Default()
configuration.GinConfigure(andServe, router)
if !andServe {
return
}
if configuration.TLS {
println("- TLS on")
if err := router.RunTLS(configuration.Port, configuration.Crt, configuration.Key); err != nil {
panic(fmt.Sprintf("router error: %s\n", err))
}
} else {
println("- TLS off")
if err := router.Run(configuration.Port); err != nil {
panic(fmt.Sprintf("router error: %s\n", err))
}
}
}
const (
defaultPort uint = 5500
defaultConfPath = "multi-media\\public"
defaultConfTarget = "v"
)