mirror of
https://github.com/nkanaev/yarr.git
synced 2025-09-13 09:55:36 +00:00
65 lines
1.2 KiB
Go
65 lines
1.2 KiB
Go
package server
|
|
|
|
import (
|
|
"log"
|
|
"net/http"
|
|
|
|
"github.com/nkanaev/yarr/src/storage"
|
|
"github.com/nkanaev/yarr/src/worker"
|
|
)
|
|
|
|
type Server struct {
|
|
Addr string
|
|
db *storage.Storage
|
|
worker *worker.Worker
|
|
cache map[string]interface{}
|
|
|
|
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{}),
|
|
}
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|