Skip to content
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
95 changes: 94 additions & 1 deletion cmd/shortener/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,96 @@
package main

func main() {}
import (
"crypto/md5"
"encoding/hex"
"fmt"
"io"
"net/http"
"strings"
)

type Storage struct {
storage map[string]string
}

func (s *Storage) Add(short string, url string) {
s.storage[short] = url
}

func (s *Storage) Get(short string) (string, bool) {
url, ok := s.storage[short]
return url, ok
}

func CreateStorage() *Storage {
return &Storage{storage: make(map[string]string)}
}

var urlStorage = CreateStorage()

func shortenURL(url string) string {
// Решил использовать хэширование и первые символы результата, как короткую форму URL
hash := md5.Sum([]byte(url))
hashString := hex.EncodeToString(hash[:])
shortURL := hashString[:8]
return shortURL
}

func main() {
mux := http.NewServeMux()
mux.HandleFunc(`/`, defineHandler)

err := http.ListenAndServe(`:8080`, mux)

if err != nil {
fmt.Println("Error:", err)
}
}

func defineHandler(res http.ResponseWriter, req *http.Request) {
if req.Method == http.MethodPost && req.URL.Path == "/" {
postHandler(res, req)
} else if req.Method == http.MethodGet && req.URL.Path != "/" {
getHandler(res, req)
} else {
invalidRequestHandler(res, req)
}
}

func postHandler(res http.ResponseWriter, req *http.Request) {
body, err := io.ReadAll(req.Body)
if err != nil {
http.Error(res, "Error reading request body", http.StatusInternalServerError)
return
}
defer req.Body.Close()

url := string(body)
short := shortenURL(url)

urlStorage.Add(short, url)

res.Header().Set("Content-Type", "text/plain")
res.WriteHeader(http.StatusCreated)
res.Write([]byte("http://localhost:8080/" + short))
}

func getHandler(res http.ResponseWriter, req *http.Request) {
id := req.URL.Path[len("/"):]
if id == "" || strings.Contains(id, "/") {
http.Error(res, "Bad Request", http.StatusBadRequest)
return
}

url, ok := urlStorage.Get(id)
if !ok {
http.NotFound(res, req)
return
}
res.Header().Set("Location", url)
res.WriteHeader(http.StatusTemporaryRedirect)
}

func invalidRequestHandler(res http.ResponseWriter, req *http.Request) {
http.Error(res, "Bad Request", http.StatusBadRequest)
}
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/shilin-anton/urlreducer

go 1.21.3

require github.com/gorilla/mux v1.8.1 // indirect