cleanup
This commit is contained in:
@ -2,16 +2,31 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
"text/template"
|
||||||
|
"time"
|
||||||
|
|
||||||
"cc.fascinated/paste/internal/config"
|
"cc.fascinated/paste/internal/config"
|
||||||
"cc.fascinated/paste/internal/metrics"
|
"cc.fascinated/paste/internal/metrics"
|
||||||
"cc.fascinated/paste/internal/prisma"
|
"cc.fascinated/paste/internal/prisma"
|
||||||
"cc.fascinated/paste/internal/routes"
|
"cc.fascinated/paste/internal/routes"
|
||||||
|
"github.com/labstack/echo-contrib/echoprometheus"
|
||||||
|
"github.com/labstack/echo/v4"
|
||||||
|
"github.com/labstack/echo/v4/middleware"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type Template struct {
|
||||||
|
templates *template.Template
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *Template) Render(w io.Writer, name string, data interface{}, c echo.Context) error {
|
||||||
|
return t.templates.ExecuteTemplate(w, name, data)
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
fmt.Println("Starting Paste...")
|
fmt.Println("Starting Paste...")
|
||||||
router := routes.NewRouter()
|
|
||||||
|
|
||||||
// Connect to the Prisma database
|
// Connect to the Prisma database
|
||||||
err := prisma.ConnectPrisma()
|
err := prisma.ConnectPrisma()
|
||||||
@ -20,11 +35,68 @@ func main() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
router := echo.New()
|
||||||
|
router.HideBanner = true // Hide the banner
|
||||||
|
|
||||||
|
// Use Gzip Compression
|
||||||
|
router.Use(middleware.GzipWithConfig(middleware.GzipConfig{
|
||||||
|
Skipper: func(c echo.Context) bool {
|
||||||
|
return strings.Contains(c.Path(), "metrics")
|
||||||
|
},
|
||||||
|
}))
|
||||||
|
router.Use(middleware.BodyLimit("1M")) // Limit the body size to 1MB
|
||||||
|
router.Use(echoprometheus.NewMiddleware("paste")) // adds middleware to gather metrics
|
||||||
|
|
||||||
|
// Rate Limiter
|
||||||
|
router.Use(middleware.RateLimiterWithConfig( middleware.RateLimiterConfig{
|
||||||
|
Skipper: func(c echo.Context) bool {
|
||||||
|
if strings.Contains(c.Path(), "metrics") { // Skip rate limiter for metrics
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
if c.Request().Method == http.MethodGet { // Skip rate limiter for GET requests
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
},
|
||||||
|
Store: middleware.NewRateLimiterMemoryStoreWithConfig(
|
||||||
|
middleware.RateLimiterMemoryStoreConfig{Rate: 10, Burst: 30, ExpiresIn: 3 * time.Minute},
|
||||||
|
),
|
||||||
|
IdentifierExtractor: func(ctx echo.Context) (string, error) {
|
||||||
|
id := ctx.RealIP()
|
||||||
|
return id, nil
|
||||||
|
},
|
||||||
|
ErrorHandler: func(context echo.Context, err error) error {
|
||||||
|
return context.JSON(http.StatusForbidden, nil)
|
||||||
|
},
|
||||||
|
DenyHandler: func(context echo.Context, identifier string,err error) error {
|
||||||
|
return context.JSON(http.StatusTooManyRequests, nil)
|
||||||
|
},
|
||||||
|
}))
|
||||||
|
|
||||||
|
// Template Renderer
|
||||||
|
templates := &Template{
|
||||||
|
templates: template.Must(template.ParseGlob("public/views/*.html")),
|
||||||
|
}
|
||||||
|
router.Renderer = templates
|
||||||
|
|
||||||
|
// Middleware Logger
|
||||||
|
router.Use(middleware.LoggerWithConfig(middleware.LoggerConfig{
|
||||||
|
Format: "method=${method}, uri=${uri}, status=${status}\n",
|
||||||
|
}))
|
||||||
|
|
||||||
|
// CORS (Allow all origins)
|
||||||
|
router.Use(middleware.CORSWithConfig(middleware.CORSConfig{
|
||||||
|
AllowOrigins: []string{"*"},
|
||||||
|
AllowMethods: []string{http.MethodGet, http.MethodPost},
|
||||||
|
}))
|
||||||
|
|
||||||
if config.EnableMetrics() { // Check if metrics are enabled
|
if config.EnableMetrics() { // Check if metrics are enabled
|
||||||
metrics.RegisterMetrics() // Register the metrics
|
metrics.RegisterMetrics() // Register the metrics
|
||||||
metrics.InitMetricsUpdater() // Start the metrics updater
|
metrics.InitMetricsUpdater() // Start the metrics updater
|
||||||
}
|
}
|
||||||
|
|
||||||
|
routes.InitRoutes(router) // Initialize the routes
|
||||||
|
|
||||||
port := 8080
|
port := 8080
|
||||||
router.Logger.Fatal(router.Start(fmt.Sprintf(":%d", port)))
|
router.Logger.Fatal(router.Start(fmt.Sprintf(":%d", port)))
|
||||||
}
|
}
|
@ -1,83 +1,13 @@
|
|||||||
package routes
|
package routes
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"html/template"
|
|
||||||
"io"
|
|
||||||
"net/http"
|
|
||||||
"strings"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"cc.fascinated/paste/internal/config"
|
"cc.fascinated/paste/internal/config"
|
||||||
"github.com/labstack/echo-contrib/echoprometheus"
|
"github.com/labstack/echo-contrib/echoprometheus"
|
||||||
"github.com/labstack/echo/v4"
|
"github.com/labstack/echo/v4"
|
||||||
"github.com/labstack/echo/v4/middleware"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Template struct {
|
// InitRoutes initializes the routes for the application.
|
||||||
templates *template.Template
|
func InitRoutes(router *echo.Echo) {
|
||||||
}
|
|
||||||
|
|
||||||
func (t *Template) Render(w io.Writer, name string, data interface{}, c echo.Context) error {
|
|
||||||
return t.templates.ExecuteTemplate(w, name, data)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Creates a new router
|
|
||||||
func NewRouter() *echo.Echo {
|
|
||||||
router := echo.New()
|
|
||||||
router.HideBanner = true // Hide the banner
|
|
||||||
|
|
||||||
// Use Gzip Compression
|
|
||||||
router.Use(middleware.GzipWithConfig(middleware.GzipConfig{
|
|
||||||
Skipper: func(c echo.Context) bool {
|
|
||||||
return strings.Contains(c.Path(), "metrics")
|
|
||||||
},
|
|
||||||
}))
|
|
||||||
router.Use(middleware.BodyLimit("1M")) // Limit the body size to 1MB
|
|
||||||
router.Use(echoprometheus.NewMiddleware("paste")) // adds middleware to gather metrics
|
|
||||||
|
|
||||||
// Rate Limiter
|
|
||||||
router.Use(middleware.RateLimiterWithConfig( middleware.RateLimiterConfig{
|
|
||||||
Skipper: func(c echo.Context) bool {
|
|
||||||
if strings.Contains(c.Path(), "metrics") { // Skip rate limiter for metrics
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
if c.Request().Method == http.MethodGet { // Skip rate limiter for GET requests
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
},
|
|
||||||
Store: middleware.NewRateLimiterMemoryStoreWithConfig(
|
|
||||||
middleware.RateLimiterMemoryStoreConfig{Rate: 10, Burst: 30, ExpiresIn: 3 * time.Minute},
|
|
||||||
),
|
|
||||||
IdentifierExtractor: func(ctx echo.Context) (string, error) {
|
|
||||||
id := ctx.RealIP()
|
|
||||||
return id, nil
|
|
||||||
},
|
|
||||||
ErrorHandler: func(context echo.Context, err error) error {
|
|
||||||
return context.JSON(http.StatusForbidden, nil)
|
|
||||||
},
|
|
||||||
DenyHandler: func(context echo.Context, identifier string,err error) error {
|
|
||||||
return context.JSON(http.StatusTooManyRequests, nil)
|
|
||||||
},
|
|
||||||
}))
|
|
||||||
|
|
||||||
// Template Renderer
|
|
||||||
templates := &Template{
|
|
||||||
templates: template.Must(template.ParseGlob("public/views/*.html")),
|
|
||||||
}
|
|
||||||
router.Renderer = templates
|
|
||||||
|
|
||||||
// Middleware Logger
|
|
||||||
router.Use(middleware.LoggerWithConfig(middleware.LoggerConfig{
|
|
||||||
Format: "method=${method}, uri=${uri}, status=${status}\n",
|
|
||||||
}))
|
|
||||||
|
|
||||||
// CORS (Allow all origins)
|
|
||||||
router.Use(middleware.CORSWithConfig(middleware.CORSConfig{
|
|
||||||
AllowOrigins: []string{"*"},
|
|
||||||
AllowMethods: []string{http.MethodGet, http.MethodPost},
|
|
||||||
}))
|
|
||||||
|
|
||||||
if config.EnableMetrics() { // Check if metrics are enabled
|
if config.EnableMetrics() { // Check if metrics are enabled
|
||||||
router.GET("/metrics", echoprometheus.NewHandler())
|
router.GET("/metrics", echoprometheus.NewHandler())
|
||||||
}
|
}
|
||||||
@ -97,6 +27,4 @@ func NewRouter() *echo.Echo {
|
|||||||
router.GET("/api/paste/:id", getPaste)
|
router.GET("/api/paste/:id", getPaste)
|
||||||
router.POST("/documents", createPaste) // Hastebin compatibility
|
router.POST("/documents", createPaste) // Hastebin compatibility
|
||||||
router.POST("/api/upload", createPaste)
|
router.POST("/api/upload", createPaste)
|
||||||
|
|
||||||
return router
|
|
||||||
}
|
}
|
Reference in New Issue
Block a user