forked from nkanaev/yarr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
67 lines (57 loc) · 1.29 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
56
57
58
59
60
61
62
63
64
65
66
67
package server
import (
"log"
"net/http"
"sync"
"github.com/hades300/yarr/src/storage"
"github.com/hades300/yarr/src/worker"
)
type Server struct {
Addr string
db *storage.Storage
worker *worker.Worker
cache map[string]interface{}
cache_mutex *sync.Mutex
BasePath string
// auth
Username string
Password string
// https
CertFile string
KeyFile string
}
func NewServer(db *storage.Storage, addr string) *Server {
return &Server{
db: db,
Addr: addr,
worker: worker.NewWorker(db),
cache: make(map[string]interface{}),
cache_mutex: &sync.Mutex{},
}
}
func (h *Server) GetAddr() string {
proto := "http"
if h.CertFile != "" && h.KeyFile != "" {
proto = "https"
}
return proto + "://" + h.Addr + h.BasePath
}
func (s *Server) Start() {
refreshRate := s.db.GetSettingsValueInt64("refresh_rate")
s.worker.FindFavicons()
s.worker.StartFeedCleaner()
s.worker.SetRefreshRate(refreshRate)
if refreshRate > 0 {
s.worker.RefreshFeeds()
}
httpserver := &http.Server{Addr: s.Addr, Handler: s.handler()}
var err error
if s.CertFile != "" && s.KeyFile != "" {
err = httpserver.ListenAndServeTLS(s.CertFile, s.KeyFile)
} else {
err = httpserver.ListenAndServe()
}
if err != http.ErrServerClosed {
log.Fatal(err)
}
}