rename handler -> server

This commit is contained in:
Nazar Kanaev 2021-03-16 21:20:01 +00:00
parent 66fdbef90b
commit e53265472f
3 changed files with 15 additions and 14 deletions

View File

@ -90,7 +90,7 @@ func main() {
log.Fatal("Failed to initialise database: ", err) log.Fatal("Failed to initialise database: ", err)
} }
srv := server.New(store, addr) srv := server.NewServer(store, addr)
if certfile != "" && keyfile != "" { if certfile != "" && keyfile != "" {
srv.CertFile = certfile srv.CertFile = certfile

View File

@ -6,6 +6,6 @@ import (
"github.com/nkanaev/yarr/src/server" "github.com/nkanaev/yarr/src/server"
) )
func Start(s *server.Handler) { func Start(s *server.Server) {
s.Start() s.Start()
} }

View File

@ -12,7 +12,7 @@ import (
"github.com/nkanaev/yarr/src/storage" "github.com/nkanaev/yarr/src/storage"
) )
type Handler struct { type Server struct {
Addr string Addr string
db *storage.Storage db *storage.Storage
feedQueue chan storage.Feed feedQueue chan storage.Feed
@ -26,9 +26,9 @@ type Handler struct {
KeyFile string KeyFile string
} }
func New(db *storage.Storage, addr string) *Handler { func NewServer(db *storage.Storage, addr string) *Server {
queueSize := int32(0) queueSize := int32(0)
return &Handler{ return &Server{
db: db, db: db,
feedQueue: make(chan storage.Feed, 3000), feedQueue: make(chan storage.Feed, 3000),
queueSize: &queueSize, queueSize: &queueSize,
@ -37,7 +37,7 @@ func New(db *storage.Storage, addr string) *Handler {
} }
} }
func (h *Handler) GetAddr() string { func (h *Server) GetAddr() string {
proto := "http" proto := "http"
if h.CertFile != "" && h.KeyFile != "" { if h.CertFile != "" && h.KeyFile != "" {
proto = "https" proto = "https"
@ -45,8 +45,9 @@ func (h *Handler) GetAddr() string {
return proto + "://" + h.Addr + BasePath return proto + "://" + h.Addr + BasePath
} }
func (h *Handler) Start() { func (h *Server) Start() {
h.startJobs() h.startJobs()
s := &http.Server{Addr: h.Addr, Handler: h} s := &http.Server{Addr: h.Addr, Handler: h}
var err error var err error
@ -64,7 +65,7 @@ func unsafeMethod(method string) bool {
return method == "POST" || method == "PUT" || method == "DELETE" return method == "POST" || method == "PUT" || method == "DELETE"
} }
func (h Handler) ServeHTTP(rw http.ResponseWriter, req *http.Request) { func (h Server) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
reqPath := req.URL.Path reqPath := req.URL.Path
if BasePath != "" { if BasePath != "" {
if !strings.HasPrefix(reqPath, BasePath) { if !strings.HasPrefix(reqPath, BasePath) {
@ -99,7 +100,7 @@ func (h Handler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
route.handler(rw, req.WithContext(ctx)) route.handler(rw, req.WithContext(ctx))
} }
func (h *Handler) startJobs() { func (h *Server) startJobs() {
delTicker := time.NewTicker(time.Hour * 24) delTicker := time.NewTicker(time.Hour * 24)
syncSearchChannel := make(chan bool, 10) syncSearchChannel := make(chan bool, 10)
@ -183,11 +184,11 @@ func (h *Handler) startJobs() {
} }
} }
func (h Handler) requiresAuth() bool { func (h Server) requiresAuth() bool {
return h.Username != "" && h.Password != "" return h.Username != "" && h.Password != ""
} }
func (h *Handler) fetchAllFeeds() { func (h *Server) fetchAllFeeds() {
log.Print("Refreshing all feeds") log.Print("Refreshing all feeds")
h.db.ResetFeedErrors() h.db.ResetFeedErrors()
for _, feed := range h.db.ListFeeds() { for _, feed := range h.db.ListFeeds() {
@ -195,7 +196,7 @@ func (h *Handler) fetchAllFeeds() {
} }
} }
func (h *Handler) fetchFeed(feed storage.Feed) { func (h *Server) fetchFeed(feed storage.Feed) {
atomic.AddInt32(h.queueSize, 1) atomic.AddInt32(h.queueSize, 1)
h.feedQueue <- feed h.feedQueue <- feed
} }
@ -214,8 +215,8 @@ func db(req *http.Request) *storage.Storage {
return nil return nil
} }
func handler(req *http.Request) *Handler { func handler(req *http.Request) *Server {
return req.Context().Value(ctxHandler).(*Handler) return req.Context().Value(ctxHandler).(*Server)
} }
const ( const (