From 2ad107b9e5b553c8fa743c2a900fe152f50de667 Mon Sep 17 00:00:00 2001 From: Phil Date: Fri, 10 Dec 2021 22:38:02 +0100 Subject: [PATCH] Make Json Enpoint / CLI | PRG --- go.mod | 6 +++--- go.sum | 9 ++++++--- routes/id.go | 15 +++++++++++++++ routes/posthome.go | 41 ++++++++++++++++++++++++++++++++++++++++- 4 files changed, 64 insertions(+), 7 deletions(-) diff --git a/go.mod b/go.mod index 809fd53..51a6bf4 100644 --- a/go.mod +++ b/go.mod @@ -10,12 +10,12 @@ require ( ) require ( - github.com/andybalholm/brotli v1.0.2 // indirect - github.com/klauspost/compress v1.13.4 // indirect + github.com/andybalholm/brotli v1.0.4 // indirect + github.com/klauspost/compress v1.13.6 // indirect github.com/onsi/ginkgo v1.16.5 // indirect github.com/onsi/gomega v1.17.0 // indirect github.com/valyala/bytebufferpool v1.0.0 // indirect github.com/valyala/fasthttp v1.31.0 // indirect github.com/valyala/tcplisten v1.0.0 // indirect - golang.org/x/sys v0.0.0-20210514084401-e8d321eab015 // indirect + golang.org/x/sys v0.0.0-20211210111614-af8b64212486 // indirect ) diff --git a/go.sum b/go.sum index 47bb127..8d32e7d 100644 --- a/go.sum +++ b/go.sum @@ -43,8 +43,9 @@ github.com/CloudyKit/fastprinter v0.0.0-20200109182630-33d98a066a53/go.mod h1:+3 github.com/CloudyKit/jet/v6 v6.1.0/go.mod h1:d3ypHeIRNo2+XyqnGA8s+aphtcVpjP5hPwP/Lzo7Ro4= github.com/Joker/hpp v1.0.0/go.mod h1:8x5n+M1Hp5hC0g8okX3sR3vFQwynaX/UgSOM9MeBKzY= github.com/Joker/jade v1.1.1/go.mod h1:C5O3w7HbsWdb9ik1puKS81QsllcBd+CXRVCbXFwSdsE= -github.com/andybalholm/brotli v1.0.2 h1:JKnhI/XQ75uFBTiuzXpzFrUriDPiZjlOSzh6wXogP0E= github.com/andybalholm/brotli v1.0.2/go.mod h1:loMXtMfwqflxFJPmdbJO0a3KNoPuLBgiu3qAvBg8x/Y= +github.com/andybalholm/brotli v1.0.4 h1:V7DdXeJtZscaqfNuAdSRuRFzuiKlHSC/Zh3zl9qY3JY= +github.com/andybalholm/brotli v1.0.4/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig= github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY= github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o= github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmVTwzkszR9V5SSuryQ31EELlFMUz1kKyl939pY= @@ -188,8 +189,9 @@ github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/X github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU= github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8= github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= -github.com/klauspost/compress v1.13.4 h1:0zhec2I8zGnjWcKyLl6i3gPqKANCCn5e9xmviEEeX6s= github.com/klauspost/compress v1.13.4/go.mod h1:8dP1Hq4DHOhN9w426knH3Rhby4rFm6D8eO+e+Dq5Gzg= +github.com/klauspost/compress v1.13.6 h1:P76CopJELS0TiO2mebmnzgWaajssP/EszplttgQxcgc= +github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk= github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= @@ -438,8 +440,9 @@ golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20210403161142-5e06dd20ab57/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20210514084401-e8d321eab015 h1:hZR0X1kPW+nwyJ9xRxqZk1vx5RUObAPBdKVvXPDUH/E= golang.org/x/sys v0.0.0-20210514084401-e8d321eab015/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20211210111614-af8b64212486 h1:5hpz5aRr+W1erYCL5JRhSUBJRph7l9XkNveoExlrKYk= +golang.org/x/sys v0.0.0-20211210111614-af8b64212486/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= diff --git a/routes/id.go b/routes/id.go index 27facd2..9193a18 100644 --- a/routes/id.go +++ b/routes/id.go @@ -8,6 +8,21 @@ import ( func ID(c *fiber.Ctx) error { val, err := db.Client.Get(c.Params("id")).Result() + + if c.Get("CLI") == "1" { + if err != nil { + return c.Status(404).JSON(&fiber.Map{ + "error": true, + "url": "URL not found", + }) + } else { + return c.Status(301).JSON(&fiber.Map{ + "error": false, + "url": val, + }) + } + } + if err != nil { return c.Render("views/404", fiber.Map{ "BASEURL": utils.URL, diff --git a/routes/posthome.go b/routes/posthome.go index ca8dc5b..711b6e2 100644 --- a/routes/posthome.go +++ b/routes/posthome.go @@ -11,12 +11,21 @@ import ( ) type eurl struct { - URL string `form:"surl"` + CLI bool `json:"cli"` + URL string `json:"surl" form:"surl"` } func Posthome(c *fiber.Ctx) error { u := new(eurl) if err := c.BodyParser(u); err != nil { + + if u.CLI { + return c.Status(500).JSON(&fiber.Map{ + "success": false, + "msg": "Parsing Error", + }) + } + return c.Status(500).Render("views/home", fiber.Map{ "ERR": "Parsing Error", "GitCommitShort": utils.GitCommitShort, @@ -26,6 +35,14 @@ func Posthome(c *fiber.Ctx) error { } if !regexp.MustCompile(`^(http|https|mailto|ts3server)://`).MatchString(u.URL) { + + if u.CLI { + return c.Status(424).JSON(&fiber.Map{ + "success": false, + "msg": "Invalid URL", + }) + } + return c.Status(424).Render("views/home", fiber.Map{ "ERR": "Invalid URL, please check and try again.", "GitCommitShort": utils.GitCommitShort, @@ -37,6 +54,14 @@ func Posthome(c *fiber.Ctx) error { id, err := gonanoid.New(8) if err != nil { + + if u.CLI { + return c.Status(500).JSON(&fiber.Map{ + "success": false, + "msg": err.Error(), + }) + } + return c.Status(500).Render("views/home", fiber.Map{ "ERR": err.Error(), "GitCommitShort": utils.GitCommitShort, @@ -48,6 +73,13 @@ func Posthome(c *fiber.Ctx) error { err = db.Client.Set(id, u.URL, 1296000*time.Second).Err() if err != nil { + if u.CLI { + return c.Status(500).JSON(&fiber.Map{ + "success": false, + "msg": err.Error(), + }) + } + return c.Status(500).Render("views/home", fiber.Map{ "ERR": err.Error(), "GitCommitShort": utils.GitCommitShort, @@ -58,6 +90,13 @@ func Posthome(c *fiber.Ctx) error { fURL := utils.URL + id + if u.CLI { + return c.Status(201).JSON(&fiber.Map{ + "success": true, + "URL": fURL, + }) + } + return c.Status(201).Render("views/home", fiber.Map{ "URL": fURL, "GitCommitShort": utils.GitCommitShort,