More fixes

This commit is contained in:
James Mills
2021-01-30 15:26:24 +10:00
parent 401f635b3b
commit 8495ba0319
8 changed files with 15 additions and 304 deletions

View File

@@ -41,27 +41,12 @@ type Server struct {
router *Router
server *http.Server
// Blogs Cache
blogs *BlogsCache
// Messages Cache
msgs *MessagesCache
// Feed Cache
cache *Cache
// Feed Archiver
archive Archiver
// Data Store
db Store
// Scheduler
cron *cron.Cron
// Dispatcher
tasks *Dispatcher
// Auth
am *auth.Manager
@@ -72,21 +57,11 @@ type Server struct {
// API
api *API
// POP3 Service
pop3Service *POP3Service
// SMTP Service
smtpService *SMTPService
// Passwords
pm passwords.Passwords
}
func (s *Server) render(name string, w http.ResponseWriter, ctx *Context) {
if ctx.Authenticated && ctx.Username != "" {
ctx.NewMessages = s.msgs.Get(ctx.User.Username)
}
buf, err := s.tmplman.Exec(name, ctx)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
@@ -112,8 +87,6 @@ func (s *Server) AddShutdownHook(f func()) {
// Shutdown ...
func (s *Server) Shutdown(ctx context.Context) error {
s.cron.Stop()
s.tasks.Stop()
s.smtpService.Stop()
if err := s.server.Shutdown(ctx); err != nil {
log.WithError(err).Error("error shutting down server")
@@ -190,13 +163,6 @@ func (s *Server) setupMetrics() {
)
// database keys
metrics.NewGaugeFunc(
"db", "feeds",
"Number of database /feeds keys",
func() float64 {
return float64(s.db.LenFeeds())
},
)
metrics.NewGaugeFunc(
"db", "sessions",
"Number of database /sessions keys",
@@ -264,7 +230,7 @@ func (s *Server) setupCronJobs() error {
continue
}
job := jobSpec.Factory(s.config, s.blogs, s.cache, s.archive, s.db)
job := jobSpec.Factory(s.config, s.db)
if err := s.cron.AddJob(jobSpec.Schedule, job); err != nil {
return err
}
@@ -279,7 +245,7 @@ func (s *Server) runStartupJobs() {
log.Info("running startup jobs")
for name, jobSpec := range StartupJobs {
job := jobSpec.Factory(s.config, s.blogs, s.cache, s.archive, s.db)
job := jobSpec.Factory(s.config, s.db)
log.Infof("running %s now...", name)
job.Run()
}
@@ -380,38 +346,6 @@ func NewServer(bind string, options ...Option) (*Server, error) {
return nil, fmt.Errorf("error validating config: %w", err)
}
blogs, err := LoadBlogsCache(config.Data)
if err != nil {
log.WithError(err).Error("error loading blogs cache (re-creating)")
blogs = NewBlogsCache()
log.Info("updating blogs cache")
blogs.UpdateBlogs(config)
}
if len(blogs.Blogs) == 0 {
log.Info("empty blogs cache, updating...")
blogs.UpdateBlogs(config)
}
msgs, err := LoadMessagesCache(config.Data)
if err != nil {
log.WithError(err).Error("error loading messages cache (re-creating)")
msgs = NewMessagesCache()
}
log.Info("updating messages cache")
msgs.Refresh(config)
cache, err := LoadCache(config.Data)
if err != nil {
log.WithError(err).Error("error loading feed cache")
return nil, err
}
archive, err := NewDiskArchiver(filepath.Join(config.Data, archiveDir))
if err != nil {
log.WithError(err).Error("error creating feed archiver")
return nil, err
}
db, err := NewStore(config.Store)
if err != nil {
log.WithError(err).Error("error creating store")
@@ -433,8 +367,6 @@ func NewServer(bind string, options ...Option) (*Server, error) {
am := auth.NewManager(auth.NewOptions("/login", "/register"))
tasks := NewDispatcher(10, 100) // TODO: Make this configurable?
pm := passwords.NewScryptPasswords(nil)
sc := NewSessionStore(db, config.SessionCacheTTL)
@@ -449,11 +381,7 @@ func NewServer(bind string, options ...Option) (*Server, error) {
sc,
)
api := NewAPI(router, config, cache, archive, db, pm, tasks)
pop3Service := NewPOP3Service(config, db, pm, msgs, tasks)
smtpService := NewSMTPService(config, db, pm, msgs, tasks)
api := NewAPI(router, config, db, pm)
csrfHandler := nosurf.New(router)
csrfHandler.ExemptGlob("/api/v1/*")
@@ -479,33 +407,12 @@ func NewServer(bind string, options ...Option) (*Server, error) {
// API
api: api,
// POP3 Servicee
pop3Service: pop3Service,
// SMTP Servicee
smtpService: smtpService,
// Blogs Cache
blogs: blogs,
// Messages Cache
msgs: msgs,
// Feed Cache
cache: cache,
// Feed Archiver
archive: archive,
// Data Store
db: db,
// Schedular
cron: cron.New(),
// Dispatcher
tasks: tasks,
// Auth Manager
am: am,
@@ -527,12 +434,6 @@ func NewServer(bind string, options ...Option) (*Server, error) {
server.tasks.Start()
log.Info("started task dispatcher")
server.pop3Service.Start()
log.Info("started POP3 service")
server.smtpService.Start()
log.Info("started SMTP service")
server.setupMetrics()
log.Infof("serving metrics endpoint at %s/metrics", server.config.BaseURL)