handlers

package
v0.0.0-...-059eb94 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 17, 2022 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package handlers stores Shortener api handlers.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Handlers

type Handlers struct {
	// contains filtered or unexported fields
}

func NewHandlers

func NewHandlers(store store.LinksStorager, wp app.WorkerPool, logger *zap.SugaredLogger, context context.Context) Handlers

func (Handlers) AddShortLinkHandler

func (h Handlers) AddShortLinkHandler(w http.ResponseWriter, req *http.Request)

AddShortLinkHandler creates and return short representation of URL address and saves it.

Example
request := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(yandexLink))
w := httptest.NewRecorder()
h := http.HandlerFunc(H.AddShortLinkHandler)
h.ServeHTTP(w, request)
Output:

func (Handlers) BatchHandler

func (h Handlers) BatchHandler(w http.ResponseWriter, req *http.Request)

BatchHandler takes a couple of URL addresses via JSON, creates and returns short representation of that and saves it.

Example
req, err := json.Marshal([]models.BatchOriginal{
	{CorrelationID: "1",
		OriginalURL: gitLink},
	{CorrelationID: "2",
		OriginalURL: yandexLink},
})
if err != nil {
	log.Fatal(err)
}

request := httptest.NewRequest(http.MethodPost, "/user/urls", bytes.NewBuffer(req))
w := httptest.NewRecorder()
h := http.HandlerFunc(H.BatchHandler)
h.ServeHTTP(w, request)
Output:

func (Handlers) DeleteLinksHandler

func (h Handlers) DeleteLinksHandler(w http.ResponseWriter, req *http.Request)

DeleteLinksHandler takes a couple of user's URL addresses via JSON and deletes from store.

Example
link := app.ShortLink([]byte(yandexLink))
req, err := json.Marshal([]string{
	link,
})
if err != nil {
	log.Fatal(err)
}
authCookie := &http.Cookie{Name: auth.AuthCookie, Value: "authCookieValue"}

request := httptest.NewRequest(http.MethodDelete, "/api/user/urls", bytes.NewBuffer(req))
request.AddCookie(authCookie)
w := httptest.NewRecorder()
h := http.HandlerFunc(H.DeleteLinksHandler)
h.ServeHTTP(w, request)
Output:

func (Handlers) GetShortLinkHandler

func (h Handlers) GetShortLinkHandler(w http.ResponseWriter, req *http.Request)

GetShortLinkHandler redirects client to full url address by short representation.

Example
request := httptest.NewRequest(http.MethodGet, "/"+yandexLink, nil)
w := httptest.NewRecorder()
h := http.HandlerFunc(H.GetShortLinkHandler)
h.ServeHTTP(w, request)
Output:

func (Handlers) GetUserUrlsHandler

func (h Handlers) GetUserUrlsHandler(w http.ResponseWriter, req *http.Request)

GetUserUrlsHandler returns user's loaded links by userID.

Example
request := httptest.NewRequest(http.MethodGet, "/user/urls", nil)
authCookie := &http.Cookie{Name: auth.AuthCookie, Value: "authCookieValue"}
request.AddCookie(authCookie)

w := httptest.NewRecorder()
h := http.HandlerFunc(H.GetUserUrlsHandler)
h.ServeHTTP(w, request)
Output:

func (Handlers) PingDatabaseHandler

func (h Handlers) PingDatabaseHandler(w http.ResponseWriter, req *http.Request)

PingDatabaseHandler checks if links storage is available.

func (Handlers) ShortenHandler

func (h Handlers) ShortenHandler(w http.ResponseWriter, req *http.Request)

ShortenHandler creates and returns short representation of URL address and saves it via JSON.

Example
sReq := models.ShortenRequest{URL: yandexLink}
body, err := json.Marshal(sReq)
request := httptest.NewRequest(http.MethodPost, "/api/shorten", bytes.NewBuffer(body))
if err != nil {
	log.Fatal(err)
}

w := httptest.NewRecorder()
h := http.HandlerFunc(H.ShortenHandler)
h.ServeHTTP(w, request)
Output:

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL