-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
86 lines (72 loc) · 2.13 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
package main
import (
"context"
"flag"
"fmt"
"log"
"os"
"os/signal"
"time"
"github.com/urvil38/fileserver/version"
)
func getEnv(env string) string {
val := os.Getenv(env)
return val
}
const (
defaultAddr = "0.0.0.0"
defaultTimeout = 10 * time.Second
)
func main() {
homePath := getEnv("HOME")
host := flag.String("host", defaultAddr, "IP address of fileserver on which it listen on")
port := flag.String("port", "8080", "port on which fileserver runs on")
rootDir := flag.String("path", homePath, "path to the directory you want to share using fileserver")
certFile := flag.String("cert", "", "path to the public cert file")
keyFile := flag.String("key", "", "path to the private key file")
timeout := flag.Duration("timeout", defaultTimeout, "read/write timeout")
hideDotFiles := flag.Bool("no-dotfiles", false, "weather to show file starting with dot i.e. hidden files")
quiet := flag.Bool("quiet", false, "Suppress log messages from output")
logIP := flag.Bool("log-ip", false, "Log ip address of incoming request")
gzip := flag.Bool("gzip", false, "enable gzip")
username := flag.String("username", "", "Username for basic authentication")
password := flag.String("password", "", "Password for basic authentication")
v := flag.Bool("v", false, "display version of fileserver")
flag.Parse()
if *v {
fmt.Println("Version: " + version.VERSION)
fmt.Println("Git Commit: " + version.GITCOMMIT)
os.Exit(0)
}
if *rootDir == "." {
cwd, err := os.Getwd()
if err != nil {
log.Fatalln(err)
}
*rootDir = cwd
}
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
fs := NewFileServer(Config{
rootDir: *rootDir,
host: *host,
port: *port,
certFile: *certFile,
keyFile: *keyFile,
gzipEnable: *gzip,
timeout: *timeout,
hideDotFiles: *hideDotFiles,
quiet: *quiet,
logIP: *logIP,
username: *username,
password: *password,
})
go fs.Start()
<-c
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
log.Println("Recevied SIGINT signal")
log.Println("shutting down server")
fs.Stop(ctx)
os.Exit(0)
}