-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
51 lines (42 loc) · 1020 Bytes
/
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
package main
import (
"fmt"
"log"
"os"
"zhare/server"
"github.com/alecthomas/kong"
)
var cli struct {
Directory string `name:"dir" short:"d" type:"existingdir" help:"directory to serve"`
Port int `name:"port" short:"p" default:"4000" help:"server port"`
Log bool `name:"log" default:"true" negatable:"" help:"disable logging"`
Files []string `arg:"" optional:"" name:"file" type:"existingfile" help:"files to serve"`
}
func main() {
kong.Parse(&cli)
if cli.Directory != "" {
cli.Files = getFilesFromDir(cli.Directory)
}
srv := server.NewServer(&server.ServerOpts{
Addr: fmt.Sprintf(":%d", cli.Port),
Files: cli.Files,
EnableLogger: cli.Log,
})
err := srv.Start()
if err != nil {
log.Fatal(err)
}
}
func getFilesFromDir(dir string) []string {
fileNames := make([]string, 0)
files, err := os.ReadDir(dir)
if err != nil {
panic(err)
}
for _, f := range files {
if !f.IsDir() {
fileNames = append(fileNames, f.Name())
}
}
return fileNames
}