8/src/hh1.nim

145 lines
4.1 KiB
Nim
Raw Permalink Normal View History

2024-07-19 17:51:48 +02:00
import
2024-08-15 22:19:10 +02:00
mike,
2024-07-19 19:58:22 +02:00
nimja,
2024-07-20 16:31:35 +02:00
strutils,
json,
2024-07-20 18:16:38 +02:00
strformat,
2024-08-14 20:04:43 +02:00
httpClient,
dotenv,
2024-08-07 22:29:42 +02:00
./lib/[mics, htmx],
2024-07-20 16:31:35 +02:00
./db/users,
2024-08-07 22:50:33 +02:00
./model/[model, rand],
2024-10-16 21:47:43 +02:00
./controller/[signup, login, htmx, admin]
2024-08-14 20:04:43 +02:00
load()
2024-07-19 17:36:17 +02:00
2024-07-20 18:16:38 +02:00
"/^path" -> before[post, get]:
var path2 = ctx.pathParams["path"]
echo(fmt"[mike] before request: {ctx.httpMethod} {path2}")
2024-07-20 16:31:35 +02:00
"/" -> [get, post]:
2024-08-07 20:32:25 +02:00
var
2024-08-19 21:20:05 +02:00
user1 = ctx.cookies.getOrDefault("user", "")
2024-08-10 22:53:25 +02:00
user = getUserFromCookie(user1)
echo user.firstName
2024-08-15 22:19:10 +02:00
compileTemplateFile("view" / "index.html", baseDir = getScriptDir())
2024-07-19 17:36:17 +02:00
2024-07-19 18:39:21 +02:00
"/shop" -> get:
2024-08-10 11:32:40 +02:00
var
2024-08-19 21:20:05 +02:00
user1 = ctx.cookies.getOrDefault("user", "")
2024-08-10 22:53:25 +02:00
user = getUserFromCookie(user1)
2024-08-15 22:19:10 +02:00
compileTemplateFile("view" / "shop.html", baseDir = getScriptDir())
2024-07-19 18:39:21 +02:00
"/contact" -> get:
2024-08-10 11:32:40 +02:00
var
2024-08-19 21:20:05 +02:00
user1 = ctx.cookies.getOrDefault("user", "")
2024-08-10 22:53:25 +02:00
user = getUserFromCookie(user1)
2024-08-15 22:19:10 +02:00
compileTemplateFile("view" / "contact.html", baseDir = getScriptDir())
2024-07-19 18:39:21 +02:00
"/cart" -> get:
2024-08-10 11:32:40 +02:00
var
2024-08-19 21:20:05 +02:00
user1 = ctx.cookies.getOrDefault("user", "")
2024-08-10 22:53:25 +02:00
user = getUserFromCookie(user1)
2024-08-15 22:19:10 +02:00
compileTemplateFile("view" / "cart.html", baseDir = getScriptDir())
2024-07-19 18:39:21 +02:00
2024-07-19 19:25:49 +02:00
"/checkout" -> get:
2024-08-10 11:32:40 +02:00
var
2024-08-19 21:20:05 +02:00
user1 = ctx.cookies.getOrDefault("user", "")
2024-08-10 22:53:25 +02:00
user = getUserFromCookie(user1)
2024-08-15 22:19:10 +02:00
compileTemplateFile("view" / "checkout.html", baseDir = getScriptDir())
2024-07-19 19:25:49 +02:00
2024-07-19 19:58:22 +02:00
"/shop/:id" -> get:
2024-08-09 12:00:15 +02:00
var
prodId = capitalizeAscii(ctx.pathParams["id"])
2024-08-19 21:20:05 +02:00
user1 = ctx.cookies.getOrDefault("user", "")
2024-08-10 22:53:25 +02:00
user = getUserFromCookie(user1)
2024-08-15 22:19:10 +02:00
compileTemplateFile("view" / "shop-detail.html", baseDir = getScriptDir())
2024-07-19 19:25:49 +02:00
"/testimonial" -> get:
2024-08-10 11:32:40 +02:00
var
2024-08-19 21:20:05 +02:00
user1 = ctx.cookies.getOrDefault("user", "")
2024-08-10 22:53:25 +02:00
user = getUserFromCookie(user1)
2024-08-15 22:19:10 +02:00
compileTemplateFile("view" / "testimonial.html", baseDir = getScriptDir())
2024-07-19 19:25:49 +02:00
2024-08-10 22:53:25 +02:00
"/signup" -> [get, post]: ctx.signup()
2024-07-20 14:26:19 +02:00
"/login" -> get:
2024-08-10 11:32:40 +02:00
var
2024-08-19 21:20:05 +02:00
user1 = ctx.cookies.getOrDefault("user", "")
2024-08-10 22:53:25 +02:00
user = getUserFromCookie(user1)
2024-08-15 22:19:10 +02:00
compileTemplateFile("view" / "login.html", baseDir = getScriptDir())
2024-10-16 22:06:21 +02:00
"/logout" -> get:
ctx &= initCookie("user", "")
ctx.redirect("/")
2024-08-07 22:29:42 +02:00
2024-08-14 20:04:43 +02:00
"/htmx/login" -> post: ctx.htmxLogin()
2024-08-08 19:19:24 +02:00
"/htmx/email" -> post: htmxEmail(ctx)
2024-08-08 19:19:24 +02:00
"/htmx/password" -> post: htmxPassword(ctx)
2024-07-23 22:53:43 +02:00
"/htmx/fname" -> post: htmxFirstName(ctx)
"/htmx/lname" -> post: htmxLastName(ctx)
"/htmx/phone" -> post: htmxPhone(ctx)
2024-08-05 20:54:09 +02:00
"/:path" -> get:
2024-08-10 11:32:40 +02:00
var
2024-08-19 21:20:05 +02:00
user1 = ctx.cookies.getOrDefault("user", "")
2024-08-10 22:53:25 +02:00
user = getUserFromCookie(user1)
2024-08-15 22:19:10 +02:00
compileTemplateFile("view" / "404.html", baseDir = getScriptDir())
2024-08-05 20:54:09 +02:00
2024-08-15 21:57:47 +02:00
# Experimental
2024-10-16 00:01:30 +02:00
"/hi" -> get:
var
user1 = ctx.cookies.getOrDefault("user", "")
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)
2024-08-14 20:04:43 +02:00
2024-08-19 21:51:21 +02:00
"/admin" -> get:
compileTemplateFile("view" / "admin" / "index.html", baseDir = getScriptDir())
2024-10-16 21:47:43 +02:00
"/admin/add-product" -> [get, post]: ctx.adminAddProduct()
2024-10-16 00:01:30 +02:00
"/htmx/admin/product-name" -> post: ctx.htmxProductName()
2024-10-16 00:17:13 +02:00
"/htmx/admin/description" -> post: ctx.htmxProductDescription()
"/htmx/admin/product-price" -> post: ctx.htmxProductPrice()
"/htmx/admin/product-quantity" -> post: ctx.htmxProductQuantity()
2024-10-16 22:50:26 +02:00
"/admin/add-image" -> get:
var uploadcare_pub_key = getEnv("UPLOAD_CARE_PUBLIC_KEY")
compileTemplateFile("view" / "admin" / "addimage.html", baseDir = getScriptDir())
"/jj" -> post:
var form = ctx.urlForm
echo form
var img_uuid = form["img1"]
echo img_uuid
var js = parseJson(fmt"""{{"status": "success", "img_uuid": "{img_uuid}"}}""")
ctx.send(js)
2024-07-19 17:51:48 +02:00
servePublic("src/public", "/static")
2024-08-19 21:20:05 +02:00
run()