-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.sh
executable file
·71 lines (56 loc) · 1.24 KB
/
server.sh
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
#!/usr/bin/env bash
set -e
path=
count=10
radius=500
lat=0
lng=0
function parse_http_request {
# an http (v 1.1) request looks like this:
#
# METHOD /path HTTP/1.1
# Header: value
# Other-heder: value
# Cookie: here-comes-the-cookies
#
# request body
#
# the headers are separated from the body by an empty line
# we are interested in the method and the path (mostly the path)
read method path version
# /search?count=10&radius=500&lat=59.33258&lng=18.0649&tags=
count_re=".*count=([0-9]+).*"
if [[ "$path" =~ $count_re ]]; then
count=${BASH_REMATCH[1]}
fi
radius_re=".*radius=([0-9]+).*"
if [[ "$path" =~ $radius_re ]]; then
radius=${BASH_REMATCH[1]}
fi
lat_re=".*lat=([0-9.]+).*"
if [[ "$path" =~ $lat_re ]]; then
lat=${BASH_REMATCH[1]}
fi
lng_re=".*lng=([0-9.]+).*"
if [[ "$path" =~ $lng_re ]]; then
lng=${BASH_REMATCH[1]}
fi
}
function find_products {
products=$(./find_products.sh $lat $lng $radius $count 2>/dev/null | ./build_json.sh 2>/dev/null)
}
function render_response {
echo "HTTP/1.1 200 OK
Content-Type: application/json
Access-Control-Allow-Origin: *
Connection: close
{
\"products\": [
$products
]
}"
}
parse_http_request
find_products
render_response
exit 0