Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvements + dockerfile #4

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ jobs:
go-version: 1.16
- name: Build
run: go build -v ./cmd/server/main.go
# - name: Test
# run: go test -v ./...
- name: Test
run: go test -v ./...
...
15 changes: 5 additions & 10 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,37 +1,32 @@
# настройки запуска
run:
# значение конкурентности устанавливаем по количеству CPU
concurrency: 4
concurrency: 8
# анализ не должен превышать
timeout: 1m
timeout: 2m
# выводить "exit-code" при наличии хотя бы одного сообщения
issues-exit-code: 1
# не включать анализ тестов
tests: false
# Пропускать папки вендора, сторонних зависимостей и т.д.
# vendor$, third_party$, testdata$, examples$, Godeps$, builtin$
skip-dirs-use-default: true
# Пропускать файлы, заканчивающиеся на .bad.go
skip-files:
- ".*\\.bad\\.go$"
# список линтеров
linters:
disable-all: true
enable:
- gofmt # форматирование кода (пробелы, отступы, скобки)
#- goimports # обновление списка imports
- goimports # обновление списка imports
- govet # общий анализ возможных багов
- goconst # нахождение строк, которые следует вынести в константы
- funlen # детектирование слишком крупных функций
- bodyclose # проверка на незакрытые body после чтения тела ответа
- errcheck # проверка на обработку всех ошибок
- deadcode # детектирование не использованного кода
- exportloopref # детектирование экспорта указателя на переменную внутри цикла
# настройки отдельных линтеров
linters-settings:
#goimports:
goimports:
# указываем префикс локальных импортов, они будут группироваться
#local-prefixes: path/to/module
local-prefixes: path/to/module
govet:
# проверка на использование переменных «в тени»
check-shadowing: true
Expand Down
15 changes: 15 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
FROM golang:1.16 AS builder

RUN mkdir -p /app
ADD . /app
WORKDIR /app

RUN GOOS=linux GOARCH=amd64 CGO_ENABLED=0 \
go build -o /web-shortener ./cmd/server/main.go

### we are using another container to use for application
FROM scratch
#RUN mkdir -p /app
COPY --from=builder /app /app
WORKDIR /app
CMD ["/web-shortener","-fileConfig=./config.yaml"]
43 changes: 29 additions & 14 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"context"
"fmt"
"log"
"os"
"os/signal"
"syscall"
Expand All @@ -13,47 +12,63 @@ import (
"github.com/1r0npipe/shortener-web-links/internal/handler"
"github.com/1r0npipe/shortener-web-links/internal/params"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
)

func main() {
logger, err := zap.NewProduction()
if err != nil {
panic(err)
}
defer func() {
err := logger.Sync()
if err != nil {
panic(err)
}
}()

slog := logger.Sugar()
slog.Info("Initializin the configuration ...")

flags, err := params.Init()
if err != nil {
fmt.Println(params.HelpMessage)
log.Fatal("Can't read the flags properly")
slog.Fatal("Can't read the flags properly")
}
fmt.Println(flags)
config, err := config.ReadNewConfig(flags.FileConfig)
configData, err := config.ReadNewConfig(flags.FileConfig)
if err != nil {
log.Fatal("Can't read config file")
slog.Fatal("Can't read config file")
}
// override the port if provided at the CLI
if flags.Port != "" {
config.Server.Port = flags.Port
configData.Server.Port = flags.Port
}

if err != nil {
slog.Error(err)
}
osSig := make(chan os.Signal, 1)
signal.Notify(osSig,
syscall.SIGINT,
syscall.SIGTERM)

//router := gin.Default()
router := gin.New()

router.GET("/healthz", handler.CheckHealth)
router.POST("/v1/link", handler.GenerateNewLink)
// TODO:
router.GET("/v1/:shortUrl", handler.RedirectByShortUrl)
router.GET("/v1/stat/:{id}", handler.GetStatById)
// TODO:
router.GET("/v1/stat/:shortUrl", handler.GetStatById)
go func() {
err := router.Run(":" + config.Server.Port)
err := router.Run(":" + configData.Server.Port)
if err != nil {
log.Fatal("can't start server")
slog.Fatal("can't start server")
}
}()
<-osSig
log.Printf("shutting down ...\n")
_, cancelFunc := context.WithTimeout(context.Background(), time.Duration(config.Server.Timeout)*time.Second)
slog.Info("shutting down ...\n")
_, cancelFunc := context.WithTimeout(context.Background(), time.Duration(configData.Server.Timeout)*time.Second)
defer cancelFunc()
log.Printf("the service has been down...")
slog.Info("the service has been down...")

}
7 changes: 3 additions & 4 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ server:
port: 8080
exprire: 60
db:
address: localhost
port: 6137
username: redis
password: redis
address: 'localhost:6379'
username: ''
password: 'RedisPassword'
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.16
require (
github.com/gin-gonic/gin v1.7.2 // indirect
github.com/go-playground/validator/v10 v10.6.1 // indirect
github.com/go-redis/redis v6.15.9+incompatible
github.com/golang/protobuf v1.5.2 // indirect
github.com/itchyny/base58-go v0.1.0
github.com/json-iterator/go v1.1.11 // indirect
Expand All @@ -14,6 +15,7 @@ require (
github.com/modern-go/reflect2 v1.0.1 // indirect
github.com/stretchr/testify v1.7.0
github.com/ugorji/go v1.2.6 // indirect
go.uber.org/zap v1.17.0
golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a // indirect
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 // indirect
golang.org/x/text v0.3.6 // indirect
Expand Down
9 changes: 9 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+
github.com/go-playground/validator/v10 v10.4.1/go.mod h1:nlOn6nFhuKACm19sB/8EGNn9GlaMV7XkbRSipzJ0Ii4=
github.com/go-playground/validator/v10 v10.6.1 h1:W6TRDXt4WcWp4c4nf/G+6BkGdhiIo0k417gfr+V6u4I=
github.com/go-playground/validator/v10 v10.6.1/go.mod h1:xm76BBt941f7yWdGnI2DVPFFg1UK3YY04qifoXU3lOk=
github.com/go-redis/redis v6.15.9+incompatible h1:K0pv1D7EQUjfyoMql+r/jZqCLizCGKFlFgcHWWmHQjg=
github.com/go-redis/redis v6.15.9+incompatible/go.mod h1:NAIEuMOZ/fxfXJIrKDQDz8wamY7mA7PouImQ2Jvg6kA=
github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw=
Expand All @@ -37,6 +39,7 @@ github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJ
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/modern-go/reflect2 v1.0.1 h1:9f412s+6RmYXLWZSEzVVgPGK7C2PphHj5RJrvfx9AWI=
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand All @@ -51,6 +54,12 @@ github.com/ugorji/go v1.2.6/go.mod h1:anCg0y61KIhDlPZmnH+so+RQbysYVyDko0IMgJv0Nn
github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY=
github.com/ugorji/go/codec v1.2.6 h1:7kbGefxLoDBuYXOms4yD7223OpNMMPNPZxXk5TvFcyQ=
github.com/ugorji/go/codec v1.2.6/go.mod h1:V6TCNZ4PHqoHGFZuSG1W8nrCzzdgA2DozYxWFFpvxTw=
go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw=
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/multierr v1.6.0 h1:y6IPFStTAIT5Ytl7/XYmHvzXQ7S3g/IeZW9hyZ5thw4=
go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU=
go.uber.org/zap v1.17.0 h1:MTjgFu6ZLKvY6Pvaqk97GlxNBuMpV4Hy/3P6tRGlI2U=
go.uber.org/zap v1.17.0/go.mod h1:MXVU+bhUf/A7Xi2HNOnopQOrmycQ5Ih87HtOu4q5SSo=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a h1:kr2P4QFmQr29mSLA43kwrOcgcReGTfbE9N577tCTuBc=
Expand Down
10 changes: 4 additions & 6 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
)

var (
ErrFileRead = errors.New("can't read config file")
ErrDecodeYAML = errors.New("can't decode yaml file")
)

Expand All @@ -17,18 +16,17 @@ type Config struct {
Timeout int `yaml:"timeout"`
} `yaml:"server"`
DB struct {
Address string `yaml:"dbAddress"`
Port string `yaml:"dbPort"`
Username string `yaml:"dbUsername"`
Password string `yaml:"dbpasswor"`
Address string `yaml:"address"`
Username string `yaml:"username"`
Password string `yaml:"password"`
} `yaml:"db"`
}

func ReadNewConfig(configPath string) (*Config, error) {
config := &Config{}
file, err := ioutil.ReadFile(configPath)
if err != nil {
return nil, ErrFileRead
return nil, err
}
if err := yaml.Unmarshal(file, config); err != nil {
return nil, ErrDecodeYAML
Expand Down
4 changes: 2 additions & 2 deletions internal/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ func base58Enc(input []byte) string {
return string(encoded)
}

func GenerateShortUrl(input string) (string, error) {
urlHashB := applySha256(input)
func GenerateShortUrl(input string, userID string) (string, error) {
urlHashB := applySha256(input + userID)
generatedLine := new(big.Int).SetBytes(urlHashB).Uint64()
result := base58Enc([]byte(fmt.Sprintf("%d", generatedLine)))
if result == "error" {
Expand Down
20 changes: 12 additions & 8 deletions internal/generator/generator_test.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
package generator

import (
"testing"
"github.com/stretchr/testify/assert"
"testing"
)

func TestGenerateShortUrl(t *testing.T) {
tests := []struct {
longLink string
userID string
shortLink string
}{
{longLink: "www.yandex.ru/super-puper-long-link",
shortLink: "QJNdBNdAjYS"},
{longLink: "www.revenue.ie/this-is%fully-fake-link%no-sense-to-use-it",
shortLink: "MJZ9gmvb8pS"},
{longLink: "www.google.com/i-dont-know%what%to%write%right-here-a?",
shortLink: "ZvrgR7LPPrP"},
{longLink: "www.yandex.ru/super-puper-long-link",
userID: "1123-1233-1233",
shortLink: "G2qypjzSMoa"},
{longLink: "www.revenue.ie/this-is%fully-fake-link%no-sense-to-use-it",
userID: "1123-1233-1233",
shortLink: "UfBNP3mcjX1"},
{longLink: "www.google.com/i-dont-know%what%to%write%right-here-a?",
userID: "1123-1233-1233",
shortLink: "TA6eiUvMY5b"},
}
for _, tt := range tests {
shortLink, _ := GenerateShortUrl(tt.longLink)
shortLink, _ := GenerateShortUrl(tt.longLink, tt.userID)
assert.Equal(t, shortLink, tt.shortLink)
}
}
Loading