-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
65 lines (60 loc) · 2.02 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
// Package main is the starting point of the file server.
package main
import (
"encoding/json"
"flag"
"fmt"
. "github.com/wisbery/tarolas/server"
"net/http"
"os"
"os/signal"
"syscall"
)
var defaultConfigurationFileName = "./config/config.json"
// showUsageAndExit shows the usage of this service.
func showUsage() {
}
// readConfiguration reads server's configuration from configuration file specified in command line.
// To correctly start tarolas server, there must be given one command line parameter named '--config'.
// This parameter must specify existing JSON file with configuration parameters.
// When something goes wrong, the error message and usage is printed,
// and then the file server terminates with exit code 1.
func readConfiguration() (*Configuration, error) {
// prepare configuration flag
cfgFileName := flag.String("config", "", "path to configuration file")
flag.Parse()
// check if configuration file name is given
if *cfgFileName == "" {
cfgFileName = &defaultConfigurationFileName
}
// try to read the configuration file
data, err := os.ReadFile(*cfgFileName)
if err != nil {
fmt.Printf("error occured while reading tarolas configuration file\n%v\n\n", err)
showUsage()
return nil, err
}
// try to load JSON data read from configuration file
var cfg Configuration
err = json.Unmarshal(data, &cfg)
if err != nil {
fmt.Printf("error occured while parsing tarolas configuration file:\n%v\n\n", err)
showUsage()
return nil, err
}
// TODO check here if root directory exists and the server has read/write/delete access in this directory
// TODO add read/write/delete checks
return &cfg, nil
}
// Function main starts the server.
func main() {
var httpServer *http.Server
osSignals := make(chan os.Signal, 1)
signal.Notify(osSignals, syscall.SIGINT, syscall.SIGTERM)
if configuration, err := readConfiguration(); err == nil {
httpServer = StartServer(configuration)
sig := <-osSignals
fmt.Printf("\nTarolas received signal [%v], gracefully exiting...\n", sig)
StopServer(httpServer)
}
}