Put project modules under pkg directory

This commit is contained in:
Andrey Chervyakov 2021-04-12 17:17:09 +06:00
parent f4684be37d
commit e1516e450b
13 changed files with 7 additions and 7 deletions

50
pkg/cgnolink/link/dto.go Normal file
View file

@ -0,0 +1,50 @@
package link
import (
"net/url"
"time"
)
type CreationModel struct {
Id string `json:"id"`
Name string `json:"name"`
RedirectURL string `json:"redirectUrl"`
Password string `json:"password"`
}
type ResourceModel struct {
Id string `json:"id"`
Name string `json:"name"`
RedirectURL string `json:"redirectUrl"`
CreationTime int64 `json:"creationTime"`
}
type UpdateModel struct {
Name string `json:"name,omitempty"`
RedirectURL string `json:"redirectUrl,omitempty"`
Password string `json:"password"`
}
func (m *CreationModel) MapModelToEntity() (*Link, error) {
u, err := url.Parse(m.RedirectURL)
if err != nil {
return nil, err
}
return &Link{
Id: m.Id,
Name: m.Name,
RedirectURL: *u,
Password: m.Password,
CreationTime: time.Now().UTC(),
}, nil
}
func MapEntityToModel(entity *Link) ResourceModel {
return ResourceModel{
Id: entity.Id,
Name: entity.Name,
RedirectURL: entity.RedirectURL.String(),
CreationTime: entity.CreationTime.Unix(),
}
}

View file

@ -0,0 +1,16 @@
package link
import (
"net/url"
"time"
)
type Link struct {
Id string
Name string
RedirectURL url.URL
Password string
CreationTime time.Time
}
type Links = []*Link

View file

@ -0,0 +1,151 @@
package link
import (
"encoding/json"
"github.com/jackc/pgx/v4/pgxpool"
"github.com/labstack/echo/v4"
"net/http"
"net/url"
"strconv"
)
func redirectHandler(ctx echo.Context, serv Service) error {
linkId := ctx.Param("id")
linkPassword := ctx.QueryParam("password")
redirectUrl, err := serv.AccessLink(linkId, linkPassword)
if err != nil {
return err
}
return ctx.Redirect(http.StatusSeeOther, redirectUrl.String())
}
func creationHandler(ctx echo.Context, serv Service) error {
var model CreationModel
if err := json.NewDecoder(ctx.Request().Body).Decode(&model); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Invalid data format.")
}
entity, err := model.MapModelToEntity()
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Invalid URL.")
}
if err = serv.Create(entity); err != nil {
return err
}
return ctx.NoContent(http.StatusCreated)
}
func retrievalByIdHandler(ctx echo.Context, serv Service) error {
linkId := ctx.Param("id")
l, err := serv.GetById(linkId)
if err != nil {
return err
}
return ctx.JSON(http.StatusOK, MapEntityToModel(l))
}
func allRetrievalHandler(ctx echo.Context, serv Service) error {
limit := 20
if v := ctx.QueryParam("limit"); v != "" {
num, err := strconv.Atoi(v)
if err != nil || num < 0 {
return echo.NewHTTPError(http.StatusBadRequest, "Invalid limit value.")
}
limit = num
}
offset := 0
if v := ctx.QueryParam("offset"); v != "" {
num, err := strconv.Atoi(v)
if err != nil || num < 0 {
return echo.NewHTTPError(http.StatusBadRequest, "Invalid offset value.")
}
offset = num
}
links, err := serv.GetAll(limit, offset)
if err != nil {
return err
}
models := make([]ResourceModel, len(links))
for i, v := range links {
models[i] = MapEntityToModel(v)
}
return ctx.JSON(http.StatusOK, models)
}
func updateHandler(ctx echo.Context, serv Service) error {
linkId := ctx.Param("id")
var model UpdateModel
if err := json.NewDecoder(ctx.Request().Body).Decode(&model); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Invalid data format.")
}
var parsedUrl *url.URL
parsedUrl, err := url.Parse(model.RedirectURL)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Invalid URL value.")
}
if err := serv.UpdateById(linkId, struct {
Name string
Password string
RedirectURL *url.URL
}{Name: model.Name, Password: model.Password, RedirectURL: parsedUrl}); err != nil {
return err
}
return ctx.NoContent(http.StatusOK)
}
func removalHandler(ctx echo.Context, serv Service) error {
linkId := ctx.Param("id")
if err := serv.DeleteById(linkId); err != nil {
return err
}
return ctx.NoContent(http.StatusNoContent)
}
func AddHandlers(server *echo.Echo, pool *pgxpool.Pool) {
serv := NewService(NewRepository(pool))
linksGroup := server.Group("/links")
exactLinkGroup := linksGroup.Group("/:id")
linksGroup.POST("", func(ctx echo.Context) error {
return creationHandler(ctx, serv)
})
linksGroup.GET("", func(ctx echo.Context) error {
return allRetrievalHandler(ctx, serv)
})
exactLinkGroup.GET("", func(ctx echo.Context) error {
return retrievalByIdHandler(ctx, serv)
})
exactLinkGroup.PATCH("", func(ctx echo.Context) error {
return updateHandler(ctx, serv)
})
exactLinkGroup.DELETE("", func(ctx echo.Context) error {
return removalHandler(ctx, serv)
})
server.GET("/:id", func(ctx echo.Context) error {
return redirectHandler(ctx, serv)
})
}

View file

@ -0,0 +1,182 @@
package link
import (
"cgnolink/pkg/cgnolink/database"
"context"
"errors"
"github.com/jackc/pgtype"
"github.com/jackc/pgx/v4"
"github.com/jackc/pgx/v4/pgxpool"
"net/url"
"time"
)
const defaultContextTimeout = 10 * time.Second
type Repository interface {
Save(link *Link) error
FindById(id string) (*Link, error)
GetAll(limit int, offset int) (Links, error)
Update(link *Link) error
DeleteById(id string) error
}
type PgRepository struct {
pool *pgxpool.Pool
}
func NewRepository(pool *pgxpool.Pool) Repository {
return &PgRepository{pool: pool}
}
func (r *PgRepository) Save(link *Link) error {
ctx, cancel := context.WithTimeout(context.Background(), defaultContextTimeout)
defer cancel()
sql := `
INSERT INTO links (id, name, redirect_url, password, creation_time)
VALUES ($1, $2, $3, $4, $5::timestamp)
`
database.LogPoolState(r.pool, "Saving link")
_, err := r.pool.Exec(ctx, sql, link.Id, link.Name, link.RedirectURL.String(), link.Password, link.CreationTime.Format("2006-01-02 15:04:05"))
if err != nil {
return err
}
return nil
}
func (r *PgRepository) FindById(id string) (*Link, error) {
ctx, cancel := context.WithTimeout(context.Background(), defaultContextTimeout)
defer cancel()
sql := `
SELECT id, name, redirect_url, password, creation_time
FROM links
WHERE id = $1
`
database.LogPoolState(r.pool, "Finding link by ID")
entity, err := mapRowToEntity(r.pool.QueryRow(ctx, sql, id))
if err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return nil, nil
} else {
return nil, err
}
}
return entity, nil
}
func (r *PgRepository) GetAll(limit int, offset int) (Links, error) {
if limit < 0 {
return nil, errors.New("limit can't be negative")
}
if offset < 0 {
return nil, errors.New("offset can't be negative")
}
ctx, cancel := context.WithTimeout(context.Background(), defaultContextTimeout)
defer cancel()
sql := `
SELECT id, name, redirect_url, password, creation_time
FROM links
LIMIT $1
OFFSET $2
`
database.LogPoolState(r.pool, "Getting all links")
rows, err := r.pool.Query(ctx, sql, limit, offset)
if err != nil {
return nil, err
}
defer rows.Close()
links := make(Links, 0)
for rows.Next() {
link, err := mapRowToEntity(rows)
if err != nil {
return nil, err
}
links = append(links, link)
}
if err = rows.Err(); err != nil {
return nil, err
}
return links, nil
}
func (r *PgRepository) Update(link *Link) error {
if link.Id == "" {
return errors.New("link ID must not be empty")
}
ctx, cancel := context.WithTimeout(context.Background(), defaultContextTimeout)
defer cancel()
sql := `
UPDATE links
SET name = $1, redirect_url = $2, password = $3
WHERE id = $4
`
database.LogPoolState(r.pool, "Updating link")
_, err := r.pool.Exec(ctx, sql, link.Name, link.RedirectURL.String(), link.Password, link.Id)
if err != nil {
return err
}
return nil
}
func (r *PgRepository) DeleteById(id string) error {
ctx, cancel := context.WithTimeout(context.Background(), defaultContextTimeout)
defer cancel()
sql := `
DELETE FROM links WHERE id = $1
`
database.LogPoolState(r.pool, "Deleting link")
_, err := r.pool.Exec(ctx, sql, id)
if err != nil {
return err
}
return nil
}
func mapRowToEntity(r interface{}) (*Link, error) {
var entity Link
var urlStr string
var t pgtype.Timestamp
switch v := r.(type) {
case pgx.Row:
if err := v.Scan(&entity.Id, &entity.Name, &urlStr, &entity.Password, &t); err != nil {
return nil, err
}
case pgx.Rows:
if err := v.Scan(&entity.Id, &entity.Name, &urlStr, &entity.Password, &t); err != nil {
return nil, err
}
default:
return nil, errors.New("unsupported type")
}
u, err := url.Parse(urlStr)
if err != nil {
return nil, err
}
entity.RedirectURL = *u
entity.CreationTime = t.Time
return &entity, nil
}

View file

@ -0,0 +1,171 @@
package link
import (
apperrors "cgnolink/pkg/cgnolink/errors"
"github.com/patrickmn/go-cache"
"golang.org/x/crypto/bcrypt"
"net/url"
)
var linkNotFoundError = apperrors.NotFoundError{Message: "Link with given ID was not found."}
type Service interface {
Create(link *Link) error
GetById(id string) (*Link, error)
AccessLink(id string, password string) (*url.URL, error)
GetAll(limit int, offset int) (Links, error)
UpdateById(
id string,
data struct {
Name string
Password string
RedirectURL *url.URL
},
) error
DeleteById(id string) error
}
type PgService struct {
rep Repository
cache *cache.Cache
}
func (service *PgService) AccessLink(id string, password string) (*url.URL, error) {
link, err := service.GetById(id)
if err != nil {
return nil, err
}
if link.Password != "" {
if password == "" || bcrypt.CompareHashAndPassword([]byte(link.Password), []byte(password)) != nil {
return nil, linkNotFoundError
}
}
return &link.RedirectURL, nil
}
func NewService(rep Repository) Service {
return &PgService{
rep: rep,
cache: cache.New(cache.NoExpiration, 0),
}
}
func (service *PgService) Create(link *Link) error {
existingLink, err := service.rep.FindById(link.Id)
if err != nil {
return apperrors.UnknownError{Err: err}
}
if existingLink != nil {
return apperrors.AlreadyExistsError{Message: "Link with given ID already exists."}
}
if link.Password != "" {
hashedPassword, err := HashPassword(link.Password)
if err != nil {
return err
}
link.Password = hashedPassword
}
if err = service.rep.Save(link); err != nil {
return apperrors.UnknownError{Err: err}
}
return nil
}
func (service *PgService) GetById(id string) (*Link, error) {
if v, found := service.cache.Get(id); found {
if link, ok := v.(*Link); ok {
return link, nil
}
}
link, err := service.rep.FindById(id)
if err != nil {
return nil, apperrors.UnknownError{Err: err}
}
if link == nil {
return nil, linkNotFoundError
}
service.cache.Set(id, link, cache.DefaultExpiration)
return link, nil
}
func (service *PgService) GetAll(limit int, offset int) (Links, error) {
links, err := service.rep.GetAll(limit, offset)
if err != nil {
return nil, apperrors.UnknownError{Err: err}
}
return links, nil
}
func (service *PgService) UpdateById(
id string,
data struct {
Name string
Password string
RedirectURL *url.URL
},
) error {
link, err := service.GetById(id)
if err != nil {
return err
}
hasChanges := false
switch {
case data.Name != "":
link.Name = data.Name
hasChanges = true
case data.RedirectURL != nil:
link.RedirectURL = *data.RedirectURL
hasChanges = true
case data.Password != "":
hashedPw, err := HashPassword(data.Password)
if err != nil {
return err
}
link.Password = hashedPw
hasChanges = true
}
if hasChanges {
if err := service.rep.Update(link); err != nil {
return apperrors.UnknownError{Err: err}
}
service.cache.Delete(link.Id)
}
return nil
}
func (service *PgService) DeleteById(id string) error {
if err := service.rep.DeleteById(id); err != nil {
return apperrors.UnknownError{Err: err}
}
service.cache.Delete(id)
return nil
}
func HashPassword(password string) (string, error) {
hashedPw, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
if err != nil {
return "", apperrors.UnknownError{Err: err}
}
return string(hashedPw), nil
}