more cleanup
This commit is contained in:
@ -5,27 +5,27 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
// GetMongoConnectionString returns the MongoDB connection string from the environment or a default value.
|
// Returns the MongoDB connection string from the environment or a default value.
|
||||||
func GetMongoConnectionString() string {
|
func GetMongoConnectionString() string {
|
||||||
return getEnv("MONGO_URI", "mongodb://localhost:27017")
|
return getEnv("MONGO_URI", "mongodb://localhost:27017")
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetPasteIDLength returns the paste ID length from the environment or a default value.
|
// Returns the paste ID length from the environment or a default value.
|
||||||
func GetPasteIDLength() int {
|
func GetPasteIDLength() int {
|
||||||
return getIntEnv("PASTE_ID_LENGTH", 12)
|
return getIntEnv("PASTE_ID_LENGTH", 12)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetMaxPasteLength returns the maximum paste length from the environment or a default value.
|
// Returns the maximum paste length from the environment or a default value.
|
||||||
func GetMaxPasteLength() int {
|
func GetMaxPasteLength() int {
|
||||||
return getIntEnv("MAX_PASTE_LENGTH", 5000000)
|
return getIntEnv("MAX_PASTE_LENGTH", 5000000)
|
||||||
}
|
}
|
||||||
|
|
||||||
// EnableMetrics returns whether to enable metrics or not (default: false).
|
// Returns whether to enable metrics or not (default: false).
|
||||||
func EnableMetrics() bool {
|
func EnableMetrics() bool {
|
||||||
return getEnv("ENABLE_METRICS", "false") == "true"
|
return getEnv("ENABLE_METRICS", "false") == "true"
|
||||||
}
|
}
|
||||||
|
|
||||||
// getEnv returns the environment variable value or a default value if not set.
|
// Returns the environment variable value or a default value if not set.
|
||||||
func getEnv(key, defaultValue string) string {
|
func getEnv(key, defaultValue string) string {
|
||||||
if value, exists := os.LookupEnv(key); exists {
|
if value, exists := os.LookupEnv(key); exists {
|
||||||
return value
|
return value
|
||||||
@ -33,7 +33,7 @@ func getEnv(key, defaultValue string) string {
|
|||||||
return defaultValue
|
return defaultValue
|
||||||
}
|
}
|
||||||
|
|
||||||
// getIntEnv returns the integer value of an environment variable or a default value if not set or invalid.
|
// Returns the integer value of an environment variable or a default value if not set or invalid.
|
||||||
func getIntEnv(key string, defaultValue int) int {
|
func getIntEnv(key string, defaultValue int) int {
|
||||||
valueStr := getEnv(key, strconv.Itoa(defaultValue))
|
valueStr := getEnv(key, strconv.Itoa(defaultValue))
|
||||||
value, err := strconv.Atoi(valueStr)
|
value, err := strconv.Atoi(valueStr)
|
||||||
|
@ -2,7 +2,9 @@ package errors
|
|||||||
|
|
||||||
import "errors"
|
import "errors"
|
||||||
|
|
||||||
var ErrPasteTooLarge = errors.New("paste is too large")
|
var (
|
||||||
var ErrUnableToGeneratePasteKey = errors.New("unable to generate paste key")
|
ErrPasteTooLarge = errors.New("paste is too large")
|
||||||
var ErrUnableToConnectToDatabase = errors.New("unable to connect to database")
|
ErrUnableToGeneratePasteKey = errors.New("unable to generate paste key")
|
||||||
var ErrInvalidPasteID = errors.New("invalid paste id")
|
ErrUnableToConnectToDatabase = errors.New("unable to connect to database")
|
||||||
|
ErrInvalidPasteID = errors.New("invalid paste id")
|
||||||
|
)
|
@ -29,6 +29,7 @@ func InitMetricsUpdater() {
|
|||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Updates the metrics
|
||||||
func updateMetrics() {
|
func updateMetrics() {
|
||||||
fmt.Println("Updating metrics...")
|
fmt.Println("Updating metrics...")
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
@ -13,6 +13,7 @@ import (
|
|||||||
stringUtils "cc.fascinated/paste/internal/utils"
|
stringUtils "cc.fascinated/paste/internal/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Gets a paste by its ID
|
||||||
func GetPaste(id string) (*db.PasteModel, error) {
|
func GetPaste(id string) (*db.PasteModel, error) {
|
||||||
fmt.Printf("Getting paste \"%s\"...\n", id)
|
fmt.Printf("Getting paste \"%s\"...\n", id)
|
||||||
before := time.Now()
|
before := time.Now()
|
||||||
|
@ -6,7 +6,7 @@ import (
|
|||||||
"github.com/labstack/echo/v4"
|
"github.com/labstack/echo/v4"
|
||||||
)
|
)
|
||||||
|
|
||||||
// InitRoutes initializes the routes for the application.
|
// Initializes the routes for the application.
|
||||||
func InitRoutes(router *echo.Echo) {
|
func InitRoutes(router *echo.Echo) {
|
||||||
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())
|
||||||
|
@ -7,6 +7,7 @@ import (
|
|||||||
"github.com/labstack/echo/v4"
|
"github.com/labstack/echo/v4"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Renders the paste content
|
||||||
func renderPaste(c echo.Context) error {
|
func renderPaste(c echo.Context) error {
|
||||||
// Get the paste ID
|
// Get the paste ID
|
||||||
id := c.Param("id")
|
id := c.Param("id")
|
||||||
@ -29,6 +30,7 @@ func renderPaste(c echo.Context) error {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Renders the raw paste content
|
||||||
func renderPasteRaw(c echo.Context) error {
|
func renderPasteRaw(c echo.Context) error {
|
||||||
// Get the paste ID
|
// Get the paste ID
|
||||||
id := c.Param("id")
|
id := c.Param("id")
|
||||||
|
Reference in New Issue
Block a user