WIP: Refractor code / remove html engine / new restapi #21
8 changed files with 58 additions and 91 deletions
4
go.mod
4
go.mod
|
@ -7,7 +7,6 @@ toolchain go1.24.2
|
||||||
require (
|
require (
|
||||||
github.com/go-redis/redis/v8 v8.11.5
|
github.com/go-redis/redis/v8 v8.11.5
|
||||||
github.com/gofiber/fiber/v2 v2.52.6
|
github.com/gofiber/fiber/v2 v2.52.6
|
||||||
github.com/gofiber/template/html/v2 v2.1.3
|
|
||||||
github.com/joho/godotenv v1.5.1
|
github.com/joho/godotenv v1.5.1
|
||||||
github.com/matoous/go-nanoid/v2 v2.1.0
|
github.com/matoous/go-nanoid/v2 v2.1.0
|
||||||
)
|
)
|
||||||
|
@ -16,8 +15,6 @@ require (
|
||||||
github.com/andybalholm/brotli v1.1.1 // indirect
|
github.com/andybalholm/brotli v1.1.1 // indirect
|
||||||
github.com/cespare/xxhash/v2 v2.3.0 // indirect
|
github.com/cespare/xxhash/v2 v2.3.0 // indirect
|
||||||
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
|
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
|
||||||
github.com/gofiber/template v1.8.3 // indirect
|
|
||||||
github.com/gofiber/utils v1.1.0 // indirect
|
|
||||||
github.com/google/uuid v1.6.0 // indirect
|
github.com/google/uuid v1.6.0 // indirect
|
||||||
github.com/klauspost/compress v1.18.0 // indirect
|
github.com/klauspost/compress v1.18.0 // indirect
|
||||||
github.com/mattn/go-colorable v0.1.14 // indirect
|
github.com/mattn/go-colorable v0.1.14 // indirect
|
||||||
|
@ -25,6 +22,7 @@ require (
|
||||||
github.com/mattn/go-runewidth v0.0.16 // indirect
|
github.com/mattn/go-runewidth v0.0.16 // indirect
|
||||||
github.com/philhofer/fwd v1.1.3-0.20240916144458-20a13a1f6b7c // indirect
|
github.com/philhofer/fwd v1.1.3-0.20240916144458-20a13a1f6b7c // indirect
|
||||||
github.com/rivo/uniseg v0.4.7 // indirect
|
github.com/rivo/uniseg v0.4.7 // indirect
|
||||||
|
github.com/stretchr/testify v1.10.0 // indirect
|
||||||
github.com/tinylib/msgp v1.2.5 // indirect
|
github.com/tinylib/msgp v1.2.5 // indirect
|
||||||
github.com/valyala/bytebufferpool v1.0.0 // indirect
|
github.com/valyala/bytebufferpool v1.0.0 // indirect
|
||||||
github.com/valyala/fasthttp v1.60.0 // indirect
|
github.com/valyala/fasthttp v1.60.0 // indirect
|
||||||
|
|
6
go.sum
6
go.sum
|
@ -12,12 +12,6 @@ github.com/go-redis/redis/v8 v8.11.5 h1:AcZZR7igkdvfVmQTPnu9WE37LRrO/YrBH5zWyjDC
|
||||||
github.com/go-redis/redis/v8 v8.11.5/go.mod h1:gREzHqY1hg6oD9ngVRbLStwAWKhA0FEgq8Jd4h5lpwo=
|
github.com/go-redis/redis/v8 v8.11.5/go.mod h1:gREzHqY1hg6oD9ngVRbLStwAWKhA0FEgq8Jd4h5lpwo=
|
||||||
github.com/gofiber/fiber/v2 v2.52.6 h1:Rfp+ILPiYSvvVuIPvxrBns+HJp8qGLDnLJawAu27XVI=
|
github.com/gofiber/fiber/v2 v2.52.6 h1:Rfp+ILPiYSvvVuIPvxrBns+HJp8qGLDnLJawAu27XVI=
|
||||||
github.com/gofiber/fiber/v2 v2.52.6/go.mod h1:YEcBbO/FB+5M1IZNBP9FO3J9281zgPAreiI1oqg8nDw=
|
github.com/gofiber/fiber/v2 v2.52.6/go.mod h1:YEcBbO/FB+5M1IZNBP9FO3J9281zgPAreiI1oqg8nDw=
|
||||||
github.com/gofiber/template v1.8.3 h1:hzHdvMwMo/T2kouz2pPCA0zGiLCeMnoGsQZBTSYgZxc=
|
|
||||||
github.com/gofiber/template v1.8.3/go.mod h1:bs/2n0pSNPOkRa5VJ8zTIvedcI/lEYxzV3+YPXdBvq8=
|
|
||||||
github.com/gofiber/template/html/v2 v2.1.3 h1:n1LYBtmr9C0V/k/3qBblXyMxV5B0o/gpb6dFLp8ea+o=
|
|
||||||
github.com/gofiber/template/html/v2 v2.1.3/go.mod h1:U5Fxgc5KpyujU9OqKzy6Kn6Qup6Tm7zdsISR+VpnHRE=
|
|
||||||
github.com/gofiber/utils v1.1.0 h1:vdEBpn7AzIUJRhe+CiTOJdUcTg4Q9RK+pEa0KPbLdrM=
|
|
||||||
github.com/gofiber/utils v1.1.0/go.mod h1:poZpsnhBykfnY1Mc0KeEa6mSHrS3dV0+oBWyeQmb2e0=
|
|
||||||
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||||
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||||
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
|
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
|
||||||
|
|
9
main.go
9
main.go
|
@ -1,21 +1,15 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"embed"
|
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
|
||||||
|
|
||||||
"git.hackmi.ch/Phil/goshorly/db"
|
"git.hackmi.ch/Phil/goshorly/db"
|
||||||
"git.hackmi.ch/Phil/goshorly/routes"
|
"git.hackmi.ch/Phil/goshorly/routes"
|
||||||
"git.hackmi.ch/Phil/goshorly/utils"
|
"git.hackmi.ch/Phil/goshorly/utils"
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
"github.com/gofiber/fiber/v2/middleware/limiter"
|
"github.com/gofiber/fiber/v2/middleware/limiter"
|
||||||
"github.com/gofiber/template/html/v2"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
//go:embed views/*
|
|
||||||
var viewsfs embed.FS
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
utils.Print_Starting_Screen()
|
utils.Print_Starting_Screen()
|
||||||
utils.Init_env_file()
|
utils.Init_env_file()
|
||||||
|
@ -24,11 +18,8 @@ func main() {
|
||||||
|
|
||||||
db.Init_redis()
|
db.Init_redis()
|
||||||
|
|
||||||
engine := html.NewFileSystem(http.FS(viewsfs), ".html")
|
|
||||||
|
|
||||||
app := fiber.New(fiber.Config{
|
app := fiber.New(fiber.Config{
|
||||||
CaseSensitive: true,
|
CaseSensitive: true,
|
||||||
Views: engine,
|
|
||||||
})
|
})
|
||||||
|
|
||||||
app.Get("/", routes.Gethome)
|
app.Get("/", routes.Gethome)
|
||||||
|
|
|
@ -1,19 +1,9 @@
|
||||||
package routes
|
package routes
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"git.hackmi.ch/Phil/goshorly/db"
|
|
||||||
"git.hackmi.ch/Phil/goshorly/utils"
|
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Gethome(c *fiber.Ctx) error {
|
func Gethome(c *fiber.Ctx) error {
|
||||||
return c.Render("views/home", fiber.Map{
|
return c.SendString("Homepage")
|
||||||
"CI_COMMIT_SHA": utils.CI_COMMIT_SHA,
|
|
||||||
"CI_COMMIT_BRANCH": utils.CI_COMMIT_BRANCH,
|
|
||||||
"CI_COMMIT_TAG": utils.CI_COMMIT_TAG,
|
|
||||||
"CI_TAGGED": utils.CI_TAGGED,
|
|
||||||
"CI_BUILD": utils.CI_BUILD,
|
|
||||||
"TotalLinks": db.GetTotalLinks(),
|
|
||||||
"TotalViews": db.GetTotalViews(),
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,7 +4,6 @@ import (
|
||||||
"log"
|
"log"
|
||||||
|
|
||||||
"git.hackmi.ch/Phil/goshorly/db"
|
"git.hackmi.ch/Phil/goshorly/db"
|
||||||
"git.hackmi.ch/Phil/goshorly/utils"
|
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -18,9 +17,7 @@ func ID(c *fiber.Ctx) error {
|
||||||
"url": "URL not found",
|
"url": "URL not found",
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
return c.Render("views/404", fiber.Map{
|
return c.SendStatus(404)
|
||||||
"BASEURL": utils.URL,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -27,16 +27,16 @@ func Posthome(c *fiber.Ctx) error {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.Status(500).Render("views/home", fiber.Map{
|
// return c.Status(500).Render("views/home", fiber.Map{
|
||||||
"ERR": "Parsing Error",
|
// "ERR": "Parsing Error",
|
||||||
"CI_COMMIT_SHA": utils.CI_COMMIT_SHA,
|
// "CI_COMMIT_SHA": utils.CI_COMMIT_SHA,
|
||||||
"CI_COMMIT_BRANCH": utils.CI_COMMIT_BRANCH,
|
// "CI_COMMIT_BRANCH": utils.CI_COMMIT_BRANCH,
|
||||||
"CI_COMMIT_TAG": utils.CI_COMMIT_TAG,
|
// "CI_COMMIT_TAG": utils.CI_COMMIT_TAG,
|
||||||
"CI_TAGGED": utils.CI_TAGGED,
|
// "CI_TAGGED": utils.CI_TAGGED,
|
||||||
"CI_BUILD": utils.CI_BUILD,
|
// "CI_BUILD": utils.CI_BUILD,
|
||||||
"TotalLinks": db.GetTotalLinks(),
|
// "TotalLinks": db.GetTotalLinks(),
|
||||||
"TotalViews": db.GetTotalViews(),
|
// "TotalViews": db.GetTotalViews(),
|
||||||
})
|
// })
|
||||||
}
|
}
|
||||||
|
|
||||||
if !regexp.MustCompile(`^(http|https|mailto|ts3server)://`).MatchString(u.URL) {
|
if !regexp.MustCompile(`^(http|https|mailto|ts3server)://`).MatchString(u.URL) {
|
||||||
|
@ -48,16 +48,16 @@ func Posthome(c *fiber.Ctx) error {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.Status(424).Render("views/home", fiber.Map{
|
// return c.Status(424).Render("views/home", fiber.Map{
|
||||||
"ERR": "Invalid URL, please check and try again.",
|
// "ERR": "Invalid URL, please check and try again.",
|
||||||
"CI_COMMIT_SHA": utils.CI_COMMIT_SHA,
|
// "CI_COMMIT_SHA": utils.CI_COMMIT_SHA,
|
||||||
"CI_COMMIT_BRANCH": utils.CI_COMMIT_BRANCH,
|
// "CI_COMMIT_BRANCH": utils.CI_COMMIT_BRANCH,
|
||||||
"CI_COMMIT_TAG": utils.CI_COMMIT_TAG,
|
// "CI_COMMIT_TAG": utils.CI_COMMIT_TAG,
|
||||||
"CI_TAGGED": utils.CI_TAGGED,
|
// "CI_TAGGED": utils.CI_TAGGED,
|
||||||
"CI_BUILD": utils.CI_BUILD,
|
// "CI_BUILD": utils.CI_BUILD,
|
||||||
"TotalLinks": db.GetTotalLinks(),
|
// "TotalLinks": db.GetTotalLinks(),
|
||||||
"TotalViews": db.GetTotalViews(),
|
// "TotalViews": db.GetTotalViews(),
|
||||||
})
|
// })
|
||||||
}
|
}
|
||||||
|
|
||||||
id, err := gonanoid.New(8)
|
id, err := gonanoid.New(8)
|
||||||
|
@ -71,16 +71,16 @@ func Posthome(c *fiber.Ctx) error {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.Status(500).Render("views/home", fiber.Map{
|
// return c.Status(500).Render("views/home", fiber.Map{
|
||||||
"ERR": err.Error(),
|
// "ERR": err.Error(),
|
||||||
"CI_COMMIT_SHA": utils.CI_COMMIT_SHA,
|
// "CI_COMMIT_SHA": utils.CI_COMMIT_SHA,
|
||||||
"CI_COMMIT_BRANCH": utils.CI_COMMIT_BRANCH,
|
// "CI_COMMIT_BRANCH": utils.CI_COMMIT_BRANCH,
|
||||||
"CI_COMMIT_TAG": utils.CI_COMMIT_TAG,
|
// "CI_COMMIT_TAG": utils.CI_COMMIT_TAG,
|
||||||
"CI_TAGGED": utils.CI_TAGGED,
|
// "CI_TAGGED": utils.CI_TAGGED,
|
||||||
"CI_BUILD": utils.CI_BUILD,
|
// "CI_BUILD": utils.CI_BUILD,
|
||||||
"TotalLinks": db.GetTotalLinks(),
|
// "TotalLinks": db.GetTotalLinks(),
|
||||||
"TotalViews": db.GetTotalViews(),
|
// "TotalViews": db.GetTotalViews(),
|
||||||
})
|
// })
|
||||||
}
|
}
|
||||||
|
|
||||||
err = db.Set(id, u.URL, 1296000*time.Second)
|
err = db.Set(id, u.URL, 1296000*time.Second)
|
||||||
|
@ -93,16 +93,16 @@ func Posthome(c *fiber.Ctx) error {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.Status(500).Render("views/home", fiber.Map{
|
// return c.Status(500).Render("views/home", fiber.Map{
|
||||||
"ERR": err.Error(),
|
// "ERR": err.Error(),
|
||||||
"CI_COMMIT_SHA": utils.CI_COMMIT_SHA,
|
// "CI_COMMIT_SHA": utils.CI_COMMIT_SHA,
|
||||||
"CI_COMMIT_BRANCH": utils.CI_COMMIT_BRANCH,
|
// "CI_COMMIT_BRANCH": utils.CI_COMMIT_BRANCH,
|
||||||
"CI_COMMIT_TAG": utils.CI_COMMIT_TAG,
|
// "CI_COMMIT_TAG": utils.CI_COMMIT_TAG,
|
||||||
"CI_TAGGED": utils.CI_TAGGED,
|
// "CI_TAGGED": utils.CI_TAGGED,
|
||||||
"CI_BUILD": utils.CI_BUILD,
|
// "CI_BUILD": utils.CI_BUILD,
|
||||||
"TotalLinks": db.GetTotalLinks(),
|
// "TotalLinks": db.GetTotalLinks(),
|
||||||
"TotalViews": db.GetTotalViews(),
|
// "TotalViews": db.GetTotalViews(),
|
||||||
})
|
// })
|
||||||
}
|
}
|
||||||
|
|
||||||
fURL := utils.URL + id
|
fURL := utils.URL + id
|
||||||
|
@ -121,14 +121,16 @@ func Posthome(c *fiber.Ctx) error {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.Status(201).Render("views/home", fiber.Map{
|
// return c.Status(201).Render("views/home", fiber.Map{
|
||||||
"URL": fURL,
|
// "URL": fURL,
|
||||||
"CI_COMMIT_SHA": utils.CI_COMMIT_SHA,
|
// "CI_COMMIT_SHA": utils.CI_COMMIT_SHA,
|
||||||
"CI_COMMIT_BRANCH": utils.CI_COMMIT_BRANCH,
|
// "CI_COMMIT_BRANCH": utils.CI_COMMIT_BRANCH,
|
||||||
"CI_COMMIT_TAG": utils.CI_COMMIT_TAG,
|
// "CI_COMMIT_TAG": utils.CI_COMMIT_TAG,
|
||||||
"CI_TAGGED": utils.CI_TAGGED,
|
// "CI_TAGGED": utils.CI_TAGGED,
|
||||||
"CI_BUILD": utils.CI_BUILD,
|
// "CI_BUILD": utils.CI_BUILD,
|
||||||
"TotalLinks": db.GetTotalLinks(),
|
// "TotalLinks": db.GetTotalLinks(),
|
||||||
"TotalViews": db.GetTotalViews(),
|
// "TotalViews": db.GetTotalViews(),
|
||||||
})
|
// })
|
||||||
|
|
||||||
|
return c.SendStatus(200)
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,16 +12,11 @@ var ConfigLimiter limiter.Config = limiter.Config{
|
||||||
Expiration: 60 * time.Second,
|
Expiration: 60 * time.Second,
|
||||||
LimitReached: func(c *fiber.Ctx) error {
|
LimitReached: func(c *fiber.Ctx) error {
|
||||||
if c.Get("content-type") == "application/json" {
|
if c.Get("content-type") == "application/json" {
|
||||||
return c.Status(418).JSON(fiber.Map{
|
return c.Status(429).JSON(fiber.Map{
|
||||||
"msg": "Too many requests, slow down I'm a teapot (10 requests per minute)",
|
"msg": "Too many requests, slow down I'm a teapot (10 requests per minute)",
|
||||||
"success": false,
|
"success": false,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
return c.Render("views/home", fiber.Map{
|
return c.SendStatus(429)
|
||||||
"ERR": "You have reached the limit of requests! Please check back later. (1 minute)",
|
|
||||||
"CI_COMMIT_SHA": CI_COMMIT_SHA,
|
|
||||||
"CI_COMMIT_BRANCH": CI_COMMIT_BRANCH,
|
|
||||||
"CI_BUILD": CI_BUILD,
|
|
||||||
})
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue