Refactor shortner and web packages for new Store interface

This commit is contained in:
2016-12-04 23:01:51 +00:00
parent d86f3d8218
commit de2ec75696
7 changed files with 193 additions and 133 deletions

View File

@@ -20,24 +20,24 @@ type APIHandler struct {
// Shorten shortens given URL.
func (h *APIHandler) Shorten(c *routing.Context) error {
uid, url, err := h.shortener.Shorten(c.FormValue("url"))
record, err := h.shortener.Shorten(c.FormValue("url"))
if err != nil {
return h.respondWithError(c, err)
}
r := makeResponse(c, uid, url)
r := makeResponse(c, record)
return h.respond(c, &r)
}
// Lookup shortened UID.
func (h *APIHandler) Lookup(c *routing.Context) error {
uid := c.FormValue("uid")
url, err := h.shortener.Lookup(uid)
record, err := h.shortener.Lookup(uid)
if err != nil {
return h.respondWithError(c, err)
}
r := makeResponse(c, uid, url)
r := makeResponse(c, record)
return h.respond(c, &r)
}

View File

@@ -71,12 +71,12 @@ func (h *Handler) Index(c *routing.Context) error {
rawURL := c.FormValue("url")
if len(rawURL) > 0 {
uid, url, err := h.shortener.Shorten(rawURL)
record, err := h.shortener.Shorten(rawURL)
if err != nil {
return h.respond(c, template, makeErrResponse(err))
}
r := makeResponse(c, uid, url)
r := makeResponse(c, record)
return h.respond(c, template, r)
}
@@ -106,19 +106,19 @@ func (h *Handler) Static(c *routing.Context) error {
func (h *Handler) LookupAndRedirect(c *routing.Context) error {
uid := []byte(c.Param("uid"))
url, err := h.shortener.Lookup(uid)
record, err := h.shortener.Lookup(uid)
if err != nil {
return h.NotFound(c)
}
r := makeResponse(c, uid, url)
r := makeResponse(c, record)
c.Response.Header.Set("Pragma", "no-cache")
c.Response.Header.Set("Expires", "Mon, 01 Jan 1990 00:00:00 GMT")
c.Response.Header.Set("X-XSS-Protection", "1; mode=block")
c.Response.Header.Set("Cache-Control",
"no-cache, no-store, max-age=0, must-revalidate")
c.Redirect(string(url), fasthttp.StatusMovedPermanently)
c.Redirect(string(record.URL), fasthttp.StatusMovedPermanently)
c.Response.Header.Set("Connection", "close")
c.Response.Header.Set("X-Content-Type-Options", "nosniff")
c.Response.Header.Set("Accept-Ranges", "none")

View File

@@ -3,14 +3,15 @@ package web
import (
"net/url"
"github.com/jimeh/ozu.io/storage"
"github.com/qiangxue/fasthttp-routing"
)
func makeResponse(c *routing.Context, uid []byte, url []byte) Response {
func makeResponse(c *routing.Context, r *storage.Record) Response {
return Response{
UID: string(uid),
URL: makeShortURL(c, uid),
Target: string(url),
UID: string(r.UID),
URL: makeShortURL(c, r.UID),
Target: string(r.URL),
}
}