8/src/hh1.nim
2024-08-16 01:49:10 +05:30

113 lines
No EOL
3.1 KiB
Nim

import
mike,
nimja,
strutils,
json,
strformat,
httpClient,
dotenv,
./lib/[mics, htmx],
./db/users,
./model/[model, rand],
./controller/[signup, login, htmx]
load()
"/^path" -> before[post, get]:
var path2 = ctx.pathParams["path"]
echo(fmt"[mike] before request: {ctx.httpMethod} {path2}")
"/" -> [get, post]:
var
user1 = ctx.cookies.getOrDefault("user1", "")
user = getUserFromCookie(user1)
echo user.firstName
compileTemplateFile("view" / "index.html", baseDir = getScriptDir())
"/shop" -> get:
var
user1 = ctx.cookies.getOrDefault("user1", "")
user = getUserFromCookie(user1)
compileTemplateFile("view" / "shop.html", baseDir = getScriptDir())
"/contact" -> get:
var
user1 = ctx.cookies.getOrDefault("user1", "")
user = getUserFromCookie(user1)
compileTemplateFile("view" / "contact.html", baseDir = getScriptDir())
"/cart" -> get:
var
user1 = ctx.cookies.getOrDefault("user1", "")
user = getUserFromCookie(user1)
compileTemplateFile("view" / "cart.html", baseDir = getScriptDir())
"/checkout" -> get:
var
user1 = ctx.cookies.getOrDefault("user1", "")
user = getUserFromCookie(user1)
compileTemplateFile("view" / "checkout.html", baseDir = getScriptDir())
"/shop/:id" -> get:
var
prodId = capitalizeAscii(ctx.pathParams["id"])
user1 = ctx.cookies.getOrDefault("user1", "")
user = getUserFromCookie(user1)
compileTemplateFile("view" / "shop-detail.html", baseDir = getScriptDir())
"/testimonial" -> get:
var
user1 = ctx.cookies.getOrDefault("user1", "")
user = getUserFromCookie(user1)
compileTemplateFile("view" / "testimonial.html", baseDir = getScriptDir())
"/signup" -> [get, post]: ctx.signup()
"/login" -> get:
var
user1 = ctx.cookies.getOrDefault("user1", "")
user = getUserFromCookie(user1)
compileTemplateFile("view" / "login.html", baseDir = getScriptDir())
"/htmx/login" -> post: ctx.htmxLogin()
"/htmx/email" -> post: htmxEmail(ctx)
"/htmx/password" -> post: htmxPassword(ctx)
"/:path" -> get:
var
user1 = ctx.cookies.getOrDefault("user1", "")
user = getUserFromCookie(user1)
compileTemplateFile("view" / "404.html", baseDir = getScriptDir())
# Experimental
"/hi" -> get:
var
user1 = ctx.cookies.getOrDefault("user1", "")
user = getUserFromCookie(user1)
uploadcare_pub_key = getEnv("UPLOAD_CARE_PUBLIC_KEY")
compileTemplateFile("view" / "hi.html", baseDir = getScriptDir())
"/delete-hi" -> post:
var
client = newHttpClient()
uploadcare_pub_key = getEnv("UPLOAD_CARE_PUBLIC_KEY")
uploadcare_secret_key = getEnv("UPLOAD_CARE_SECRET_KEY")
client.headers = newHttpHeaders({"Authorization": fmt"Uploadcare.Simple {uploadcare_pub_key}:{uploadcare_secret_key}"})
var form = ctx.urlForm
echo form
var img_uuid = form["img1"]
echo img_uuid
echo fmt"https://api.uploadcare.com/files/{img_uuid}/storage/"
try:
let response = client.request(fmt"https://api.uploadcare.com/files/{img_uuid}/storage/", httpMethod = HttpDelete)
echo response.status
finally:
client.close()
var js = parseJson("""{"status": "success"}""")
ctx.send(js)
servePublic("src/public", "/static")
run()