From e29742c43e0d1c704844bdd459b024d396d6c92f Mon Sep 17 00:00:00 2001 From: xtrafrancyz Date: Fri, 3 Apr 2020 15:24:38 +0300 Subject: [PATCH] Update dependencies --- go.mod | 11 ++++++ go.sum | 31 +++++++++++++++++ web.go | 106 +++++++++++++++++++++++++++------------------------------ 3 files changed, 92 insertions(+), 56 deletions(-) create mode 100644 go.mod create mode 100644 go.sum diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..6a70fb7 --- /dev/null +++ b/go.mod @@ -0,0 +1,11 @@ +module github.com/xtrafrancyz/golish + +go 1.14 + +require ( + github.com/GeertJohan/go.rice v1.0.0 + github.com/fasthttp/router v1.0.0 + github.com/go-sql-driver/mysql v1.5.0 + github.com/valyala/fasthttp v1.9.0 + github.com/vharitonsky/iniflags v0.0.0-20180513140207-a33cd0b5f3de +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..5d382dc --- /dev/null +++ b/go.sum @@ -0,0 +1,31 @@ +github.com/GeertJohan/go.incremental v1.0.0/go.mod h1:6fAjUhbVuX1KcMD3c8TEgVUqmo4seqhv0i0kdATSkM0= +github.com/GeertJohan/go.rice v1.0.0 h1:KkI6O9uMaQU3VEKaj01ulavtF7o1fWT7+pk/4voiMLQ= +github.com/GeertJohan/go.rice v1.0.0/go.mod h1:eH6gbSOAUv07dQuZVnBmoDP8mgsM1rtixis4Tib9if0= +github.com/akavel/rsrc v0.8.0/go.mod h1:uLoCtb9J+EyAqh+26kdrTgmzRBFPGOolLWKpdxkKq+c= +github.com/daaku/go.zipexe v1.0.0 h1:VSOgZtH418pH9L16hC/JrgSNJbbAL26pj7lmD1+CGdY= +github.com/daaku/go.zipexe v1.0.0/go.mod h1:z8IiR6TsVLEYKwXAoE/I+8ys/sDkgTzSL0CLnGVd57E= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/fasthttp/router v1.0.0 h1:ziodHFKysNBSMsVeVjuHdI4wzhPwYTE2sT+1uBgHFxk= +github.com/fasthttp/router v1.0.0/go.mod h1:ZVa4I0mMb6i+hPjW90TE5DzFpIv03w8eAo6dlExRE7U= +github.com/go-sql-driver/mysql v1.5.0 h1:ozyZYNQW3x3HtqT1jira07DN2PArx2v7/mN66gGcHOs= +github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= +github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI= +github.com/klauspost/compress v1.8.2 h1:Bx0qjetmNjdFXASH02NSAREKpiaDwkO1DRZ3dV2KCcs= +github.com/klauspost/compress v1.8.2/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A= +github.com/klauspost/cpuid v1.2.1 h1:vJi+O/nMdFt0vqm8NZBI6wzALWdA2X+egi0ogNyrC/w= +github.com/klauspost/cpuid v1.2.1/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgoMS4s3ek= +github.com/nkovacs/streamquote v0.0.0-20170412213628-49af9bddb229/go.mod h1:0aYXnNPJ8l7uZxf45rWW1a/uME32OF0rhiYGNQ2oF2E= +github.com/savsgio/gotils v0.0.0-20200319105752-a9cc718f6a3f h1:XfUnevLK4O22at3R77FlyQHKwlQs75LELdsH2wRX2KQ= +github.com/savsgio/gotils v0.0.0-20200319105752-a9cc718f6a3f/go.mod h1:lHhJedqxCoHN+zMtwGNTXWmF0u9Jt363FYRhV6g0CdY= +github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= +github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= +github.com/valyala/fasthttp v1.9.0 h1:hNpmUdy/+ZXYpGy0OBfm7K0UQTzb73W0T0U4iJIVrMw= +github.com/valyala/fasthttp v1.9.0/go.mod h1:FstJa9V+Pj9vQ7OJie2qMHdwemEDaDiSdBnvPM1Su9w= +github.com/valyala/fasttemplate v1.0.1/go.mod h1:UQGH1tvbgY+Nz5t2n7tXsz52dQxojPUpymEIMZ47gx8= +github.com/valyala/tcplisten v0.0.0-20161114210144-ceec8f93295a/go.mod h1:v3UYOV9WzVtRmSR+PDvWpU/qWl4Wa5LApYYX4ZtKbio= +github.com/vharitonsky/iniflags v0.0.0-20180513140207-a33cd0b5f3de h1:fkw+7JkxF3U1GzQoX9h69Wvtvxajo5Rbzy6+YMMzPIg= +github.com/vharitonsky/iniflags v0.0.0-20180513140207-a33cd0b5f3de/go.mod h1:irMhzlTz8+fVFj6CH2AN2i+WI5S6wWFtK3MBCIxIpyI= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/net v0.0.0-20190827160401-ba9fcec4b297/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= diff --git a/web.go b/web.go index 095fbc0..786b255 100644 --- a/web.go +++ b/web.go @@ -9,7 +9,7 @@ import ( "strings" "github.com/GeertJohan/go.rice" - "github.com/qiangxue/fasthttp-routing" + "github.com/fasthttp/router" "github.com/valyala/fasthttp" "github.com/xtrafrancyz/golish/backend" ) @@ -20,18 +20,18 @@ type web struct { } func (w *web) run() { - router := routing.New() - router.Get("/", w.handleRoot) - router.Get("/", w.handleSlug) - adminGroup := router.Group("/@"+Config.adminPath, w.addACAO) - adminGroup.Get("/list", w.handleList) - adminGroup.Post("/create", w.handleCreate) - adminGroup.Post("/delete", w.handleDelete) - adminGroup.Post("/edit", w.handleEdit) - adminGroup.Get("/", w.handleAdminRoot) + r := router.New() + r.GET("/", w.handleRoot) + r.GET("/{slug:["+Config.slugCharacters+"]+}", w.handleSlug) + adminGroup := r.Group("/@" + Config.adminPath) + adminGroup.GET("/list", w.addACAO(w.handleList)) + adminGroup.POST("/create", w.addACAO(w.handleCreate)) + adminGroup.POST("/delete", w.addACAO(w.handleDelete)) + adminGroup.POST("/edit", w.addACAO(w.handleEdit)) + adminGroup.GET("/{file:*}", w.addACAO(w.handleAdminRoot)) server := &fasthttp.Server{ - Handler: router.HandleRequest, + Handler: r.Handler, ReduceMemoryUsage: true, } @@ -43,73 +43,70 @@ func (w *web) run() { } } -func (w *web) addACAO(c *routing.Context) error { - c.Response.Header.Set("Access-Control-Allow-Origin", "*") - return nil +func (w *web) addACAO(h fasthttp.RequestHandler) fasthttp.RequestHandler { + return func(ctx *fasthttp.RequestCtx) { + ctx.Response.Header.Set("Access-Control-Allow-Origin", "*") + h(ctx) + } } -func (w *web) handleRoot(c *routing.Context) error { +func (w *web) handleRoot(ctx *fasthttp.RequestCtx) { if Config.defaultRedirect != "" { - c.Redirect(Config.defaultRedirect, fasthttp.StatusFound) + ctx.Redirect(Config.defaultRedirect, fasthttp.StatusFound) } else { - c.NotFound() + ctx.NotFound() } - return nil } -func (w *web) handleSlug(c *routing.Context) error { - log.Print(string(c.URI().Path())) - full := w.backend.TryClickLink(c.Param("slug")) +func (w *web) handleSlug(ctx *fasthttp.RequestCtx) { + log.Print(string(ctx.URI().Path())) + full := w.backend.TryClickLink(ctx.UserValue("slug").(string)) if full != nil { - c.Redirect(full.Url, fasthttp.StatusFound) + ctx.Redirect(full.Url, fasthttp.StatusFound) } else { - c.NotFound() + ctx.NotFound() } - - return nil } -func (w *web) handleAdminRoot(c *routing.Context) error { - path := "/" + c.Param("file") - if path == "/" { +func (w *web) handleAdminRoot(ctx *fasthttp.RequestCtx) { + path := "/" + ctx.UserValue("file").(string) + if path == "//" { path = "/index.html" } log.Print("{admin}" + path) bytes, err := w.adminFiles.Bytes(path) if err != nil { - c.NotFound() + ctx.NotFound() } else { if strings.HasSuffix(path, ".html") { - c.Response.Header.Set("Content-Type", "text/html") + ctx.Response.Header.Set("Content-Type", "text/html") } else if strings.HasSuffix(path, ".css") { - c.Response.Header.Set("Content-Type", "text/css") + ctx.Response.Header.Set("Content-Type", "text/css") } else if strings.HasSuffix(path, ".js") { - c.Response.Header.Set("Content-Type", "application/javascript") + ctx.Response.Header.Set("Content-Type", "application/javascript") } else if strings.HasSuffix(path, ".png") { - c.Response.Header.Set("Content-Type", "image/png") + ctx.Response.Header.Set("Content-Type", "image/png") } - c.SetStatusCode(fasthttp.StatusOK) - _, _ = c.Write(bytes) + ctx.SetStatusCode(fasthttp.StatusOK) + _, _ = ctx.Write(bytes) } - return nil } -func (w *web) handleList(c *routing.Context) error { +func (w *web) handleList(ctx *fasthttp.RequestCtx) { log.Print("{admin}/list") links := w.backend.GetAllLinks() marshaled, _ := json.Marshal(links) - c.Response.Header.Set("Content-Type", "application/json") - _, _ = c.Write(marshaled) - return nil + ctx.Response.Header.Set("Content-Type", "application/json") + _, _ = ctx.Write(marshaled) } -func (w *web) handleCreate(c *routing.Context) error { - url := c.PostArgs().Peek("url") - slug := c.PostArgs().Peek("slug") +func (w *web) handleCreate(ctx *fasthttp.RequestCtx) { + url := ctx.PostArgs().Peek("url") + slug := ctx.PostArgs().Peek("slug") log.Printf("{admin}/create (url=%s, slug=%s)", url, slug) if len(url) == 0 { - c.SetStatusCode(fasthttp.StatusBadRequest) + ctx.SetStatusCode(fasthttp.StatusBadRequest) } else { var link *backend.Link = nil var err error = nil @@ -131,27 +128,24 @@ func (w *web) handleCreate(c *routing.Context) error { } else { marshaled, _ = json.Marshal(link) } - c.Response.Header.Set("Content-Type", "application/json") - _, _ = c.Write(marshaled) + ctx.Response.Header.Set("Content-Type", "application/json") + _, _ = ctx.Write(marshaled) } - return nil } -func (w *web) handleDelete(c *routing.Context) error { - slug := c.PostArgs().Peek("slug") +func (w *web) handleDelete(ctx *fasthttp.RequestCtx) { + slug := ctx.PostArgs().Peek("slug") log.Printf("{admin}/delete (slug=%s)", slug) w.backend.Delete(string(slug)) - c.SetStatusCode(fasthttp.StatusOK) - return nil + ctx.SetStatusCode(fasthttp.StatusOK) } -func (w *web) handleEdit(c *routing.Context) error { - slug := c.PostArgs().Peek("slug") - url := c.PostArgs().Peek("url") +func (w *web) handleEdit(ctx *fasthttp.RequestCtx) { + slug := ctx.PostArgs().Peek("slug") + url := ctx.PostArgs().Peek("url") log.Printf("{admin}/edit (slug=%s, url=%s)", slug, url) w.backend.Edit(string(slug), string(url)) - c.SetStatusCode(fasthttp.StatusOK) - return nil + ctx.SetStatusCode(fasthttp.StatusOK) } type OperationError struct {