diff --git a/docs/docs.go b/docs/docs.go index be72c29..16e735b 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -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", diff --git a/docs/swagger.json b/docs/swagger.json index fa9ab9d..d2aa708 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -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", diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 32fd262..b9e69ff 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -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: @@ -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: diff --git a/internal/domain/shortly/handler.go b/internal/domain/shortly/handler.go index be4f372..57f891f 100644 --- a/internal/domain/shortly/handler.go +++ b/internal/domain/shortly/handler.go @@ -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:] diff --git a/internal/server/server.go b/internal/server/server.go index b4d3da6..f97797b 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -21,6 +21,7 @@ import ( type route struct { pattern *regexp.Regexp handler http.Handler + method string } type ShortlyMux struct { @@ -28,17 +29,17 @@ type ShortlyMux struct { 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 } @@ -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(