Forget-Me-Not/helpers/global.nim

91 lines
2.6 KiB
Nim
Raw Normal View History

2025-05-15 08:01:35 -05:00
#import std/[times, logging]
import std/[times, strutils, re, uri, paths, random, strtabs]
#universal
const APP_PATH* = "/var/www/forget-me-not-gs"
const ASSETS_PATH* = "/var/www/forget-me-not-gs/app/assets"
const APP_NAME* = "Forget-Me-Not"
const APP_MODE* = "dev"
#dev
const APP_URL* = "http://fmn-gs"
#const SITE_URL* = "http://"
const ASSETS_URL* = "http://assets.fmn-gs"
#prod
#const APP_URL* = "https://ssm.itwrx.org"
#const SITE_URL* = "https://itwrx.org"
#const ASSETS_URL* = "https://assets.itwrx.org"
var frStrTab* = newStringTable()
#Guildensterns logger is conflicting with my, evidently incorrect, usage of the std lib logger so i'll just write some lines to a file for now.
#var logger* = newFileLogger("errors.log")
let dt = now()
let nowDT* = dt.format("M-d-YYYY h:mm:ss tt")
proc writeLogLine*(errorMsg: string) =
{.gcsafe.}:
let logFile = open("errors.log", fmAppend)
defer: logFile.close()
logFile.writeLine(errorMsg)
#template location*(slug: string, csrfToken: string, fr: FormResult): untyped =
template location*(slug: string): untyped =
"location: " & APP_URL & slug
template locationBack*(): string =
"location: " & http.headers.getOrDefault("referer")
template locationOrigin*(origin: string): untyped =
"location: " & origin
proc filenameToSentence*(filename: string): string =
#remove file extension.
let filePath = Path filename
let filePathEnum = splitFile(filePath)
var name = filePathEnum[1].string
#replace dashes and underscores with spaces.
name = name.replace(re"_", " ")
name = name.replace(re"-", " ")
#strip numbers.
name = name.replace(re"[0-9]", "")
return name
proc titleToSlug*(title: string): string =
#replace one or more spaces with dash
var dataString = title.replace(re" +", "-")
#replace anything that is not a letter, number or underscore with nothing.
dataString = dataString.replace(re"[^a-zA-Z0-9-]", "")
#convert to all lowercase.
dataString = dataString.toLowerAscii()
return dataString
proc getIdFromURI*(uri: string): int =
#let parsedUri = parseUri(uri)
let pathSeq = parseUri(uri).path.split('/')
result = strutils.parseInt(pathSeq[2])
proc parseIntIf*(input: string): int =
if input.len > 0:
return parseInt(input)
else:
return 0
proc parseFloatIf*(input: string): float =
if input.len > 0:
return parseFloat(input)
else:
return 0.0
#not cryptographically secure.
proc rndStr20*(): string =
for _ in 0..20:
add(result, char(rand(int('A') .. int('z'))))
proc boolToInt*(myBool: bool): int =
if myBool == true:
return 1
else:
return 0