mirror of
https://github.com/jimeh/ozu.io.git
synced 2026-02-19 08:06:39 +00:00
Refactor shortner and web packages for new Store interface
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
|
||||
@@ -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")
|
||||
|
||||
@@ -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),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user