Skip to content

Commit

Permalink
Init commit
Browse files Browse the repository at this point in the history
  • Loading branch information
aopoltorzhicky committed Jul 25, 2023
1 parent fed35fe commit a97b844
Show file tree
Hide file tree
Showing 13 changed files with 2,052 additions and 0 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Release
on:
push:
tags:
- 'v*.*.*'

jobs:
release:
name: Release
runs-on: ubuntu-latest
env:
ACTIONS_ALLOW_UNSECURE_COMMANDS: true
steps:
- name: Check out the repo
uses: actions/checkout@v2

- name: Set up enviroment
run: echo ::set-env name=RELEASE_VERSION::${GITHUB_REF#refs/*/}

- name: Publish GitHub release
uses: "marvinpinto/action-automatic-releases@latest"
with:
repo_token: "${{ secrets.GITHUB_TOKEN }}"
prerelease: false
41 changes: 41 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: Tests
on: push

permissions:
contents: read

jobs:
lint:
name: lint
runs-on: ubuntu-latest
steps:
- uses: actions/setup-go@v3
with:
go-version: '1.20'
- uses: actions/checkout@v3
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
version: v1.51.2
args: --timeout=4m
test:
runs-on: ubuntu-latest
steps:
- name: install Go
uses: actions/setup-go@v2
with:
go-version: 1.20.x
- name: checkout code
uses: actions/checkout@v2
- uses: actions/cache@v2
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
- name: golang tests
env:
GO111MODULE: on
run: |
go mod download
go test ./...
18 changes: 18 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib
.env
.idea
.DS_Store

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Dependency directories (remove the comment below to include it)
vendor/
14 changes: 14 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
linters:
enable:
- goconst
- gocritic
- gofmt
- govet
- prealloc
- unconvert
- unused
- errcheck
- ineffassign
- containedctx
- tenv
- musttag
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
lint:
golangci-lint run

test:
go test ./...
14 changes: 14 additions & 0 deletions data.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package main

// Data -
type Data struct {
Raw []byte
Node string
ResponseTime int64
}

// Provider -
type Provider struct {
ID string `yaml:"id" validate:"required"`
Address string `yaml:"addr" validate:"required"`
}
13 changes: 13 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package main

import "errors"

// Errors
var (
ErrInvalidURI = errors.New("invalid URI")
ErrEmptyIPFSGatewayList = errors.New("empty IPFS gateway list")
ErrHTTPRequest = errors.New("HTTP request error")
ErrJSONDecoding = errors.New("JSON decoding error")
ErrNoIPFSResponse = errors.New("can't load document from IPFS")
ErrInvalidCID = errors.New("invalid CID")
)
73 changes: 73 additions & 0 deletions functions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package main

import (
"fmt"
"math/rand"
"net/url"
"regexp"
"strings"

"github.com/ipfs/go-cid"
"github.com/pkg/errors"
)

// Hash - separate IPFS hash from link
func Hash(link string) (string, error) {
hash := FindAllLinks([]byte(link))
if len(hash) != 1 {
return "", errors.Errorf("invalid IPFS link: %s", link)
}
_, err := cid.Decode(hash[0])
return hash[0], err
}

// Link - get gateway link
func Link(gateway, hash string) string {
return fmt.Sprintf("%s/ipfs/%s", gateway, hash)
}

// Path - get path without protocol
func Path(link string) string {
return strings.TrimPrefix(link, "ipfs://")
}

var ipfsURL = regexp.MustCompile(`ipfs:\/\/(?P<hash>(baf[123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz]{56})|Qm[123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz]{44})`)

// FindAllLinks -
func FindAllLinks(data []byte) []string {
matches := ipfsURL.FindAllSubmatch(data, -1)
if len(matches) == 0 {
return nil
}

res := make([]string, 0)
for i := range matches {
if len(matches[i]) != 2 {
continue
}
res = append(res, string(matches[i][1]))
}
return res
}

// ShuffleGateways - shuffle gateways for different request order for different files
func ShuffleGateways(gateways []string) []string {
if len(gateways) < 2 {
return gateways
}

shuffled := make([]string, len(gateways))
copy(shuffled, gateways)
rand.Shuffle(len(shuffled), func(i, j int) { shuffled[i], shuffled[j] = shuffled[j], shuffled[i] })
return shuffled
}

// Is -
func Is(link string) bool {
u, err := url.Parse(link)
if err != nil {
return false
}
_, err = cid.Decode(u.Host)
return err == nil
}
35 changes: 35 additions & 0 deletions functions_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package main

import "testing"

func TestIs(t *testing.T) {
tests := []struct {
name string
link string
want bool
}{
{
name: "ipfs://QmWYTUjkRusrhz4BCmoMKfSA8DBXk5pH2oAN83S9mABE3w",
link: "ipfs://QmWYTUjkRusrhz4BCmoMKfSA8DBXk5pH2oAN83S9mABE3w",
want: true,
}, {
name: "ipfs://zdj7WkPvrxL7VxiWbjBP5rfshPtAzXwZ77uvZhfSAoHDeb3iw/1",
link: "ipfs://zdj7WkPvrxL7VxiWbjBP5rfshPtAzXwZ77uvZhfSAoHDeb3iw/1",
want: true,
}, {
name: "ipfs://bafkreie7cvrfe6cgiat6nrffmtlf5al4fkae6hxtoy7lfebbz62v32lyvi",
link: "ipfs://bafkreie7cvrfe6cgiat6nrffmtlf5al4fkae6hxtoy7lfebbz62v32lyvi",
want: true,
}, {
name: "invalid",
link: "ipfs://invalid",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := Is(tt.link); got != tt.want {
t.Errorf("Is() = %v, want %v", got, tt.want)
}
})
}
}
Loading

0 comments on commit a97b844

Please sign in to comment.