Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ jobs:
MONGO_TEST: mongodb://127.0.0.1:27017

- name: golangci-lint
uses: golangci/golangci-lint-action@v6
uses: golangci/golangci-lint-action@v7
with:
version: v1.62
version: v2.6
env:
TZ: "America/Chicago"

Expand Down
78 changes: 36 additions & 42 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,30 +1,7 @@
linters-settings:
govet:
enable:
- shadow
gocyclo:
min-complexity: 15
dupl:
threshold: 100
goconst:
min-len: 2
min-occurrences: 2
misspell:
locale: US
lll:
line-length: 140
gocritic:
enabled-tags:
- performance
- style
- experimental
disabled-checks:
- wrapperFunc
- hugeParam
- rangeValCopy
version: "2"

linters:
disable-all: true
default: none
enable:
- bodyclose
- revive
Expand All @@ -35,29 +12,46 @@ linters:
- gosec
- misspell
- unparam
- typecheck
- ineffassign
- stylecheck
- gochecknoinits
- copyloopvar
- gocritic
- nakedret
- gosimple
- prealloc

fast: false
settings:
govet:
enable:
- shadow
gocyclo:
min-complexity: 15
dupl:
threshold: 100
goconst:
min-len: 2
min-occurrences: 2
misspell:
locale: US
lll:
line-length: 140
gocritic:
enabled-tags:
- performance
- style
- experimental
disabled-checks:
- wrapperFunc
- hugeParam
- rangeValCopy
exclusions:
paths:
- vendor
rules:
- text: "weak cryptographic primitive"
linters:
- gosec
- text: "package-comments: should have a package comment"
linters:
- revive

run:
concurrency: 4

issues:
exclude-dirs:
- vendor
exclude-rules:
- text: "weak cryptographic primitive"
linters:
- gosec
- text: "package-comments: should have a package comment"
linters:
- revive
exclude-use-default: false
2 changes: 1 addition & 1 deletion app/agent/demo_mode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (m *mockDemoWriter) Write(p []byte) (int, error) {

func (m *mockDemoWriter) Get() []string {
m.Lock()
res := m.Buffer.String()
res := m.String()
m.Unlock()
return strings.Split(res, "\n")
}
2 changes: 1 addition & 1 deletion app/agent/event_loop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func TestDemo(t *testing.T) {
el.Run(ctx)
wrStrings := lwr.Get()
t.Logf("%v", wrStrings)
assert.True(t, len(wrStrings) >= 25 && len(wrStrings) <= 31, len(wrStrings))
assert.True(t, len(wrStrings) >= 25 && len(wrStrings) <= 35, len(wrStrings))
}

type mockLogClient struct{}
Expand Down
2 changes: 1 addition & 1 deletion app/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func main() {
}

for name, command := range dispatch {
if p.Active != nil && p.Command.Find(name) == p.Active {
if p.Active != nil && p.Find(name) == p.Active {
if err := command.Run(ctx); err != nil {
log.Printf("[ERROR] %s failed, %v", name, err)
os.Exit(1)
Expand Down
10 changes: 5 additions & 5 deletions app/server/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (m *Mongo) Publish(records []core.LogEntry) (err error) {
recs[i] = m.makeMongoEntry(v)
}

coll := m.Database(m.MongoParams.DBName).Collection(m.MongoParams.Collection)
coll := m.Database(m.DBName).Collection(m.Collection)
res, err := coll.InsertMany(context.TODO(), recs)
if err != nil {
return errors.Wrapf(err, "publish %d records", len(records))
Expand Down Expand Up @@ -105,7 +105,7 @@ func (m *Mongo) LastPublished() (entry core.LogEntry, err error) {
}

var mentry mongoLogEntry
coll := m.Database(m.MongoParams.DBName).Collection(m.MongoParams.Collection)
coll := m.Database(m.DBName).Collection(m.Collection)
res := coll.FindOne(context.TODO(), bson.M{}, options.FindOne().SetSort(bson.D{{Key: "_id", Value: -1}}))
if err := res.Decode(&mentry); err != nil {
return core.LogEntry{}, nil
Expand All @@ -131,7 +131,7 @@ func (m *Mongo) Find(req core.Request) ([]core.LogEntry, error) {
query := m.makeQuery(req)

var mresult []mongoLogEntry
coll := m.Database(m.MongoParams.DBName).Collection(m.MongoParams.Collection)
coll := m.Database(m.DBName).Collection(m.Collection)

sortOpt := bson.D{{Key: "_id", Value: 1}}
if req.LastID == "" || req.LastID == "0" {
Expand Down Expand Up @@ -226,15 +226,15 @@ func (m *Mongo) init(collection string) error {
{Keys: bson.D{{Key: "container", Value: 1}, {Key: "ts", Value: 1}}},
}

err := m.Client.Database(m.MongoParams.DBName).CreateCollection(context.Background(), m.MongoParams.Collection,
err := m.Client.Database(m.DBName).CreateCollection(context.Background(), m.Collection,
options.CreateCollection().SetCapped(true).SetSizeInBytes(int64(m.MaxCollectionSize)).
SetMaxDocuments(int64(m.MaxDocs)))

if err != nil && !strings.Contains(err.Error(), "already exists") {
return errors.Wrapf(err, "initilize collection %s with %+v", collection, m.MongoParams)
}

coll := m.Database(m.MongoParams.DBName).Collection(m.MongoParams.Collection)
coll := m.Database(m.DBName).Collection(m.Collection)
if _, err := coll.Indexes().CreateMany(context.TODO(), indexes); err != nil {
return errors.Wrap(err, "create indexes")
}
Expand Down
52 changes: 20 additions & 32 deletions app/server/rest_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@ import (
"net/http"
"time"

"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/go-chi/render"
log "github.com/go-pkgz/lgr"
"github.com/go-pkgz/rest"
"github.com/go-pkgz/rest/logger"
"github.com/go-pkgz/routegroup"

"github.com/umputun/dkll/app/core"
)
Expand Down Expand Up @@ -58,32 +56,28 @@ func (s *RestServer) Run(ctx context.Context) error {
return srv.ListenAndServe()
}

func (s *RestServer) router() chi.Router {
router := chi.NewRouter()
router.Use(middleware.RequestID, middleware.RealIP, rest.Recoverer(log.Default()))
router.Use(middleware.Throttle(100), middleware.Timeout(60*time.Second))
func (s *RestServer) router() http.Handler {
router := routegroup.New(http.NewServeMux())
router.Use(rest.Recoverer(log.Default()))
router.Use(rest.Throttle(100))
router.Use(rest.AppInfo("dkll", "umputun", s.Version))
router.Use(rest.Ping, rest.SizeLimit(1024))
router.Use(logger.New(logger.Log(log.Default()), logger.WithBody, logger.Prefix("[DEBUG]")).Handler)

router.Route("/v1", func(r chi.Router) {
r.Post("/find", s.findCtrl)
r.Post("/stream", s.streamCtrl)
r.Get("/last", s.lastCtrl)
router.Mount("/v1").Route(func(r *routegroup.Bundle) {
r.HandleFunc("POST /find", s.findCtrl)
r.HandleFunc("POST /stream", s.streamCtrl)
r.HandleFunc("GET /last", s.lastCtrl)
})
return router
}

// POST /v1/find, body is Request. Returns list of LogEntry
// containers,hosts and excludes lists support regexp in "//", i.e. /regex/
func (s *RestServer) findCtrl(w http.ResponseWriter, r *http.Request) {

req := core.Request{}

err := render.DecodeJSON(r.Body, &req)
if err != nil {
render.Status(r, http.StatusBadRequest)
render.JSON(w, r, rest.JSON{"error": err.Error()})
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
rest.SendErrorJSON(w, r, log.Default(), http.StatusBadRequest, err, "failed to decode request")
return
}

Expand All @@ -93,29 +87,25 @@ func (s *RestServer) findCtrl(w http.ResponseWriter, r *http.Request) {

recs, err := s.DataService.Find(req)
if err != nil {
render.Status(r, http.StatusBadRequest)
render.JSON(w, r, rest.JSON{"error": err.Error()})
rest.SendErrorJSON(w, r, log.Default(), http.StatusBadRequest, err, "failed to find records")
return
}

render.JSON(w, r, recs)
rest.RenderJSON(w, recs)
}

// POST /v1/stream?timeout=30s, body is Request. Stream list of LogEntry, breaks on timeout
// containers,hosts and excludes lists support regexp in "//", i.e. /regex/
func (s *RestServer) streamCtrl(w http.ResponseWriter, r *http.Request) {

req := core.Request{}

timeout := 5 * time.Minute // max timeout
if tm, err := time.ParseDuration(r.URL.Query().Get("timeout")); err == nil && tm <= timeout {
timeout = tm
}

err := render.DecodeJSON(r.Body, &req)
if err != nil {
render.Status(r, http.StatusBadRequest)
render.JSON(w, r, rest.JSON{"error": err.Error()})
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
rest.SendErrorJSON(w, r, log.Default(), http.StatusBadRequest, err, "failed to decode request")
return
}

Expand All @@ -127,15 +117,13 @@ func (s *RestServer) streamCtrl(w http.ResponseWriter, r *http.Request) {
for {
recs, err := s.DataService.Find(req)
if err != nil {
render.Status(r, http.StatusBadRequest)
render.JSON(w, r, rest.JSON{"error": err.Error()})
rest.SendErrorJSON(w, r, log.Default(), http.StatusBadRequest, err, "failed to find records")
return
}
if len(recs) > 0 {
for _, rec := range recs {
if err := json.NewEncoder(w).Encode(rec); err != nil {
render.Status(r, http.StatusInternalServerError)
render.JSON(w, r, rest.JSON{"error": err.Error()})
rest.SendErrorJSON(w, r, log.Default(), http.StatusInternalServerError, err, "failed to encode record")
return
}
}
Expand All @@ -161,8 +149,8 @@ func (s *RestServer) streamCtrl(w http.ResponseWriter, r *http.Request) {
func (s *RestServer) lastCtrl(w http.ResponseWriter, r *http.Request) {
last, err := s.DataService.LastPublished()
if err != nil {
render.Status(r, http.StatusBadRequest)
render.JSON(w, r, rest.JSON{"error": err.Error()})
rest.SendErrorJSON(w, r, log.Default(), http.StatusBadRequest, err, "failed to get last published")
return
}
render.JSON(w, r, last)
rest.RenderJSON(w, last)
}
41 changes: 19 additions & 22 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,61 +4,58 @@ go 1.24.0

require (
github.com/fatih/color v1.18.0
github.com/fsouza/go-dockerclient v1.12.2
github.com/go-chi/chi/v5 v5.1.0
github.com/go-chi/render v1.0.3
github.com/go-pkgz/lgr v0.11.1
github.com/fsouza/go-dockerclient v1.12.3
github.com/go-pkgz/lgr v0.12.1
github.com/go-pkgz/mongo/v2 v2.2.1
github.com/go-pkgz/repeater v1.2.0
github.com/go-pkgz/rest v1.19.0
github.com/go-pkgz/rest v1.20.4
github.com/go-pkgz/routegroup v1.6.0
github.com/hashicorp/go-multierror v1.1.1
github.com/hashicorp/go-syslog v1.0.0
github.com/jessevdk/go-flags v1.6.1
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.10.0
go.mongodb.org/mongo-driver v1.17.1
github.com/stretchr/testify v1.11.1
go.mongodb.org/mongo-driver v1.17.6
gopkg.in/mcuadros/go-syslog.v2 v2.3.0
gopkg.in/natefinch/lumberjack.v2 v2.2.1
)

require (
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 // indirect
github.com/Azure/go-ansiterm v0.0.0-20250102033503-faa5f7b0171c // indirect
github.com/Microsoft/go-winio v0.6.2 // indirect
github.com/ajg/form v1.5.1 // indirect
github.com/containerd/log v0.1.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/docker/docker v28.3.3+incompatible // indirect
github.com/docker/go-connections v0.5.0 // indirect
github.com/docker/docker v28.5.2+incompatible // indirect
github.com/docker/go-connections v0.6.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/golang/snappy v1.0.0 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/klauspost/compress v1.18.0 // indirect
github.com/klauspost/compress v1.18.2 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-colorable v0.1.14 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/moby/docker-image-spec v1.3.1 // indirect
github.com/moby/go-archive v0.1.0 // indirect
github.com/moby/patternmatcher v0.6.0 // indirect
github.com/moby/sys/sequential v0.6.0 // indirect
github.com/moby/sys/user v0.4.0 // indirect
github.com/moby/sys/userns v0.1.0 // indirect
github.com/moby/term v0.5.0 // indirect
github.com/moby/term v0.5.2 // indirect
github.com/montanaflynn/stats v0.7.1 // indirect
github.com/morikuni/aec v1.0.0 // indirect
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.1.0 // indirect
github.com/opencontainers/image-spec v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
github.com/xdg-go/scram v1.1.2 // indirect
github.com/xdg-go/scram v1.2.0 // indirect
github.com/xdg-go/stringprep v1.0.4 // indirect
github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78 // indirect
golang.org/x/crypto v0.29.0 // indirect
golang.org/x/sync v0.9.0 // indirect
golang.org/x/sys v0.35.0 // indirect
golang.org/x/text v0.20.0 // indirect
golang.org/x/crypto v0.45.0 // indirect
golang.org/x/sync v0.18.0 // indirect
golang.org/x/sys v0.38.0 // indirect
golang.org/x/text v0.31.0 // indirect
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading
Loading