2021-12-08 15:31:52 +00:00
|
|
|
package routes
|
|
|
|
|
|
|
|
import (
|
2022-01-08 16:21:53 +00:00
|
|
|
"log"
|
2021-12-08 15:31:52 +00:00
|
|
|
"regexp"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"git.ucode.space/Phil/goshorly/db"
|
|
|
|
"git.ucode.space/Phil/goshorly/utils"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
gonanoid "github.com/matoous/go-nanoid/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
type eurl struct {
|
2021-12-10 21:38:02 +00:00
|
|
|
CLI bool `json:"cli"`
|
|
|
|
URL string `json:"surl" form:"surl"`
|
2021-12-08 15:31:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Posthome(c *fiber.Ctx) error {
|
|
|
|
u := new(eurl)
|
|
|
|
if err := c.BodyParser(u); err != nil {
|
2021-12-10 21:38:02 +00:00
|
|
|
|
|
|
|
if u.CLI {
|
|
|
|
return c.Status(500).JSON(&fiber.Map{
|
|
|
|
"success": false,
|
|
|
|
"msg": "Parsing Error",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-12-08 15:50:22 +00:00
|
|
|
return c.Status(500).Render("views/home", fiber.Map{
|
2022-01-11 11:05:07 +00:00
|
|
|
"ERR": "Parsing Error",
|
|
|
|
"CI_COMMIT_SHORT_SHA": utils.CI_COMMIT_SHORT_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(),
|
2021-12-08 15:31:52 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if !regexp.MustCompile(`^(http|https|mailto|ts3server)://`).MatchString(u.URL) {
|
2021-12-10 21:38:02 +00:00
|
|
|
|
|
|
|
if u.CLI {
|
|
|
|
return c.Status(424).JSON(&fiber.Map{
|
|
|
|
"success": false,
|
|
|
|
"msg": "Invalid URL",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-12-08 15:50:22 +00:00
|
|
|
return c.Status(424).Render("views/home", fiber.Map{
|
2022-01-11 11:05:07 +00:00
|
|
|
"ERR": "Invalid URL, please check and try again.",
|
|
|
|
"CI_COMMIT_SHORT_SHA": utils.CI_COMMIT_SHORT_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(),
|
2021-12-08 15:31:52 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
id, err := gonanoid.New(8)
|
|
|
|
|
|
|
|
if err != nil {
|
2021-12-10 21:38:02 +00:00
|
|
|
|
|
|
|
if u.CLI {
|
|
|
|
return c.Status(500).JSON(&fiber.Map{
|
|
|
|
"success": false,
|
|
|
|
"msg": err.Error(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-12-08 15:50:22 +00:00
|
|
|
return c.Status(500).Render("views/home", fiber.Map{
|
2022-01-11 11:05:07 +00:00
|
|
|
"ERR": err.Error(),
|
|
|
|
"CI_COMMIT_SHORT_SHA": utils.CI_COMMIT_SHORT_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(),
|
2021-12-08 15:31:52 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
err = db.Client.Set(id, u.URL, 1296000*time.Second).Err()
|
|
|
|
|
|
|
|
if err != nil {
|
2021-12-10 21:38:02 +00:00
|
|
|
if u.CLI {
|
|
|
|
return c.Status(500).JSON(&fiber.Map{
|
|
|
|
"success": false,
|
|
|
|
"msg": err.Error(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-12-08 15:50:22 +00:00
|
|
|
return c.Status(500).Render("views/home", fiber.Map{
|
2022-01-11 11:05:07 +00:00
|
|
|
"ERR": err.Error(),
|
|
|
|
"CI_COMMIT_SHORT_SHA": utils.CI_COMMIT_SHORT_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(),
|
2021-12-08 15:31:52 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fURL := utils.URL + id
|
|
|
|
|
2022-01-08 16:21:53 +00:00
|
|
|
// Increase Total Links into the DB
|
|
|
|
_, err = db.StatsIncreaseTotalLinks()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err.Error())
|
|
|
|
}
|
|
|
|
|
2021-12-10 21:38:02 +00:00
|
|
|
if u.CLI {
|
|
|
|
return c.Status(201).JSON(&fiber.Map{
|
|
|
|
"success": true,
|
|
|
|
"URL": fURL,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-12-08 15:50:22 +00:00
|
|
|
return c.Status(201).Render("views/home", fiber.Map{
|
2022-01-11 11:05:07 +00:00
|
|
|
"URL": fURL,
|
|
|
|
"CI_COMMIT_SHORT_SHA": utils.CI_COMMIT_SHORT_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(),
|
2021-12-08 15:31:52 +00:00
|
|
|
})
|
|
|
|
}
|