-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added nginx CORS Removed pgdb from handlers
- Loading branch information
1 parent
2900451
commit d175042
Showing
19 changed files
with
5,227 additions
and
3,810 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
type: object | ||
required: | ||
- original_url | ||
properties: | ||
original_url: | ||
type: string | ||
example: "https://example.com/very/long/url" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
type: object | ||
properties: | ||
short_code: | ||
type: string | ||
example: "abc123" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
type: object | ||
properties: | ||
error: | ||
type: object | ||
properties: | ||
code: | ||
type: string | ||
example: "404" | ||
message: | ||
type: string | ||
example: "NOT_FOUND" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
type: object | ||
properties: | ||
error: | ||
type: object | ||
properties: | ||
code: | ||
type: string | ||
example: "500" | ||
message: | ||
type: string | ||
example: "INTERNAL_SERVER_ERROR" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
type: object | ||
properties: | ||
original_url: | ||
type: string | ||
example: "https://example.com/very/long/url" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
type: object | ||
properties: | ||
id: | ||
type: integer | ||
format: int64 | ||
example: 1 | ||
original_url: | ||
type: string | ||
example: "https://example.com/very/long/url" | ||
short_code: | ||
type: string | ||
example: "abc123" | ||
created_at: | ||
type: string | ||
format: date-time | ||
clicks: | ||
type: integer | ||
example: 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
get: | ||
summary: Get original URL | ||
tags: | ||
- ShortLinks | ||
parameters: | ||
- in: path | ||
name: shortCode | ||
required: true | ||
schema: | ||
type: string | ||
example: "abc123" | ||
description: Short code of the URL to retrieve the original URL. | ||
responses: | ||
"200": | ||
description: Successful operation | ||
content: | ||
application/json: | ||
schema: | ||
$ref: "#/components/schemas/GetOriginalURLResponse" | ||
"404": | ||
description: Short link not found | ||
content: | ||
application/json: | ||
schema: | ||
$ref: "#/components/schemas/Error404" | ||
"500": | ||
description: Internal server error | ||
content: | ||
application/json: | ||
schema: | ||
$ref: "#/components/schemas/Error500" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
post: | ||
summary: Create a short link | ||
tags: | ||
- ShortLinks | ||
requestBody: | ||
required: true | ||
content: | ||
application/json: | ||
schema: | ||
$ref: "#/components/schemas/CreateShortLinkRequest" | ||
responses: | ||
"200": | ||
description: Successful operation | ||
content: | ||
application/json: | ||
schema: | ||
$ref: "#/components/schemas/CreateShortLinkResponse" | ||
"404": | ||
description: Bad request | ||
content: | ||
application/json: | ||
schema: | ||
$ref: "#/components/schemas/Error404" | ||
"500": | ||
description: Internal server error | ||
content: | ||
application/json: | ||
schema: | ||
$ref: "#/components/schemas/Error500" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,70 +1,62 @@ | ||
package handlers | ||
|
||
import ( | ||
"database/sql" | ||
"net/http" | ||
|
||
"github.com/Dmytro-Hladkykh/link-shortener-svc/internal/data" | ||
"github.com/Dmytro-Hladkykh/link-shortener-svc/internal/data/pg" | ||
"github.com/Dmytro-Hladkykh/link-shortener-svc/internal/service/requests" | ||
"gitlab.com/distributed_lab/ape" | ||
"gitlab.com/distributed_lab/ape/problems" | ||
) | ||
|
||
func CreateShortLink(w http.ResponseWriter, r *http.Request) { | ||
request, err := requests.NewCreateShortLinkRequest(r) | ||
if err != nil { | ||
Log(r).WithError(err).Error("failed to create request") | ||
ape.RenderErr(w, problems.BadRequest(err)...) | ||
return | ||
} | ||
|
||
db := DB(r) | ||
if db == nil { | ||
Log(r).Error("database connection is nil") | ||
ape.RenderErr(w, problems.InternalError()) | ||
return | ||
} | ||
|
||
shortLinkQ := pg.NewShortLinkQ(db) | ||
if shortLinkQ == nil { | ||
Log(r).Error("shortLinkQ is nil") | ||
ape.RenderErr(w, problems.InternalError()) | ||
return | ||
} | ||
|
||
// check for existing link | ||
existingLink, err := shortLinkQ.FilterByOriginalURL(request.OriginalURL).Get() | ||
if err != nil && err != sql.ErrNoRows { | ||
Log(r).WithError(err).Error("failed to check existing link") | ||
ape.RenderErr(w, problems.InternalError()) | ||
return | ||
} | ||
|
||
if existingLink != nil { | ||
// if exist then return short code | ||
ape.Render(w, map[string]interface{}{"short_code": existingLink.ShortCode}) | ||
return | ||
} | ||
|
||
// if new link then generate new short code | ||
shortCode, err := data.GenerateShortCode() | ||
if err != nil { | ||
Log(r).WithError(err).Error("failed to generate short code") | ||
ape.RenderErr(w, problems.InternalError()) | ||
return | ||
} | ||
|
||
// create new short link in db | ||
newLink, err := shortLinkQ.Insert(data.ShortLink{ | ||
OriginalURL: request.OriginalURL, | ||
ShortCode: shortCode, | ||
}) | ||
if err != nil { | ||
Log(r).WithError(err).Error("failed to insert new short link") | ||
ape.RenderErr(w, problems.InternalError()) | ||
return | ||
} | ||
|
||
ape.Render(w, map[string]interface{}{"short_code": newLink.ShortCode}) | ||
} | ||
type CreateShortLinkHandler struct { | ||
repo data.ShortLinkQ | ||
} | ||
|
||
func NewCreateShortLinkHandler(repo data.ShortLinkQ) *CreateShortLinkHandler { | ||
return &CreateShortLinkHandler{repo: repo} | ||
} | ||
|
||
func (h *CreateShortLinkHandler) CreateShortLink(w http.ResponseWriter, r *http.Request) { | ||
request, err := requests.NewCreateShortLinkRequest(r) | ||
if err != nil { | ||
Log(r).WithError(err).Error("failed to create request") | ||
ape.RenderErr(w, problems.BadRequest(err)...) | ||
return | ||
} | ||
|
||
// check if link already exists | ||
existingLink, err := h.repo.FilterByOriginalURL(request.OriginalURL).Get() | ||
if err != nil { | ||
Log(r).WithError(err).Error("failed to check existing link") | ||
ape.RenderErr(w, problems.InternalError()) | ||
return | ||
} | ||
|
||
if existingLink != nil { | ||
// if exist then return short code | ||
ape.Render(w, map[string]interface{}{"short_code": existingLink.ShortCode}) | ||
return | ||
} | ||
|
||
// if new link then generate short code | ||
shortCode, err := data.GenerateShortCode() | ||
if err != nil { | ||
Log(r).WithError(err).Error("failed to generate short code") | ||
ape.RenderErr(w, problems.InternalError()) | ||
return | ||
} | ||
|
||
// create new link in db | ||
newLink, err := h.repo.Insert(data.ShortLink{ | ||
OriginalURL: request.OriginalURL, | ||
ShortCode: shortCode, | ||
}) | ||
if err != nil { | ||
Log(r).WithError(err).Error("failed to insert new short link") | ||
ape.RenderErr(w, problems.InternalError()) | ||
return | ||
} | ||
|
||
ape.Render(w, map[string]interface{}{"short_code": newLink.ShortCode}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.