commit 6e222f75296c85b7c3af212be95763f4adcb10e4 Author: hexlocation Date: Wed Dec 25 15:02:06 2024 +0100 yay diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..39f56cd --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.db +gw diff --git a/gw.nimble b/gw.nimble new file mode 100644 index 0000000..1919322 --- /dev/null +++ b/gw.nimble @@ -0,0 +1,17 @@ +# Package + +version = "0.1.0" +author = "hexlocation" +description = "giveaway website for fun <3" +license = "GPL-2.0-only" +srcDir = "src" +bin = @["gw"] + + +# Dependencies + +requires "nim >= 2.2.0" +requires "prologue >= 0.6.6" +requires "sysrandom >= 1.1.0" + +requires "db_connector >= 0.1.0" \ No newline at end of file diff --git a/public/standard.js b/public/standard.js new file mode 100644 index 0000000..7303728 --- /dev/null +++ b/public/standard.js @@ -0,0 +1,16 @@ +async function entry() { + var request = await fetch("/entry", { + headers: { + referral: document.getElementById("referral-other").value + } + }) + var body = await request.json(); + + document.getElementById("referral-key").innerText = body.referral; + document.getElementById("private-key").innerText = body.key; + + if(body.err) { + document.getElementById("lelelelele").innerHTML = `

Do not mess with me.

` + } +} + diff --git a/src/gw.nim b/src/gw.nim new file mode 100644 index 0000000..14a39cf --- /dev/null +++ b/src/gw.nim @@ -0,0 +1,58 @@ +import prologue, prologue/middlewares/staticfile, std/json, sysrandom, asyncdispatch, std/strutils, db_connector/db_sqlite, std/asyncnet + +const key_len = 128 +const refer_len = 16 +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 +)""") + +proc index*(ctx: Context) {.async.} = + for x in db.fastRows(sql"SELECT * FROM keys"): + echo x + await ctx.staticFileResponse("web/index.html", "") + +proc ip_exists*(ip: string): bool = + return len(db.getAllRows(sql"SELECT * FROM keys WHERE ip=?", ip)) != 0 + +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 = + key = "K-" & getRandomString(key_len) + referral = "R-" & getRandomString(refer_len) + db.exec(sql"INSERT INTO keys (referral, key, ip) VALUES (?, ?, ?)", referral, key, ip) + +proc gentry*(ctx: Context) {.async.} = + var key: string = "" + var referral: string = "" + var err: bool = true + if (not ctx.request.hasHeader("referral") or + not referral_exists($ctx.request.getHeader("referral")) or + ip_exists(ctx.request.hostName)) and not debug: + err = true + else: + generate_referral(key,referral, ctx.request.hostName) + err = false + + var info = %* + { + "referral": referral, + "key": key, + "err": err + } + + resp jsonResponse(info) + +var app = newApp() +app.use(staticFileMiddleware("./public")) +app.addRoute("/", index) +app.addRoute("/entry", gentry) +app.run() diff --git a/web/index.html b/web/index.html new file mode 100644 index 0000000..f64789e --- /dev/null +++ b/web/index.html @@ -0,0 +1,30 @@ + + + + +
+

???

+ Referral: + +

+ What is this? +

At exactly 00:00 CET on 1 Jan, the system will choose a random entry. Their referral key shall be publically + displayed on this page. The person associated to this referral key shall contact us with their private key. + They shall win a little prize. To have one's entry weigh more than the rest, one shall have to spread their + referral key amongst the people. Every referral shall increase their chance of getting selected.

+ How does one enter? +

One needs a referral key to enter.

+

+

+
+ + + +