This repository has been archived by the owner on Feb 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
serv.go
81 lines (65 loc) · 1.54 KB
/
serv.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
package cmd
import (
"context"
"crypto/tls"
"errors"
"net/http"
"os"
"os/signal"
"syscall"
"github.com/valord577/clix"
log "github.com/valord577/webdav/logger"
"github.com/valord577/webdav/rt"
"github.com/valord577/webdav/serve"
)
// @author valor.
var servCmd = &clix.Command{
Name: "serv",
Summary: "Startup webdav server.",
Run: runServ,
}
var cfg string
func init() {
servCmd.FlagStringVar(&cfg, "c", "", "Declare the configuration file path")
}
func runServ(_ *clix.Command, _ []string) error {
if cfg == "" {
msg := "no configuration file declared"
log.Errorf(msg)
return errors.New(msg)
}
err := rt.ReadInFile(cfg)
if err != nil {
msg := "error to read cfg file: " + err.Error()
log.Errorf(msg)
return errors.New(msg)
}
log.Infof("activated cfg file: %s", cfg)
serv := serve.WebDAVServ()
go func() {
var err error
if rt.UseTLS() {
serv.TLSConfig = &tls.Config{
MinVersion: rt.MinVersionTLS(),
}
log.Infof("activated HTTP over TLS:\nkey -> %s\ncer -> %s", rt.KeyFilepath(), rt.CerFilepath())
err = serv.ListenAndServeTLS(rt.CerFilepath(), rt.KeyFilepath())
} else {
err = serv.ListenAndServe()
}
if err != nil && err != http.ErrServerClosed {
panic(err)
}
}()
// Block and listen for signals
sig := make(chan os.Signal, 1)
signal.Notify(sig, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGTERM)
s := <-sig
log.Infof("starting to shutdown by signal: %d", s)
err = serv.Shutdown(context.Background())
if err != nil {
panic(err)
}
log.Infof("shutdown successfully")
return err
}