mirror of
https://codeberg.org/bunbun/bunbun.dev
synced 2025-03-13 08:37:56 -07:00
added arguments and support for running locally
This commit is contained in:
parent
9a0c639c96
commit
48aab27d60
25
arg-handler.fnl
Normal file
25
arg-handler.fnl
Normal 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
4
main.fnl
Normal file → Executable file
@ -1,3 +1,7 @@
|
|||||||
|
#!/usr/bin/env fennel
|
||||||
|
|
||||||
|
(require :arg-handler)
|
||||||
|
|
||||||
(set _G.files {})
|
(set _G.files {})
|
||||||
|
|
||||||
(require :server)
|
(require :server)
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
|
|
||||||
(local handle-client (. (require :client-handler) :handle-client))
|
(local handle-client (. (require :client-handler) :handle-client))
|
||||||
|
|
||||||
(local port 50625)
|
(local port _G.port)
|
||||||
(local server (socket.tcp))
|
(local server (socket.tcp))
|
||||||
|
|
||||||
(server:bind "*" port)
|
(server:bind "*" port)
|
||||||
@ -22,8 +22,9 @@
|
|||||||
|
|
||||||
(set (_G.client-address _G.client-port) (client:getpeername))
|
(set (_G.client-address _G.client-port) (client:getpeername))
|
||||||
|
|
||||||
(set client (ssl.wrap client params))
|
(when (not _G.use-http)
|
||||||
(client:dohandshake)
|
(set client (ssl.wrap client params))
|
||||||
|
(client:dohandshake))
|
||||||
|
|
||||||
(let [co (coroutine.create (fn [] (handle-client client)))]
|
(let [co (coroutine.create (fn [] (handle-client client)))]
|
||||||
(coroutine.resume co)))
|
(coroutine.resume co)))
|
||||||
|
@ -1,14 +1,15 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
command="fennel main.fnl"
|
command="./main.fnl"
|
||||||
kill_command="pkill -f 'main\.fnl'"
|
kill_command="pkill -f 'main\.fnl'"
|
||||||
|
|
||||||
log_file="log"
|
log_file="log"
|
||||||
|
|
||||||
usage="Usage: start-server [-h] [-l] [--log-file] [-k]
|
usage="Usage: start-server [-h] [--localhost] [-l] [--log-file] [-k]
|
||||||
|
|
||||||
Options:
|
Options:
|
||||||
-h, --help Show this help message and exit
|
-h, --help Show this help message and exit
|
||||||
|
--localhost Run the server locally
|
||||||
-l, --log Log requests in the log file
|
-l, --log Log requests in the log file
|
||||||
--log-file Set the log file
|
--log-file Set the log file
|
||||||
-k, --kill Kill the server and exit
|
-k, --kill Kill the server and exit
|
||||||
@ -19,6 +20,8 @@ for (( i=1; i<=$#; i++ )); do
|
|||||||
if [[ "${!i}" == "-h" || "${!i}" == "--help" ]]; then
|
if [[ "${!i}" == "-h" || "${!i}" == "--help" ]]; then
|
||||||
printf "$usage"
|
printf "$usage"
|
||||||
exit 0
|
exit 0
|
||||||
|
elif [[ "${!i}" == "--localhost" ]]; then
|
||||||
|
command="${command} --port 8080 --use-http"
|
||||||
elif [[ "${!i}" == "-l" || "${!i}" == "--log" ]]; then
|
elif [[ "${!i}" == "-l" || "${!i}" == "--log" ]]; then
|
||||||
command="${command} | tee ${log_file}"
|
command="${command} | tee ${log_file}"
|
||||||
elif [[ "${!i}" == "--log-file" ]]; then
|
elif [[ "${!i}" == "--log-file" ]]; then
|
||||||
|
Loading…
x
Reference in New Issue
Block a user