From e53265472f6e6c138507c86f461f6369ff718e74 Mon Sep 17 00:00:00 2001 From: Nazar Kanaev Date: Tue, 16 Mar 2021 21:20:01 +0000 Subject: [PATCH] rename handler -> server --- src/main.go | 2 +- src/platform/guiless.go | 2 +- src/server/server.go | 25 +++++++++++++------------ 3 files changed, 15 insertions(+), 14 deletions(-) diff --git a/src/main.go b/src/main.go index b543c95..cfb121c 100644 --- a/src/main.go +++ b/src/main.go @@ -90,7 +90,7 @@ func main() { log.Fatal("Failed to initialise database: ", err) } - srv := server.New(store, addr) + srv := server.NewServer(store, addr) if certfile != "" && keyfile != "" { srv.CertFile = certfile diff --git a/src/platform/guiless.go b/src/platform/guiless.go index 559fa28..702fd7e 100644 --- a/src/platform/guiless.go +++ b/src/platform/guiless.go @@ -6,6 +6,6 @@ import ( "github.com/nkanaev/yarr/src/server" ) -func Start(s *server.Handler) { +func Start(s *server.Server) { s.Start() } diff --git a/src/server/server.go b/src/server/server.go index 2548abf..9dfaf2e 100644 --- a/src/server/server.go +++ b/src/server/server.go @@ -12,7 +12,7 @@ import ( "github.com/nkanaev/yarr/src/storage" ) -type Handler struct { +type Server struct { Addr string db *storage.Storage feedQueue chan storage.Feed @@ -26,9 +26,9 @@ type Handler struct { KeyFile string } -func New(db *storage.Storage, addr string) *Handler { +func NewServer(db *storage.Storage, addr string) *Server { queueSize := int32(0) - return &Handler{ + return &Server{ db: db, feedQueue: make(chan storage.Feed, 3000), 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" if h.CertFile != "" && h.KeyFile != "" { proto = "https" @@ -45,8 +45,9 @@ func (h *Handler) GetAddr() string { return proto + "://" + h.Addr + BasePath } -func (h *Handler) Start() { +func (h *Server) Start() { h.startJobs() + s := &http.Server{Addr: h.Addr, Handler: h} var err error @@ -64,7 +65,7 @@ func unsafeMethod(method string) bool { 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 if 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)) } -func (h *Handler) startJobs() { +func (h *Server) startJobs() { delTicker := time.NewTicker(time.Hour * 24) 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 != "" } -func (h *Handler) fetchAllFeeds() { +func (h *Server) fetchAllFeeds() { log.Print("Refreshing all feeds") h.db.ResetFeedErrors() 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) h.feedQueue <- feed } @@ -214,8 +215,8 @@ func db(req *http.Request) *storage.Storage { return nil } -func handler(req *http.Request) *Handler { - return req.Context().Value(ctxHandler).(*Handler) +func handler(req *http.Request) *Server { + return req.Context().Value(ctxHandler).(*Server) } const (