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

Iter9 #9

Open
wants to merge 29 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
6d6a53e
Add Get and Post handlers with simple storage
shilin-anton Feb 6, 2024
99b61eb
Update responses
shilin-anton Feb 7, 2024
dd4b6c8
Fix WriteReader order
shilin-anton Feb 7, 2024
0cddb61
Change project structure and add tests
shilin-anton Feb 9, 2024
7b0b190
Close body in a test
shilin-anton Feb 9, 2024
491b1e7
Move body.close before Result
shilin-anton Feb 9, 2024
c146465
Add defer on body closing
shilin-anton Feb 9, 2024
e4bd004
Add chi usage, Change tests
shilin-anton Feb 10, 2024
86ff933
Remove unnecessary storage creation
shilin-anton Feb 10, 2024
d6a19a0
Add config to handle flags
shilin-anton Feb 11, 2024
8fcb776
FIx run address
shilin-anton Feb 11, 2024
e595f4c
Fix host
shilin-anton Feb 11, 2024
f4296de
Add prefix on save short link
shilin-anton Feb 11, 2024
06c9355
FIx
shilin-anton Feb 11, 2024
53def49
Add ENV params
shilin-anton Feb 12, 2024
809a583
Fixes after review
shilin-anton Mar 19, 2024
b3b87b4
Add logger
shilin-anton Mar 20, 2024
37ec342
Fix linter offences
shilin-anton Mar 20, 2024
68bc72e
Add /shorten json handler and test
shilin-anton Mar 25, 2024
0b79cd2
Fix struct key
shilin-anton Mar 25, 2024
e4d5a71
Fix route
shilin-anton Mar 25, 2024
050e81a
Add gzip middleware
shilin-anton Mar 26, 2024
8597962
Add file-manager to control local file storage
shilin-anton Mar 27, 2024
914a918
Fix linter offences
shilin-anton Mar 27, 2024
ac7ff68
Remove unused types from logger
shilin-anton Apr 1, 2024
8a18246
Replace storage import by interface in fileManager
shilin-anton Apr 1, 2024
4f0ae4b
Temp commit
shilin-anton Apr 1, 2024
156fe72
Refactor code
shilin-anton Apr 1, 2024
0b79f63
More refactor
shilin-anton Apr 2, 2024
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,6 @@ vendor/
# IDEs directories
.idea
.vscode
cmd/.DS_Store
.DS_Store
/tmp
23 changes: 22 additions & 1 deletion cmd/shortener/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,24 @@
package main

func main() {}
import (
"github.com/shilin-anton/urlreducer/internal/app/config"
filemanager "github.com/shilin-anton/urlreducer/internal/app/file-manager"
handler "github.com/shilin-anton/urlreducer/internal/app/handlers"
"github.com/shilin-anton/urlreducer/internal/app/storage"
"github.com/shilin-anton/urlreducer/internal/logger"
"log"
"net/http"
)

func main() {
config.ParseConfig()
err := logger.Initialize(config.LogLevel)
if err != nil {
log.Fatal("Error initializing logger")
}

fl := filemanager.New()
myStorage := storage.New(fl)
myHandler := handler.New(myStorage)
log.Fatal(http.ListenAndServe(config.RunAddr, myHandler))
}
16 changes: 16 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module github.com/shilin-anton/urlreducer

go 1.21.3

require (
github.com/go-chi/chi/v5 v5.0.11
github.com/stretchr/testify v1.8.1
go.uber.org/zap v1.27.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
go.uber.org/multierr v1.10.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
25 changes: 25 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-chi/chi/v5 v5.0.11 h1:BnpYbFZ3T3S1WMpD79r7R5ThWX40TaFB7L31Y8xqSwA=
github.com/go-chi/chi/v5 v5.0.11/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
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=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
go.uber.org/multierr v1.10.0 h1:S0h4aNzvfcFsC3dRF1jLoaov7oRaKqRGC/pUEJ2yvPQ=
go.uber.org/multierr v1.10.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8=
go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
40 changes: 40 additions & 0 deletions internal/app/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package config

import (
"flag"
"os"
)

var RunAddr string
var BaseAddr string
var LogLevel string
var FilePath string

const (
defaultRunURL = "localhost:8080"
defaultBaseURL = "http://localhost:8080"
defaultLogLevel = "info"
defaultFilePath = "/tmp/short-url-db.json"
)

func ParseConfig() {
flag.StringVar(&RunAddr, "a", defaultRunURL, "address and port to run server")
flag.StringVar(&BaseAddr, "b", defaultBaseURL, "base URL before short link")
flag.StringVar(&LogLevel, "l", defaultLogLevel, "log level")
flag.StringVar(&FilePath, "f", defaultFilePath, "file storage path")

flag.Parse()

if envRunAddr := os.Getenv("SERVER_ADDRESS"); envRunAddr != "" {
RunAddr = envRunAddr
}
if envBaseAddr := os.Getenv("BASE_URL"); envBaseAddr != "" {
BaseAddr = envBaseAddr
}
if envLogLevel := os.Getenv("LOG_LEVEL"); envLogLevel != "" {
LogLevel = envLogLevel
}
if envFilePath := os.Getenv("FILE_STORAGE_PATH"); envFilePath != "" {
FilePath = envFilePath
}
}
123 changes: 123 additions & 0 deletions internal/app/file-manager/file-manager.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package filemanager

import (
"bufio"
"encoding/json"
"github.com/shilin-anton/urlreducer/internal/app/config"
"log"
"os"
)

type record struct {
UUID string `json:"uuid"`
ShortURL string `json:"short_url"`
OriginalURL string `json:"original_url"`
}

type ExportedManager struct {
}

type FileWriter struct {
file *os.File
scanner *bufio.Scanner
writer *bufio.Writer
}

type FileReader struct {
file *os.File
scanner *bufio.Scanner
}

func NewWriter() (*FileWriter, error) {
file, err := os.OpenFile(config.FilePath, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
return nil, err
}

return &FileWriter{
file: file,
scanner: bufio.NewScanner(file),
writer: bufio.NewWriter(file),
}, nil
}

func (fw *FileWriter) Close() error {
return fw.file.Close()
}

func NewReader() (*FileReader, error) {
file, err := os.OpenFile(config.FilePath, os.O_RDONLY|os.O_CREATE, 0666)
if err != nil {
return nil, err
}

return &FileReader{
file: file,
scanner: bufio.NewScanner(file),
}, nil
}

func (fr *FileReader) Close() {
fr.file.Close()
}

func New() *ExportedManager {
return &ExportedManager{}
}

func (em *ExportedManager) ReadFromFile(data map[string]string) {
if localStorageDisabled() {
return
}

reader, err := NewReader()
if err != nil {
log.Fatal("Error opening file:", err)
}
defer reader.Close()

for reader.scanner.Scan() {
line := reader.scanner.Bytes()

rec := &record{}
if err := json.Unmarshal(line, &rec); err != nil {
log.Fatal("Error decoding data from file:", err)
}
data[rec.ShortURL] = rec.OriginalURL
}

if err := reader.scanner.Err(); err != nil {
log.Fatal("Error scanning from file:", err)
}
}

func (em *ExportedManager) AddRecord(short string, url string, uuid string) error {
if localStorageDisabled() {
return nil
}

writer, err := NewWriter()
if err != nil {
return err
}
defer writer.Close()

newRecord := record{
UUID: uuid,
ShortURL: short,
OriginalURL: url,
}
recordJSON, err := json.Marshal(newRecord)
if err != nil {
return err
}
if _, err := writer.file.WriteString(string(recordJSON) + "\n"); err != nil {
return err
}

return nil
}

func localStorageDisabled() bool {
return config.FilePath == ""
}
71 changes: 71 additions & 0 deletions internal/app/gzip/gzip.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package gzip

import (
"compress/gzip"
"io"
"net/http"
)

// compressWriter реализует интерфейс http.ResponseWriter и позволяет прозрачно для сервера
// сжимать передаваемые данные и выставлять правильные HTTP-заголовки
type compressWriter struct {
w http.ResponseWriter
zw *gzip.Writer
}

func NewCompressWriter(w http.ResponseWriter) *compressWriter {
return &compressWriter{
w: w,
zw: gzip.NewWriter(w),
}
}

func (c *compressWriter) Header() http.Header {
return c.w.Header()
}

func (c *compressWriter) Write(p []byte) (int, error) {
return c.zw.Write(p)
}

func (c *compressWriter) WriteHeader(statusCode int) {
if statusCode < 300 {
c.w.Header().Set("Content-Encoding", "gzip")
}
c.w.WriteHeader(statusCode)
}

// Close закрывает gzip.Writer и досылает все данные из буфера.
func (c *compressWriter) Close() error {
return c.zw.Close()
}

// compressReader реализует интерфейс io.ReadCloser и позволяет прозрачно для сервера
// декомпрессировать получаемые от клиента данные
type compressReader struct {
r io.ReadCloser
zr *gzip.Reader
}

func NewCompressReader(r io.ReadCloser) (*compressReader, error) {
zr, err := gzip.NewReader(r)
if err != nil {
return nil, err
}

return &compressReader{
r: r,
zr: zr,
}, nil
}

func (c compressReader) Read(p []byte) (n int, err error) {
return c.zr.Read(p)
}

func (c *compressReader) Close() error {
if err := c.r.Close(); err != nil {
return err
}
return c.zr.Close()
}
Loading
Loading