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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,17 @@ jobs:
go-version: ${{ env.GO_VERSION }}

- name: Download Go modules
run: go mod tidy && go mod download
run: cd backend && go mod tidy && go mod download

- name: Build
run: go build -v ./...

- name: Run tests
run: go test -v ./...
run: cd backend && go build -v ./...

- name: Install golangci-lint
run: |
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v2.0.0
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v2.6.2

- name: Run golangci-lint
run: golangci-lint run ./...
run: cd backend && golangci-lint run ./...

- name: Run tests
run: cd backend && go test -v ./...
File renamed without changes.
File renamed without changes.
26 changes: 14 additions & 12 deletions .golangci.yml → backend/.golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ linters:
- tagliatelle
- varnamelen
- wsl
- wsl_v5
- noinlineerr
- gochecknoglobals
- mnd
- perfsprint
settings:
gocritic:
enabled-tags:
Expand All @@ -34,13 +39,16 @@ linters:
- opinionated
- performance
- style
disabled-checks:
- whyNoLint # Require explanations for nolint directives
govet:
enable-all: true
misspell:
locale: US
staticcheck:
checks:
- all
- -ST1000
exclusions:
generated: lax
rules:
Expand All @@ -66,15 +74,9 @@ linters:
issues:
max-issues-per-linter: 0
max-same-issues: 0
formatters:
enable:
- gci
- gofmt
- gofumpt
- goimports
exclusions:
generated: lax
paths:
- third_party$
- builtin$
- examples$
exclude-rules:
- linters:
- staticcheck
- linters:
- staticcheck
text: "ST1020:"
File renamed without changes.
11 changes: 9 additions & 2 deletions Makefile → backend/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,15 @@ generate:
test:
@go test -timeout 30s -run ./...

.PHONY: coverage
coverage:
@go test ./... -coverprofile=coverage.out


.PHONY: lint
lint:
@golangci-lint run ./...
@golangci-lint run ./... --fix

.PHONY: fmt
format:
@gofmt -w .
@gofumpt -w .
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,19 @@ import (
"strings"
)

func Base62Decode(shortURL string, salt string) int64 {
func Base62Decode(shortURL, salt string) int64 {
alphabet := getShuffledAlphabet(salt)

var decoded int64

for _, char := range shortURL {
index := strings.IndexRune(alphabet, char)
if index == -1 {
return 0
}

decoded = decoded*base62 + int64(index)
}

return decoded
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ const (

func Base62Encode(id int64, salt string) string {
alphabet := getShuffledAlphabet(salt)

var encoded string

num := id

if num == 0 {
Expand All @@ -24,11 +26,13 @@ func Base62Encode(id int64, salt string) string {
num /= base62
}
}

if len(encoded) < minEncodedLen {
encoded = strings.Repeat(string(alphabet[0]), minEncodedLen-len(encoded)) + encoded
} else if len(encoded) > minEncodedLen {
encoded = encoded[:minEncodedLen]
}

return encoded
}

Expand All @@ -49,5 +53,6 @@ func getShuffledAlphabet(salt string) string {
j := int(hashBytes[i%len(hashBytes)]) % (i + 1)
shuffled[i], shuffled[j] = shuffled[j], shuffled[i]
}

return string(shuffled)
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,18 @@ func Test_Helper_Base62Encode(t *testing.T) {

salt := "salt"
tests := []struct {
id int64
want string
id int64
}{
{1, "wwwE"},
{2, "wwwf"},
{3, "www2"},
{4, "wwwQ"},
{5, "wwwS"},
{6, "wwwV"},
{7, "wwwa"},
{8, "wwwm"},
{9, "wwwD"},
{"wwwE", 1},
{"wwwf", 2},
{"www2", 3},
{"wwwQ", 4},
{"wwwS", 5},
{"wwwV", 6},
{"wwwa", 7},
{"wwwm", 8},
{"wwwD", 9},
}

for _, test := range tests {
Expand Down
2 changes: 1 addition & 1 deletion domain/entities/url.go → backend/domain/entities/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
)

type URL struct {
CreatedAt time.Time
ShortCode string
LongURL string
CreatedAt time.Time
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@ package usecases_test

import (
"context"

"lnk/domain/entities/usecases"
"lnk/extensions/gocqltesting"
"lnk/extensions/redis/mocks"
"lnk/gateways/gocql/repositories"
"testing"

"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
"lnk/domain/entities/usecases"
"lnk/extensions/gocqltesting"
"lnk/extensions/redis/mocks"
"lnk/gateways/gocql/repositories"
)

func Test_UseCase_CreateURL(t *testing.T) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,23 @@ package usecases_test

import (
"context"
"lnk/domain/entities/usecases"
"lnk/extensions/gocqltesting"
"lnk/extensions/redis/mocks"
"lnk/gateways/gocql/repositories"
"testing"

"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
"lnk/domain/entities/usecases"
"lnk/extensions/gocqltesting"
"lnk/extensions/redis/mocks"
"lnk/gateways/gocql/repositories"
)

func Test_UseCase_GetLongURL(t *testing.T) {
t.Parallel()

session, err := gocqltesting.NewDB(t, t.Name())
require.NoError(t, err)

url := "https://www.google.com"

ctx := context.Background()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package usecases_test

import (
gocqltesting "lnk/extensions/gocqltesting"
"lnk/gateways/gocql/migrations"
"log"
"os"
"testing"

gocqltesting "lnk/extensions/gocqltesting"
"lnk/gateways/gocql/migrations"
)

func TestMain(m *testing.M) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package usecases

import (
"errors"
"lnk/extensions/redis"
"lnk/gateways/gocql/repositories"

"go.uber.org/zap"
"lnk/extensions/redis"
"lnk/gateways/gocql/repositories"
)

var ErrURLNotFound = errors.New("URL not found")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,16 @@ import (

"github.com/joho/godotenv"
"github.com/kelseyhightower/envconfig"

"lnk/extensions/logger"
"lnk/extensions/redis"
"lnk/gateways/gocql"
)

type Config struct {
App App
Logger logger.Config
Gocql gocql.Config
Redis redis.Config
Logger logger.Config
}

type App struct {
Expand All @@ -30,12 +29,15 @@ func LoadConfig() (*Config, error) {
if err != nil {
return nil, fmt.Errorf("failed to load .env file: %w", err)
}

config := &Config{}
if err := envconfig.Process("", config); err != nil {
return nil, fmt.Errorf("failed to process app config: %w", err)
}

if err := envconfig.Process("", &config.Gocql); err != nil {
return nil, fmt.Errorf("failed to process gocql config: %w", err)
}

return config, nil
}
Loading
Loading