This commit is contained in:
hexlocation 2024-12-25 15:02:06 +01:00
commit 6e222f7529
Signed by: hex
GPG key ID: A19EFFAAF8C00FCF
5 changed files with 123 additions and 0 deletions

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
*.db
gw

17
gw.nimble Normal file
View file

@ -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"

16
public/standard.js Normal file
View file

@ -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 = `<h1 style="font-weight:bold;color:red">Do not mess with me.</h1>`
}
}

58
src/gw.nim Normal file
View file

@ -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()

30
web/index.html Normal file
View file

@ -0,0 +1,30 @@
<script src="/public/standard.js"></script>
<html>
<body>
<div id="lelelelele">
<h1>???</h1>
Referral: <input id="referral-other" type="text"></input>
<button onclick="entry()">Create entry.</button>
<br><br>
<b>What is this?</b>
<p>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.</p>
<b>How does one enter?</b>
<p>One needs a referral key to enter.</p>
<p id="referral-key"></p>
<p id="private-key"></p>
</div>
</body>
</html>
<style>
body {
background-color: black;
color: #4af626;
text-align: center;
font-family: sans-serif;
}
</style>