archive/ArchiveServer.go

66 lines
1.5 KiB
Go
Raw Normal View History

2017-08-13 01:57:08 +00:00
package archive
import (
2017-08-13 03:47:03 +00:00
"errors"
2017-08-13 01:57:08 +00:00
"fmt"
"regexp"
2017-08-13 01:57:08 +00:00
"time"
)
const (
SERVER_VERSION = "archive/3.0"
)
type ArchiveServer struct {
timezone *time.Location
cfg *Config
startup time.Time
rxViewRoot, rxViewPage, rxSearch, rxSearchRx *regexp.Regexp
2017-08-13 01:57:08 +00:00
}
func NewArchiveServer(cfg *Config) (*ArchiveServer, error) {
tz, err := time.LoadLocation(cfg.Timezone)
if err != nil {
return nil, err
}
if cfg.LinesPerPage <= 0 {
return nil, fmt.Errorf("Invalid %d lines per page", cfg.LinesPerPage)
}
if len(cfg.Logs) == 0 {
return nil, fmt.Errorf("No log sources configured")
}
for _, ls := range cfg.Logs {
if len(ls.FileLocation) == 0 {
return nil, fmt.Errorf(`No file locations for log source "%s"`, ls.Description)
}
}
return &ArchiveServer{
timezone: tz,
cfg: cfg,
startup: time.Now(),
rxViewRoot: regexp.MustCompile(`^/([^/]+)/(\d+)/(\d+)$`),
rxViewPage: regexp.MustCompile(`^/([^/]+)/(\d+)/(\d+)/(?:page-)?(\d+)$`),
rxSearch: regexp.MustCompile(`^/([^/]+)/search/(.*)$`),
rxSearchRx: regexp.MustCompile(`^/([^/]+)/rx/(.*)$`),
2017-08-13 01:57:08 +00:00
}, nil
}
2017-08-13 03:47:03 +00:00
var ErrNoLogForMonth = errors.New("No logs for the selected month.")
func (this *ArchiveServer) LogFile(ls *LogSource, ym YearMonth) (string, error) {
ymIndex := ym.Index()
for _, fl := range ls.FileLocation {
if fl.StartMonth.Index() <= ymIndex && (fl.EndMonth == nil || fl.EndMonth.Index() >= ymIndex) {
return time.Date(ym.Year, ym.Month, 1, 0, 0, 0, 0, this.timezone).Format(fl.LogFilePath), nil
}
}
return "", ErrNoLogForMonth
}