feat(links): create basic link shortening algo
This commit is contained in:
parent
fc46164556
commit
81e72cedfa
9 changed files with 144 additions and 23 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
|
@ -25,3 +25,5 @@ go.work.sum
|
||||||
# env file
|
# env file
|
||||||
.env
|
.env
|
||||||
|
|
||||||
|
|
||||||
|
*.db
|
||||||
|
|
|
||||||
|
|
@ -25,8 +25,9 @@ func createMux(db *database.Database) http.Handler {
|
||||||
r := http.NewServeMux()
|
r := http.NewServeMux()
|
||||||
|
|
||||||
linkHandler := handlers.NewLinkHandler(db)
|
linkHandler := handlers.NewLinkHandler(db)
|
||||||
r.HandleFunc("GET /api/links", linkHandler.ListLinks)
|
|
||||||
|
r.HandleFunc("POST /api/link", linkHandler.CreateLink)
|
||||||
r.HandleFunc("GET /{id}", linkHandler.GetUrl)
|
r.HandleFunc("GET /{id}", linkHandler.GetUrl)
|
||||||
|
|
||||||
return r
|
return handlers.LogMiddleware(r)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,19 +0,0 @@
|
||||||
package app
|
|
||||||
|
|
||||||
import (
|
|
||||||
"maot-shortner/internal/handlers"
|
|
||||||
"net/http"
|
|
||||||
)
|
|
||||||
|
|
||||||
type HandlerList struct {
|
|
||||||
*handlers.LinkHandler
|
|
||||||
}
|
|
||||||
|
|
||||||
func CreateMux(handlers HandlerList) *http.ServeMux {
|
|
||||||
r := http.NewServeMux()
|
|
||||||
|
|
||||||
r.HandleFunc("/links", handlers.LinkHandler.ListLinks)
|
|
||||||
r.HandleFunc("/", handlers.LinkHandler.Handle)
|
|
||||||
|
|
||||||
return r
|
|
||||||
}
|
|
||||||
|
|
@ -14,3 +14,21 @@ func (db *Database) GetLinks(ctx context.Context) ([]*domain.Link, error) {
|
||||||
|
|
||||||
return links, nil
|
return links, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (db *Database) CreateLink(ctx context.Context, link *domain.Link) (*domain.Link, error) {
|
||||||
|
if err := db.WithContext(ctx).Create(link).Error; err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return link, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (db *Database) GetLinkByShortCode(ctx context.Context, shortCode string) (*domain.Link, error) {
|
||||||
|
link := &domain.Link{}
|
||||||
|
|
||||||
|
if err := db.WithContext(ctx).Where("short_url = ?", shortCode).First(link).Error; err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return link, nil
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@ import (
|
||||||
|
|
||||||
type Link struct {
|
type Link struct {
|
||||||
ID int64 `json:"id"`
|
ID int64 `json:"id"`
|
||||||
ShortURL string `json:"short_url"`
|
ShortURL string `json:"short_url" gorm:"uniqueIndex"`
|
||||||
LongURL string `json:"long_url"`
|
LongURL string `json:"long_url"`
|
||||||
CreatedAt time.Time `json:"created_at"`
|
CreatedAt time.Time `json:"created_at"`
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -44,8 +44,37 @@ func (h *LinkHandler) ListLinks(w http.ResponseWriter, r *http.Request) {
|
||||||
json.NewEncoder(w).Encode(linkList)
|
json.NewEncoder(w).Encode(linkList)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (h *LinkHandler) CreateLink(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
|
||||||
|
var linkRequest struct {
|
||||||
|
URL string `json:"url"`
|
||||||
|
RedirectCode *string `json:"redirect_code,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := json.NewDecoder(r.Body).Decode(&linkRequest); err != nil {
|
||||||
|
http.Error(w, fmt.Sprintf("Failed to decode request body: %v", err), http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
link, err := h.linkService.CreateLink(r.Context(), linkRequest.URL, linkRequest.RedirectCode)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, fmt.Sprintf("Failed to create link: %v", err), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
json.NewEncoder(w).Encode(link)
|
||||||
|
}
|
||||||
|
|
||||||
func (h *LinkHandler) GetUrl(w http.ResponseWriter, r *http.Request) {
|
func (h *LinkHandler) GetUrl(w http.ResponseWriter, r *http.Request) {
|
||||||
currentCode := r.PathValue("id")
|
currentCode := r.PathValue("id")
|
||||||
|
|
||||||
json.NewEncoder(w).Encode(&Response{Message: currentCode})
|
link, err := h.linkService.GetLink(r.Context(), currentCode)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, fmt.Sprintf("Failed to get link: %v", err), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
http.Redirect(w, r, link.LongURL, http.StatusFound)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
46
internal/handlers/log_middleware.go
Normal file
46
internal/handlers/log_middleware.go
Normal file
|
|
@ -0,0 +1,46 @@
|
||||||
|
package handlers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type statusRecorder struct {
|
||||||
|
http.ResponseWriter
|
||||||
|
statusCode int
|
||||||
|
}
|
||||||
|
|
||||||
|
func (sr *statusRecorder) WriteHeader(code int) {
|
||||||
|
sr.statusCode = code
|
||||||
|
sr.ResponseWriter.WriteHeader(code)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (sr *statusRecorder) Write(b []byte) (int, error) {
|
||||||
|
if sr.statusCode == 0 {
|
||||||
|
sr.statusCode = http.StatusOK
|
||||||
|
}
|
||||||
|
|
||||||
|
return sr.ResponseWriter.Write(b)
|
||||||
|
}
|
||||||
|
|
||||||
|
func LogMiddleware(h http.Handler) http.Handler {
|
||||||
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
start := time.Now()
|
||||||
|
rec := &statusRecorder{ResponseWriter: w}
|
||||||
|
|
||||||
|
h.ServeHTTP(rec, r)
|
||||||
|
duration := time.Since(start)
|
||||||
|
|
||||||
|
statusCode := rec.statusCode
|
||||||
|
|
||||||
|
log.Printf("%s %s %s from %s - %d %s in %s\n",
|
||||||
|
r.Method,
|
||||||
|
r.URL.Path,
|
||||||
|
r.Proto,
|
||||||
|
r.RemoteAddr,
|
||||||
|
statusCode,
|
||||||
|
http.StatusText(statusCode),
|
||||||
|
duration.String())
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
@ -7,4 +7,6 @@ import (
|
||||||
|
|
||||||
type LinkRepository interface {
|
type LinkRepository interface {
|
||||||
GetLinks(ctx context.Context) ([]*domain.Link, error)
|
GetLinks(ctx context.Context) ([]*domain.Link, error)
|
||||||
|
CreateLink(ctx context.Context, link *domain.Link) (*domain.Link, error)
|
||||||
|
GetLinkByShortCode(ctx context.Context, shortCode string) (*domain.Link, error)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,8 +2,13 @@ package services
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"crypto/rand"
|
||||||
|
"encoding/base64"
|
||||||
|
"errors"
|
||||||
"maot-shortner/internal/domain"
|
"maot-shortner/internal/domain"
|
||||||
"maot-shortner/internal/repositories"
|
"maot-shortner/internal/repositories"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type LinkService struct {
|
type LinkService struct {
|
||||||
|
|
@ -19,3 +24,40 @@ func NewLinkService(repo repositories.LinkRepository) *LinkService {
|
||||||
func (s *LinkService) GetLinks(ctx context.Context) ([]*domain.Link, error) {
|
func (s *LinkService) GetLinks(ctx context.Context) ([]*domain.Link, error) {
|
||||||
return s.repo.GetLinks(ctx)
|
return s.repo.GetLinks(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *LinkService) CreateLink(ctx context.Context, longUrl string, redirectCode *string) (*domain.Link, error) {
|
||||||
|
if !strings.Contains(longUrl, "http://") && !strings.Contains(longUrl, "https://") {
|
||||||
|
return nil, errors.New("Invalid URL")
|
||||||
|
}
|
||||||
|
|
||||||
|
var shortUrl string
|
||||||
|
|
||||||
|
if redirectCode != nil {
|
||||||
|
shortUrl = *redirectCode
|
||||||
|
} else {
|
||||||
|
shortUrl = generateShortCode(5)
|
||||||
|
}
|
||||||
|
|
||||||
|
link := &domain.Link{
|
||||||
|
LongURL: longUrl,
|
||||||
|
ShortURL: shortUrl,
|
||||||
|
CreatedAt: time.Now(),
|
||||||
|
}
|
||||||
|
|
||||||
|
return s.repo.CreateLink(ctx, link)
|
||||||
|
}
|
||||||
|
|
||||||
|
func generateShortCode(length int) string {
|
||||||
|
b := make([]byte, length)
|
||||||
|
rand.Read(b)
|
||||||
|
if _, err := rand.Read(b); err != nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
return base64.RawURLEncoding.EncodeToString(b)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *LinkService) GetLink(ctx context.Context, shortUrl string) (*domain.Link, error) {
|
||||||
|
|
||||||
|
return s.repo.GetLinkByShortCode(ctx, shortUrl)
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue