diff --git a/db/connection.go b/db/connection.go new file mode 100644 index 0000000..b59dbdc --- /dev/null +++ b/db/connection.go @@ -0,0 +1,21 @@ +package db + +import ( + "log" + + "github.com/go-redis/redis" +) + +var Client *redis.Client = redis.NewClient(&redis.Options{ + Addr: "redis:6379", + Password: "", + DB: 0, +}) + +func Init_redis() { + _, err := Client.Ping().Result() + + if err != nil { + log.Fatal(err.Error()) + } +} diff --git a/main.go b/main.go index f096f98..30b8be8 100644 --- a/main.go +++ b/main.go @@ -4,15 +4,13 @@ import ( "embed" "log" "net/http" - "regexp" - "time" + "git.ucode.space/Phil/goshorly/db" + "git.ucode.space/Phil/goshorly/routes" "git.ucode.space/Phil/goshorly/utils" - "github.com/go-redis/redis" "github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2/middleware/limiter" "github.com/gofiber/template/html" - gonanoid "github.com/matoous/go-nanoid/v2" ) //go:embed views/* @@ -23,112 +21,22 @@ func main() { utils.Init_env_vars() utils.Init_build_vars() + db.Init_redis() + engine := html.NewFileSystem(http.FS(viewsfs), ".html") app := fiber.New(fiber.Config{ CaseSensitive: true, - ServerHeader: "goshorly", Views: engine, }) - client := redis.NewClient(&redis.Options{ - Addr: "redis:6379", - Password: "", - DB: 0, - }) + app.Get("/", routes.Gethome) - _, err := client.Ping().Result() + app.Get("/:id", routes.ID) - if err != nil { - log.Fatal(err) - } + app.Use(limiter.New(utils.ConfigLimiter)) - app.Get("/", func(c *fiber.Ctx) error { - return c.Render("views/home", fiber.Map{ - "GitCommitShort": utils.GitCommitShort, - "GitBranch": utils.GitBranch, - "GitBuild": utils.GitBuild, - }) - }) - - type EUrl struct { - URL string `form:"surl"` - } - - app.Get("/:id", func(c *fiber.Ctx) error { - val, err := client.Get(c.Params("id")).Result() - if err != nil { - return c.Render("views/404", fiber.Map{ - "BASEURL": utils.URL, - }) - } - return c.Redirect(val) - }) - - app.Use(limiter.New(limiter.Config{ - Max: 10, - Expiration: 60 * time.Second, - LimitReached: func(c *fiber.Ctx) error { - return c.Render("views/home", fiber.Map{ - "ERR": "You have reached the limit of requests! Please check back later. (1 minute)", - "GitCommitShort": utils.GitCommitShort, - "GitBranch": utils.GitBranch, - "GitBuild": utils.GitBuild, - }) - }, - })) - - app.Post("/", func(c *fiber.Ctx) error { - u := new(EUrl) - if err := c.BodyParser(u); err != nil { - return c.Render("views/home", fiber.Map{ - "ERR": "Parsing Error", - "GitCommitShort": utils.GitCommitShort, - "GitBranch": utils.GitBranch, - "GitBuild": utils.GitBuild, - }) - } - - if !regexp.MustCompile(`^(http|https|mailto|ts3server)://`).MatchString(u.URL) { - return c.Render("views/home", fiber.Map{ - "ERR": "Invalid URL, please check and try again.", - "GitCommitShort": utils.GitCommitShort, - "GitBranch": utils.GitBranch, - "GitBuild": utils.GitBuild, - }) - } - - id, err := gonanoid.New(8) - - if err != nil { - return c.Render("views/home", fiber.Map{ - "ERR": err.Error(), - "GitCommitShort": utils.GitCommitShort, - "GitBranch": utils.GitBranch, - "GitBuild": utils.GitBuild, - }) - } - - err = client.Set(id, u.URL, 1296000*time.Second).Err() - - if err != nil { - return c.Render("views/home", fiber.Map{ - "ERR": err.Error(), - "GitCommitShort": utils.GitCommitShort, - "GitBranch": utils.GitBranch, - "GitBuild": utils.GitBuild, - }) - } - - fURL := utils.URL + id - - return c.Render("views/home", fiber.Map{ - "URL": fURL, - "GitCommitShort": utils.GitCommitShort, - "GitBranch": utils.GitBranch, - "GitBuild": utils.GitBuild, - }) - }) + app.Post("/", routes.Posthome) log.Fatal(app.Listen(":" + utils.PORT)) } diff --git a/routes/gethome.go b/routes/gethome.go new file mode 100644 index 0000000..e35997d --- /dev/null +++ b/routes/gethome.go @@ -0,0 +1,14 @@ +package routes + +import ( + "git.ucode.space/Phil/goshorly/utils" + "github.com/gofiber/fiber/v2" +) + +func Gethome(c *fiber.Ctx) error { + return c.Render("views/home", fiber.Map{ + "GitCommitShort": utils.GitCommitShort, + "GitBranch": utils.GitBranch, + "GitBuild": utils.GitBuild, + }) +} diff --git a/routes/id.go b/routes/id.go new file mode 100644 index 0000000..27facd2 --- /dev/null +++ b/routes/id.go @@ -0,0 +1,17 @@ +package routes + +import ( + "git.ucode.space/Phil/goshorly/db" + "git.ucode.space/Phil/goshorly/utils" + "github.com/gofiber/fiber/v2" +) + +func ID(c *fiber.Ctx) error { + val, err := db.Client.Get(c.Params("id")).Result() + if err != nil { + return c.Render("views/404", fiber.Map{ + "BASEURL": utils.URL, + }) + } + return c.Redirect(val) +} diff --git a/routes/posthome.go b/routes/posthome.go new file mode 100644 index 0000000..eaa774d --- /dev/null +++ b/routes/posthome.go @@ -0,0 +1,67 @@ +package routes + +import ( + "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 { + URL string `form:"surl"` +} + +func Posthome(c *fiber.Ctx) error { + u := new(eurl) + if err := c.BodyParser(u); err != nil { + return c.Render("views/home", fiber.Map{ + "ERR": "Parsing Error", + "GitCommitShort": utils.GitCommitShort, + "GitBranch": utils.GitBranch, + "GitBuild": utils.GitBuild, + }) + } + + if !regexp.MustCompile(`^(http|https|mailto|ts3server)://`).MatchString(u.URL) { + return c.Render("views/home", fiber.Map{ + "ERR": "Invalid URL, please check and try again.", + "GitCommitShort": utils.GitCommitShort, + "GitBranch": utils.GitBranch, + "GitBuild": utils.GitBuild, + }) + } + + id, err := gonanoid.New(8) + + if err != nil { + return c.Render("views/home", fiber.Map{ + "ERR": err.Error(), + "GitCommitShort": utils.GitCommitShort, + "GitBranch": utils.GitBranch, + "GitBuild": utils.GitBuild, + }) + } + + err = db.Client.Set(id, u.URL, 1296000*time.Second).Err() + + if err != nil { + return c.Render("views/home", fiber.Map{ + "ERR": err.Error(), + "GitCommitShort": utils.GitCommitShort, + "GitBranch": utils.GitBranch, + "GitBuild": utils.GitBuild, + }) + } + + fURL := utils.URL + id + + return c.Render("views/home", fiber.Map{ + "URL": fURL, + "GitCommitShort": utils.GitCommitShort, + "GitBranch": utils.GitBranch, + "GitBuild": utils.GitBuild, + }) +} diff --git a/utils/env.go b/utils/env.go index 3eeec85..09e955e 100644 --- a/utils/env.go +++ b/utils/env.go @@ -43,11 +43,9 @@ func Init_env_vars() { PORT = UPORT } - UREDIS_URI, err := os.LookupEnv("REDIS_URI") - if !err { + UREDIS_URI, _ := os.LookupEnv("REDIS_URI") + if UREDIS_URI != "" { REDIS_URI = "redis" - } else { - REDIS_URI = UREDIS_URI } create_string() diff --git a/utils/limiter.go b/utils/limiter.go new file mode 100644 index 0000000..f4f003a --- /dev/null +++ b/utils/limiter.go @@ -0,0 +1,21 @@ +package utils + +import ( + "time" + + "github.com/gofiber/fiber/v2" + "github.com/gofiber/fiber/v2/middleware/limiter" +) + +var ConfigLimiter limiter.Config = limiter.Config{ + Max: 10, + Expiration: 60 * time.Second, + LimitReached: func(c *fiber.Ctx) error { + return c.Render("views/home", fiber.Map{ + "ERR": "You have reached the limit of requests! Please check back later. (1 minute)", + "GitCommitShort": GitCommitShort, + "GitBranch": GitBranch, + "GitBuild": GitBuild, + }) + }, +}