From 48aab27d60c7faa16bc62896bde391b040983a79 Mon Sep 17 00:00:00 2001 From: Winter Hille Date: Fri, 12 Jul 2024 14:26:42 -0700 Subject: [PATCH] added arguments and support for running locally --- arg-handler.fnl | 25 +++++++++++++++++++++++++ main.fnl | 4 ++++ server.fnl | 7 ++++--- start-server | 7 +++++-- 4 files changed, 38 insertions(+), 5 deletions(-) create mode 100644 arg-handler.fnl mode change 100644 => 100755 main.fnl diff --git a/arg-handler.fnl b/arg-handler.fnl new file mode 100644 index 0000000..517397d --- /dev/null +++ b/arg-handler.fnl @@ -0,0 +1,25 @@ +(set _G.port 50625) +(set _G.use-http false) + +(local usage (: "Usage: %s [-h] [-p] [--use-http] + +Options: + -h, --help Show this help message and exit + -p, --port Set the port number for the server. Don't change this + unless you're running the server locally + --use-http Use http instead of https. Only recommended when running + the server locally + +" :format (. arg 0))) + +(for [i 1 (length arg)] + (tset arg i (: (. arg i) :gsub "^%-+" "")) + (if (or (= (. arg i) :h) (= (. arg i) :help)) + (do + (print usage) + (os.exit)) + (or (= (. arg i) :p) (= (. arg i) :port)) + (do + (set _G.port (. arg (+ i 1))) + (set-forcibly! i (+ i 1))) + (= (. arg i) :use-http) (set _G.use-http true))) diff --git a/main.fnl b/main.fnl old mode 100644 new mode 100755 index f98f9e2..46e39cc --- a/main.fnl +++ b/main.fnl @@ -1,3 +1,7 @@ +#!/usr/bin/env fennel + +(require :arg-handler) + (set _G.files {}) (require :server) diff --git a/server.fnl b/server.fnl index fbe5b17..a044b26 100644 --- a/server.fnl +++ b/server.fnl @@ -3,7 +3,7 @@ (local handle-client (. (require :client-handler) :handle-client)) -(local port 50625) +(local port _G.port) (local server (socket.tcp)) (server:bind "*" port) @@ -22,8 +22,9 @@ (set (_G.client-address _G.client-port) (client:getpeername)) - (set client (ssl.wrap client params)) - (client:dohandshake) + (when (not _G.use-http) + (set client (ssl.wrap client params)) + (client:dohandshake)) (let [co (coroutine.create (fn [] (handle-client client)))] (coroutine.resume co))) diff --git a/start-server b/start-server index 9be2e0f..5545e40 100755 --- a/start-server +++ b/start-server @@ -1,14 +1,15 @@ #!/bin/bash -command="fennel main.fnl" +command="./main.fnl" kill_command="pkill -f 'main\.fnl'" log_file="log" -usage="Usage: start-server [-h] [-l] [--log-file] [-k] +usage="Usage: start-server [-h] [--localhost] [-l] [--log-file] [-k] Options: -h, --help Show this help message and exit + --localhost Run the server locally -l, --log Log requests in the log file --log-file Set the log file -k, --kill Kill the server and exit @@ -19,6 +20,8 @@ for (( i=1; i<=$#; i++ )); do if [[ "${!i}" == "-h" || "${!i}" == "--help" ]]; then printf "$usage" exit 0 + elif [[ "${!i}" == "--localhost" ]]; then + command="${command} --port 8080 --use-http" elif [[ "${!i}" == "-l" || "${!i}" == "--log" ]]; then command="${command} | tee ${log_file}" elif [[ "${!i}" == "--log-file" ]]; then