-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
99 lines (85 loc) · 2.14 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
87
88
89
90
91
92
93
94
95
96
97
98
99
package main
import (
"fmt"
"github.com/rs/xhandler"
log "github.com/sirupsen/logrus"
"net/http"
"os"
"runtime"
"strconv"
)
const (
serviceID = "CLAWIO_OCWEBDAV"
authServerEnvar = serviceID + "_AUTH"
dataServerEnvar = serviceID + "_DATA"
metaServerEnvar = serviceID + "_META"
portEnvar = serviceID + "_PORT"
tmpDirEnvar = serviceID + "_TMPDIR"
logLevelEnvar = serviceID + "_LOGLEVEL"
sharedSecretEnvar = "CLAWIO_SHAREDSECRET"
endPoint = "/"
)
type environ struct {
authServer string
dataServer string
metaServer string
port int
tmpDir string
logLevel string
sharedSecret string
}
func getEnviron() (*environ, error) {
e := &environ{}
port, err := strconv.Atoi(os.Getenv(portEnvar))
if err != nil {
return nil, err
}
e.port = port
e.sharedSecret = os.Getenv(sharedSecretEnvar)
e.authServer = os.Getenv(authServerEnvar)
e.dataServer = os.Getenv(dataServerEnvar)
e.metaServer = os.Getenv(metaServerEnvar)
e.tmpDir = os.Getenv(tmpDirEnvar)
e.logLevel = os.Getenv(logLevelEnvar)
return e, nil
}
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
env, err := getEnviron()
if err != nil {
log.Error(err)
os.Exit(1)
}
l, err := log.ParseLevel(env.logLevel)
if err != nil {
l = log.ErrorLevel
}
log.SetLevel(l)
log.Infof("Service %s started", serviceID)
log.Infof("%s=%s", tmpDirEnvar, env.tmpDir)
log.Infof("%s=%s", authServerEnvar, env.authServer)
log.Infof("%s=%s", dataServerEnvar, env.dataServer)
log.Infof("%s=%s", metaServerEnvar, env.metaServer)
log.Infof("%s=%d\n", portEnvar, env.port)
log.Infof("%s=%s\n", sharedSecretEnvar, "******")
p := &newServerParams{}
p.authServer = env.authServer
p.dataServer = env.dataServer
p.metaServer = env.metaServer
p.sharedSecret = env.sharedSecret
p.tmpDir = env.tmpDir
// Create chunk tmp dir
if err := os.MkdirAll(p.tmpDir, 0644); err != nil {
log.Error(err)
os.Exit(1)
}
srv, err := newServer(p)
if err != nil {
log.Error(err)
os.Exit(1)
}
c := xhandler.Chain{}
c.UseC(xhandler.CloseHandler)
http.Handle(endPoint, c.Handler(srv))
log.Error(http.ListenAndServe(fmt.Sprintf(":%d", env.port), nil))
}