added arguments and support for running locally

This commit is contained in:
Winter 2024-07-12 14:26:42 -07:00
parent 9a0c639c96
commit 48aab27d60
4 changed files with 38 additions and 5 deletions

25
arg-handler.fnl Normal file
View File

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

4
main.fnl Normal file → Executable file
View File

@ -1,3 +1,7 @@
#!/usr/bin/env fennel
(require :arg-handler)
(set _G.files {})
(require :server)

View File

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

View File

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