Skip to content

Commit

Permalink
Added specific http method logic
Browse files Browse the repository at this point in the history
  • Loading branch information
burkaydurdu committed Mar 6, 2022
1 parent 092b628 commit 9347633
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 229 deletions.
83 changes: 1 addition & 82 deletions docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,88 +126,7 @@ const docTemplate = `{
],
"responses": {
"200": {
"description": ""
},
"404": {
"description": "Not found",
"schema": {
"type": "string"
}
}
}
},
"put": {
"description": "It redirects from short URL to original URL",
"tags": [
"Redirect"
],
"summary": "Short URL",
"parameters": [
{
"type": "string",
"description": "Shortly Code",
"name": "code",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"description": ""
},
"404": {
"description": "Not found",
"schema": {
"type": "string"
}
}
}
},
"post": {
"description": "It redirects from short URL to original URL",
"tags": [
"Redirect"
],
"summary": "Short URL",
"parameters": [
{
"type": "string",
"description": "Shortly Code",
"name": "code",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"description": ""
},
"404": {
"description": "Not found",
"schema": {
"type": "string"
}
}
}
},
"delete": {
"description": "It redirects from short URL to original URL",
"tags": [
"Redirect"
],
"summary": "Short URL",
"parameters": [
{
"type": "string",
"description": "Shortly Code",
"name": "code",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"description": ""
"description": "Redirect URL"
},
"404": {
"description": "Not found",
Expand Down
83 changes: 1 addition & 82 deletions docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -117,88 +117,7 @@
],
"responses": {
"200": {
"description": ""
},
"404": {
"description": "Not found",
"schema": {
"type": "string"
}
}
}
},
"put": {
"description": "It redirects from short URL to original URL",
"tags": [
"Redirect"
],
"summary": "Short URL",
"parameters": [
{
"type": "string",
"description": "Shortly Code",
"name": "code",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"description": ""
},
"404": {
"description": "Not found",
"schema": {
"type": "string"
}
}
}
},
"post": {
"description": "It redirects from short URL to original URL",
"tags": [
"Redirect"
],
"summary": "Short URL",
"parameters": [
{
"type": "string",
"description": "Shortly Code",
"name": "code",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"description": ""
},
"404": {
"description": "Not found",
"schema": {
"type": "string"
}
}
}
},
"delete": {
"description": "It redirects from short URL to original URL",
"tags": [
"Redirect"
],
"summary": "Short URL",
"parameters": [
{
"type": "string",
"description": "Shortly Code",
"name": "code",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"description": ""
"description": "Redirect URL"
},
"404": {
"description": "Not found",
Expand Down
56 changes: 1 addition & 55 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,24 +43,6 @@ info:
version: "1.0"
paths:
/{code}:
delete:
description: It redirects from short URL to original URL
parameters:
- description: Shortly Code
in: path
name: code
required: true
type: string
responses:
"200":
description: ""
"404":
description: Not found
schema:
type: string
summary: Short URL
tags:
- Redirect
get:
description: It redirects from short URL to original URL
parameters:
Expand All @@ -71,43 +53,7 @@ paths:
type: string
responses:
"200":
description: ""
"404":
description: Not found
schema:
type: string
summary: Short URL
tags:
- Redirect
post:
description: It redirects from short URL to original URL
parameters:
- description: Shortly Code
in: path
name: code
required: true
type: string
responses:
"200":
description: ""
"404":
description: Not found
schema:
type: string
summary: Short URL
tags:
- Redirect
put:
description: It redirects from short URL to original URL
parameters:
- description: Shortly Code
in: path
name: code
required: true
type: string
responses:
"200":
description: ""
description: Redirect URL
"404":
description: Not found
schema:
Expand Down
3 changes: 0 additions & 3 deletions internal/domain/shortly/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ type shortlyHandler struct {
// @Failure 404 {string} string "Not found"
// @Param code path string true "Shortly Code"
// @Router /{code} [get]
// @Router /{code} [post]
// @Router /{code} [put]
// @Router /{code} [delete]
func (s *shortlyHandler) RedirectURL(w http.ResponseWriter, r *http.Request) {
// Remove first character `` / ``
code := r.URL.Path[1:]
Expand Down
21 changes: 14 additions & 7 deletions internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,25 @@ import (
type route struct {
pattern *regexp.Regexp
handler http.Handler
method string
}

type ShortlyMux struct {
routes []*route
l *shortlyLog.ShortlyLog
}

func (h *ShortlyMux) Handler(pattern *regexp.Regexp, handler http.Handler) {
h.routes = append(h.routes, &route{pattern, handler})
func (h *ShortlyMux) Handler(pattern *regexp.Regexp, handler http.Handler, method string) {
h.routes = append(h.routes, &route{pattern, handler, method})
}

func (h *ShortlyMux) HandleFunc(pattern *regexp.Regexp, handler func(http.ResponseWriter, *http.Request)) {
h.routes = append(h.routes, &route{pattern, http.HandlerFunc(handler)})
func (h *ShortlyMux) HandleFunc(pattern *regexp.Regexp, handler func(http.ResponseWriter, *http.Request), method string) {
h.routes = append(h.routes, &route{pattern, http.HandlerFunc(handler), method})
}

func (h *ShortlyMux) ServeHTTP(w http.ResponseWriter, r *http.Request) {
for _, route := range h.routes {
if route.pattern.MatchString(r.URL.Path) {
if route.pattern.MatchString(r.URL.Path) && route.method == r.Method {
route.handler.ServeHTTP(w, r)
return
}
Expand Down Expand Up @@ -99,33 +100,39 @@ func (s *Server) Start() error {
// For Swagger API Documentation
// We want to serve our api docs
fs := http.FileServer(http.Dir("./docs"))
s.s.Handler(regexp.MustCompile("/docs/"), http.StripPrefix("/docs/", fs))
s.s.Handler(regexp.MustCompile("/docs/"), http.StripPrefix("/docs/", fs), http.MethodGet)

// Serve Swagger UI
s.s.Handler(
regexp.MustCompile("/api/swagger/.*"),
httpSwagger.Handler(
httpSwagger.URL("/docs/swagger.json")))
httpSwagger.URL("/docs/swagger.json")),
http.MethodGet,
)

s.s.Handler(
regexp.MustCompile("/api/v1/health"),
HTTPLogMiddleware(s.s.l, http.HandlerFunc(s.healthCheck)),
http.MethodGet,
)

s.s.Handler(
regexp.MustCompile("/api/v1/create"),
HTTPLogMiddleware(s.s.l, http.HandlerFunc(shortlyHandler.CreateShortURL)),
http.MethodPost,
)

s.s.Handler(
regexp.MustCompile("api/v1/list"),
HTTPLogMiddleware(s.s.l, http.HandlerFunc(shortlyHandler.GetShortList)),
http.MethodGet,
)

// Short URL End-point.
s.s.Handler(
regexp.MustCompile("^/[^/]*$"),
HTTPLogMiddleware(s.s.l, http.HandlerFunc(shortlyHandler.RedirectURL)),
http.MethodGet,
)

err = http.ListenAndServe(
Expand Down

0 comments on commit 9347633

Please sign in to comment.