hello world
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

This commit is contained in:
hexlocation 2024-12-28 21:25:22 +01:00
parent 5f54220113
commit 2b69254416
Signed by: hex
GPG key ID: A19EFFAAF8C00FCF

View file

@ -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()
app.run()