forked from dreamans/syncd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsyncd.go
142 lines (121 loc) · 3.39 KB
/
syncd.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// Copyright 2018 syncd Author. All Rights Reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package syncd
import (
"time"
"io"
"os"
"errors"
"encoding/base64"
"github.com/tinystack/goutil/gopath"
"github.com/tinystack/goweb"
"github.com/tinystack/golog"
"github.com/jinzhu/gorm"
)
type Syncd struct {
config *Config
serve *goweb.Serve
}
type ServeHandler struct {
BeforeHandler goweb.HandlerFunc
AfterHandler goweb.HandlerFunc
ServerErrorHandler func(error, *goweb.Context, int)
NotFoundHandler goweb.HandlerFunc
MethodNotAllowHandler goweb.HandlerFunc
}
var (
Logger *golog.Logger
Orm *gorm.DB
DbInstance *DB
Mail *SendMail
DataDir string
TmpDir string
RemoteTmpDir string
CipherKey []byte
Version string
)
const (
VERSION = "1.1.2"
)
func NewSyncd(cfg *Config) *Syncd {
syncd := &Syncd{
config: cfg,
serve: goweb.New(cfg.Serve.Addr),
}
syncd.serve.ReadTimeout = time.Second * time.Duration(cfg.Serve.ReadTimeout)
syncd.serve.WriteTimeout = time.Second * time.Duration(cfg.Serve.WriteTimeout)
syncd.serve.IdleTimeout = time.Second * time.Duration(cfg.Serve.IdleTimeout)
return syncd
}
func (s *Syncd) Start() error {
return s.serve.Start()
}
func (s *Syncd) RegisterRoute(method, path string, handler goweb.HandlerFunc) {
s.serve.Handler(method, path, handler)
}
func (s *Syncd) RegisterServeHandler(h ServeHandler) {
s.serve.BeforeHandler = h.BeforeHandler
s.serve.AfterHandler = h.AfterHandler
s.serve.ServerErrorHandler = h.ServerErrorHandler
s.serve.NotFoundHandler = h.NotFoundHandler
s.serve.MethodNotAllowHandler = h.MethodNotAllowHandler
}
func (s *Syncd) UnRegisterRoute() {}
func (s *Syncd) RegisterOrm() {
DbInstance = NewDatabase(s.config.Db)
if err := DbInstance.Open(); err != nil {
panic(err)
}
Orm = DbInstance.DbHandler
}
func (s *Syncd) RegisterMail() {
sendmail := &SendMail{
Enable: s.config.Mail.Enable,
Smtp: s.config.Mail.Smtp,
Port: s.config.Mail.Port,
User: s.config.Mail.User,
Pass: s.config.Mail.Pass,
}
Mail = SendMailNew(sendmail)
}
func (s *Syncd) RegisterLog() {
var loggerHandler io.Writer
switch s.config.Log.Path {
case "stdout":
loggerHandler = os.Stdout
case "stderr":
loggerHandler = os.Stderr
case "":
loggerHandler = os.Stdout
default:
loggerHandler = golog.NewFileHandler(s.config.Log.Path)
}
Logger = golog.New(loggerHandler)
}
func (s *Syncd) InitEnv() {
DataDir = s.config.Syncd.Dir
if s.config.Syncd.Dir == "" {
path, err := gopath.CurrentPath()
if err != nil {
panic(err)
}
DataDir = path + "/data"
}
if err := gopath.CreatePath(DataDir); err != nil {
panic(err)
}
TmpDir = DataDir + "/tmp"
if err := gopath.CreatePath(TmpDir); err != nil {
panic(err)
}
if s.config.Syncd.Cipher == "" {
panic(errors.New("syncd config 'Cipher' not setting"))
}
dec, err := base64.StdEncoding.DecodeString(s.config.Syncd.Cipher)
if err != nil {
panic(err)
}
CipherKey = dec
RemoteTmpDir = "~/.syncd"
}