This repository has been archived by the owner on May 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cerosnapshot.v
84 lines (72 loc) · 1.55 KB
/
cerosnapshot.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
module main
import vweb
import sqlite
import net.http
import x.json2
import os
import rand
struct App {
vweb.Context
pub mut:
db sqlite.DB
}
fn main() {
db_path := os.getenv_opt("DB_PATH") or { ":memory:" }
mut app := App{
db: sqlite.connect(db_path) or { panic(err) }
}
sql app.db {
create table Friend
}
vweb.run_at(app, vweb.RunParams{
port: 8081,
host: os.getenv("WEB_HOST")
family: .ip,
}) or { panic(err) }
}
['/index']
pub fn (mut app App) index() ?vweb.Result {
return $vweb.html()
}
[post]
pub fn (mut app App) new_friend(name string) ?vweb.Result {
if name == '' {
return app.text('How rude.')
}
ip := app.ip().split(":")[0]
mut request := http.Request{
url: 'https://json.geoiplookup.io/$ip'
method: .get
}
result := request.do() ?
response := json2.raw_decode(result.text) ?.as_map()
mut lon := response["longitude"] or { "" }.f64()
mut lat := response["latitude"] or { "" }.f64()
// I do hope no one lives in Sao Tome ;)
if lon != 0 && lat != 0 {
// add some random noise to offset IP geolocation being concentrated in provider locations
lat += rand.f64()*0.6-0.3
lon += rand.f64()*0.6-0.3
}
friend := Friend{
name: name
lon: lon
lat: lat
}
sql app.db {
insert friend into Friend
}
return app.redirect('/friends')
}
['/friends']
pub fn (mut app App) friends() vweb.Result {
friends := app.find_all_friends()
located_friends := friends.filter(it.lon != 0 && it.lat != 0)
visitors := friends.map(it.name).join(", ")
myself := Friend {
name: "myself"
lat: 51.0771
lon: 17.0
}
return $vweb.html()
}