-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
55 lines (45 loc) · 1.02 KB
/
server.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
package server
import (
"github.com/koihuang/speedfs/config"
"github.com/koihuang/speedfs/util"
"github.com/robfig/cron/v3"
"github.com/sirupsen/logrus"
"net/http"
"path"
"strconv"
)
var log = logrus.StandardLogger()
type Server struct {
fileRootDir string
staticHandler http.Handler
}
func New() (*Server, error) {
fileRootDir := path.Join(config.SpeedfsPath(), config.FILE_DIR_NAME)
return &Server{
fileRootDir: fileRootDir,
staticHandler: http.StripPrefix("/", http.FileServer(http.Dir(fileRootDir))),
}, nil
}
func (server *Server) Start() error {
server.initRouter()
srv := &http.Server{
Addr: ":" + strconv.Itoa(config.GloableConfig.Port),
Handler: &HttpHandler{},
}
util.Go(server.repairyAt3AmEveryDay)
err := srv.ListenAndServe()
if err != nil {
return err
}
return nil
}
func (server *Server) repairyAt3AmEveryDay() {
c := cron.New()
_, err := c.AddFunc("0 3 * * *", func() {
server.repairLastDay()
})
if err != nil {
log.Errorf("add cron err:%s", err.Error())
}
c.Start()
}