Skip to content

Commit

Permalink
.golangci.yml
Browse files Browse the repository at this point in the history
  • Loading branch information
artjoma committed Dec 25, 2023
1 parent 8f586c7 commit 5b9333a
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 23 deletions.
49 changes: 49 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# golangci-lint configuration

run:
skip-dirs:
- build
- examples

timeout: 10m

issues:

linters:
enable:
- asciicheck # check for non-ascii characters
- errorlint # enure error wrapping is safely done
- gocritic # check for certain simplifications
- gofmt # ensure code is formatted
- gosec # check for security concerns
- nilerr # ensure errors aren't mishandled
- staticcheck # check for suspicious constructs
- unused # check for unused constructs

linters-settings:
errcheck:
# report when type assertions aren't checked for errors as in
# a := b.(MyStruct)
#
check-type-assertions: true

gocritic:
disabled-tags:
- experimental
- opinionated

disabled-checks:
- ifElseChain
- assignOp
- unlambda
- exitAfterDefer

gosec:
excludes:
- G404 # checks that random numbers are securely generated

govet:
enable-all: true
disable:
- shadow
- fieldalignment
16 changes: 10 additions & 6 deletions api/http_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ func (api *HttpApi) putFileHandler(ctx *fiber.Ctx) error {
return api.writeErrResponse(ctx, http.StatusBadRequest, errors.New("got content length < 2"))
}
f, err := fileHeader.Open()
if err != nil {
return err
}
defer f.Close()

cmd := &engine.PutFileCmd{}
Expand All @@ -121,20 +124,21 @@ func (api *HttpApi) downloadFileHandler(ctx *fiber.Ctx) error {
return api.writeErrResponse(ctx, http.StatusBadRequest, errors.New("invalid request"))
}
_, buff, err := api.engine.DownloadFile(fId)

if err != nil {
return err
}
zr := lz4.NewReader(buff)
if err := zr.Apply(lz4.ConcurrencyOption(4)); err != nil {
return err
}

ctx.SendStream(zr)
return err
return ctx.SendStream(zr)
}

// TODO For browsers use attachment+file name
func (api *HttpApi) downloadFileAttachHandler(ctx *fiber.Ctx) error {
return nil
}
// func (api *HttpApi) downloadFileAttachHandler(ctx *fiber.Ctx) error {
// return nil
// }

func (api *HttpApi) getFileMetadata(ctx *fiber.Ctx) error {
fIdParam := ctx.Params("fId")
Expand Down
2 changes: 2 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
golangci-lint run

go build -ldflags "-s -w" -o build/mosaic
#env GOOS=windows GOARCH=amd64 go build -ldflags "-s -w" -o build/mosaic.exe
#env GOOS=darwin GOARCH=amd64 go build -ldflags "-s -w" -o build/mosaic-darwin
Expand Down
3 changes: 3 additions & 0 deletions engine/cmd_put_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ func (cmd *PutFileCmd) Prepare(engine *Engine) error {

// check if file already uploaded
checkMeta, err := cmd.engine.db.GetFileMetadataById(fileHash)
if err != nil {
return err
}
if checkMeta != nil {
cmd.FileMetadata = checkMeta
slog.Info("File already exists", "fId", fileHash.String(), "oSize", checkMeta.OriginalFileSize)
Expand Down
4 changes: 3 additions & 1 deletion engine/cmd_upload_err.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ func (cmd *FileUploadErrCmd) Execute() error {
err := cmd.Err
slog.Info("Try resolve upload err", "fId", fileMeta.Id.String(), "err", err.Error())
// decrease used space by file
cmd.engine.db.UpdateShardsSize(fileMeta.UsedSpace(), false)
if err := cmd.engine.db.UpdateShardsSize(fileMeta.UsedSpace(), false); err != nil {
return err
}
// async free resources on shards
go func() {
for _, chunk := range fileMeta.Chunks {
Expand Down
10 changes: 0 additions & 10 deletions engine/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,6 @@ func buildShardState() map[types.ShardId]uint64 {
return shards
}

func buildShardState2() map[types.ShardId]uint64 {
shards := map[types.ShardId]uint64{
1: 10,
2: 8,
3: 12,
4: 11,
}
return shards
}

func buildShardState3() map[types.ShardId]uint64 {
shards := map[types.ShardId]uint64{
1: 10,
Expand Down
3 changes: 2 additions & 1 deletion engine/file_downloader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package engine

import (
"bytes"
"context"
"fmt"
"github.com/pierrec/lz4/v4"
"io"
Expand All @@ -17,7 +18,7 @@ import (
func TestFileUploader(t *testing.T) {
database := mosaicdb.NewDatabase("")
storage := storage.NewStorage()
engine, err := NewEngine("/tmp/masaic/temp", database, storage, 10)
engine, err := NewEngine(context.Background(), "/tmp/masaic/temp", database, storage, 10)
if err != nil {
panic(err)
}
Expand Down
6 changes: 3 additions & 3 deletions mosaicdb/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ func NewDatabase(dbPath string) *Database {
slog.Info("Initialize db", "path", dbPath)
os.MkdirAll(dbPath, 0774)
db, err = pebble.Open(dbPath, &pebble.Options{})
if err != nil {
panic(err)
}
}
if err != nil {
panic(err)
}
return &Database{
db: db,
Expand Down
8 changes: 6 additions & 2 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ func RandomString(count int) string {

func RandomBytes(count int) []byte {
buf := make([]byte, count)
cp.Read(buf)
if _, err := cp.Read(buf); err != nil {
panic(err)
}
return buf
}

Expand Down Expand Up @@ -81,6 +83,8 @@ func Btou32(val []byte) uint32 {

func HashFrom(data []byte) types.H256 {
hasher := blake3.New()
hasher.Write(data)
if _, err := hasher.Write(data); err != nil {
panic(err)
}
return hasher.Sum(nil)
}

0 comments on commit 5b9333a

Please sign in to comment.