Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

routing works, sorta

  • Loading branch information...
commit 8503002fd8f89474327f99294d4fc9afa86499ca 1 parent d1ff941
@jneen authored
View
16 bin/balls
@@ -0,0 +1,16 @@
+#!/bin/bash
+export BALLS_LIB="$(dirname $0)/../lib"
+. $BALLS_LIB/balls.sh
+
+mode=$1; shift
+
+case $mode in
+ route|server)
+putd ROUTES
+ balls::$mode
+ ;;
+ *)
+ echo "unknown"
+ ;;
+esac
+
View
5 lib/balls.sh
@@ -0,0 +1,5 @@
+#!/bin/bash
+[[ -z "$BALLS_LIB" ]] && BALLS_LIB=$(dirname $0)
+. $BALLS_LIB/server.sh
+. $BALLS_LIB/util.sh
+. $BALLS_LIB/router.sh
View
5 lib/cgi.sh
@@ -0,0 +1,5 @@
+#!/bin/bash
+# /var/www/cgi-bin/balls.sh
+[[ -z "$BALLS_CONFIG" ]] && BALLS_CONFIG=$PWD/config.sh
+
+balls route $BALLS_CONFIG
View
52 lib/router.sh
@@ -0,0 +1,52 @@
+#!/bin/bash
+# ROUTES=""
+balls::define_route() {
+ local verb=$1; shift
+ local path=$1; shift
+ local action=$1; shift
+
+ local route_line="$(echo -e "$verb\t$path\t$action")"
+ if [ -z "$ROUTES" ]; then
+ ROUTES="$route_line"
+ else
+ ROUTES="$ROUTES
+$route_line"
+ fi
+}
+
+alias b:GET='balls::define_route GET'
+alias b:POST='balls::define_route POST'
+alias b:PUT='balls::define_route PUT'
+alias b:DELETE='balls::define_route DELETE'
+
+balls::route() {
+
+stderr "begin balls::route"
+putd REQUEST_METHOD
+putd REQUEST_PATH
+putd ROUTES
+
+ local action=$(
+ echo "$ROUTES" | grep "^$REQUEST_METHOD" | {
+ while read line; do
+ arr=($line)
+ path=${arr[1]}
+ act=${arr[2]}
+ if [[ "$REQUEST_PATH" = "$path" ]]; then
+ echo $act
+ break
+ fi
+ done
+ }
+ )
+
+putd action
+stderr "$(type $action)"
+ if [ -z "$action" ]; then
+ true
+ # 404
+ elif [ -n "$(type $action)" ]; then
+stderr LOL
+ $action
+ fi
+}
View
32 lib/server.sh
@@ -0,0 +1,32 @@
+#!/bin/bash
+balls::server() {
+ local sock=$BALLS_SOCK
+ [[ -z "$sock" ]] && sock=./tmp/balls.sock
+ [ -p $sock ] || mkfifo $sock
+
+putd ROUTES
+ while true ; do
+ ( cat $sock ) | nc -l -p 9009 | (
+ local REQ=$(while read L && [ " " "<" "$L" ] ; do echo "$L" ; done)
+ balls::parse_http
+
+putd ROUTES
+ balls::route > $sock
+ )
+ done
+}
+
+balls::parse_http() {
+ local req_line=$(echo "$REQ" | head -1)
+ req_line=($req_line)
+
+ export REQUEST_METHOD=${req_line[0]}
+
+ export REQUEST_URI=${req_line[1]}
+ export REQUEST_PATH=${REQUEST_URI%%\?*}
+ export QUERY_STRING=${REQUEST_URI#*\?}
+
+ export VERSION=${req_line[2]}
+
+ export SERVER_SOFTWARE="balls_cgi/0.0"
+}
View
20 lib/util.sh
@@ -0,0 +1,20 @@
+#!/bin/bash
+matches() {
+ local match
+ if [ -t 0 ]; then
+ match=$1; shift
+ else
+ match=$(cat -)
+ fi
+ local regex="$1"
+
+ test -n "$(echo "$match" | grep "$regex")"
+}
+
+stderr() {
+ echo "$@" >&2
+}
+
+putd() {
+ stderr "$1=${!1}"
+}
View
52 router.sh
@@ -1,52 +0,0 @@
-#!/bin/bash
-
-matches() {
- local match
- if [ -t 0 ]
- match=$1; shift
- else
- match=$(cat -)
- fi
- local regex="$1"
-
- test -n "$(echo "$match" | grep "$regex")"
-}
-
-declare -a ROUTES
-shweb::define_route() {
- verb=$1; shift
- path=$1; shift
- action=$1; shift
- ROUTES=$ROUTES"\n$verb\t$path\t$action"
-}
-
-alias GET='shweb::define_route GET'
-alias POST='shweb::define_route POST'
-alias PUT='shweb::define_route PUT'
-alias DELETE='shweb::define_route DELETE'
-
-GET /foo Foo::handle
-
-shweb::route() {
- local path
- local act
- local arr
- local action=$(
- echo $ROUTES | grep "^$HTTP_METHOD" | {
- while read line; do
- local arr=($foo)
- path=${arr[1]}
- act=${arr[2]}
- if matches "$HTTP_REQUEST_PATH" "$path"; then
- echo $act
- break
- fi
- done
- }
- )
- if [ -z "$action" ]; then
- # 404
- elif [ -n "$(type $action)" ]; then
- $action
- fi
-}
View
38 server.sh
@@ -1,38 +0,0 @@
-#!/bin/bash
-# web.sh -- http://localhost:9009/
-#folder=folder_with_imges
-cd $folder
-
-stderr() {
- echo "$@" 1>&2
-}
-
-[[ -z "$RESP" ]] && RESP=/tmp/webresp
-[ -p $RESP ] || mkfifo $RESP
-
-while true ; do
- ( cat $RESP ) | nc -l -p 9009 | (
- REQ=$(while read L && [ " " "<" "$L" ] ; do echo "$L" ; done)
- stderr "$REQ"
- stderr "[`date '+%Y-%m-%d %H:%M:%S'`] ${REQ%%$'\n'*}"
-
- REQ=${REQ#* } REQ=${REQ% HTTP*}
-
- page=$(cat foo.esh | sed 's/{{ myvar }}/lolwut/g')
-
- {
- echo
- echo -e HTTP/1.1 200 OK
- echo Content-Type: text/html
- echo Server: bash/2.0
- echo Connection: Close
- echo Content-Length: ${#page}
- echo
-
- echo "$page"
-
- echo
- } > $RESP
- stderr END
- )
-done
Please sign in to comment.
Something went wrong with that request. Please try again.