From 2b692544167192cd10d4b1198ac7e8a3be6663ae Mon Sep 17 00:00:00 2001 From: hex Date: Sat, 28 Dec 2024 21:25:22 +0100 Subject: [PATCH] hello world --- src/gw.nim | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/src/gw.nim b/src/gw.nim index 7af20e9..29f72f2 100644 --- a/src/gw.nim +++ b/src/gw.nim @@ -2,15 +2,16 @@ import prologue, prologue/middlewares/staticfile, prologue/middlewares/cors, std const key_len = 128 const refer_len = 16 -const debug = true +const debug = false let db = open("real.db", "", "", "") db.exec( sql"""CREATE TABLE IF NOT EXISTS keys( - referral VARCHAR(255) NOT NULL, - key VARCHAR(255) NOT NULL, - ip VARCHAR(255) NOT NULL + referred_by VARCHAR(255) NOT NULL, + referral VARCHAR(255) NOT NULL, + key VARCHAR(255) NOT NULL, + ip VARCHAR(255) NOT NULL )""") proc index*(ctx: Context) {.async.} = @@ -25,21 +26,27 @@ proc referral_exists*(refer: string): bool = let results = db.getAllRows(sql"SELECT * FROM keys WHERE referral=?", refer) return len(results) != 0 -proc generate_referral*(key, referral: var string, ip: string): void = +proc table_empty*(): bool = + return len(db.getAllRows(sql"SELECT * FROM keys")) == 0 + +proc generate_referral*(key, referral: var string, ip: string, referred_by: string): void = key = "K-" & getRandomString(key_len) referral = "R-" & getRandomString(refer_len) - db.exec(sql"INSERT INTO keys (referral, key, ip) VALUES (?, ?, ?)", referral, key, ip) + db.exec(sql"INSERT INTO keys (referral, key, ip, referred_by) VALUES (?, ?, ?, ?)", referral, key, ip, referred_by) proc gentry*(ctx: Context) {.async.} = var key: string = "" var referral: string = "" var err: bool = true - if (not ctx.request.hasHeader("referral") or + + echo table_empty() + + if ((not ctx.request.hasHeader("referral") or not referral_exists($ctx.request.getHeader("referral")) or - ip_exists(ctx.request.hostName)) and not debug: + ip_exists(ctx.request.hostName)) and not table_empty()) and not debug: err = true else: - generate_referral(key,referral, ctx.request.hostName) + generate_referral(key,referral, ctx.request.hostName, $ctx.request.getHeader("header")) err = false var info = %* @@ -57,4 +64,4 @@ app.use(CorsMiddleware(allowOrigins = @["*"], allowHeaders = @["*"], allowMethod app.use(staticFileMiddleware("./public")) app.addRoute("/", index) app.addRoute("/entry", gentry) -app.run() \ No newline at end of file +app.run()