From c780ed1386a9dedcd942e765d894211d7be06bb2 Mon Sep 17 00:00:00 2001 From: Jamon Holmgren Date: Wed, 29 Nov 2023 15:57:52 -0800 Subject: [PATCH] Move to qub folder, add conf file, rename app to server.bas --- README.md | 22 ++++++++--------- src/cli.sh | 20 ++++++++------- template/README.md | 8 +++--- template/bin/build | 2 +- template/bin/install_qb64 | 6 ++--- template/qub/qub.conf | 5 ++++ template/{app.bas => qub/server.bas} | 37 +++++++++++++++++++++------- 7 files changed, 64 insertions(+), 36 deletions(-) create mode 100644 template/qub/qub.conf rename template/{app.bas => qub/server.bas} (97%) diff --git a/README.md b/README.md index 225ec17..03b6b3b 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,7 @@ qub create It'll ask you for your domain name (e.g. jamon.dev) which doubles as your project's folder name. It will also ask if you want to install QB64 (I recommend you do). -When done, you can CD into the new folder and run `./bin/build` to build the website. Then, run `./app` to start the web server. Visit [http://localhost:6464/](http://localhost:6464/) to view the website. +When done, you can CD into the new folder and run `./bin/build` to build the website. Then, run `./server` to start the web server. Visit [http://localhost:6464/](http://localhost:6464/) to view the website. ## Modifying your website @@ -46,6 +46,9 @@ Your new website has the following folder structure: ``` bin +qub + server.bas + qub.conf web pages home.html @@ -56,15 +59,12 @@ web footer.html header.html head.html -app.bas README.md ``` -### app.bas - -This is the Qub web server. You can modify it if you want to change the port or add more functionality. +### qub/server.bas -However, if you don't modify it, you can periodically update it by running `qub update`. Note this will blow away any modifications you've made, so be careful! +This is the Qub web server. You can periodically update it by running `qub update`. Note this will blow away any modifications you've made, so be careful! Qub's web server was originally based on [Yacy](https://github.com/smokingwheels/Yacy_front_end) by SmokingWheels, but has been heavily modified since. It comes with a number of features: @@ -74,10 +74,10 @@ Qub's web server was originally based on [Yacy](https://github.com/smokingwheels - [x] Binary file serving (images, fonts, etc) - [x] Custom 404 page support - [x] Basic dynamic variable support (e.g. `${year}` in web/footer.html, `${slug}` for use in your navigation, etc) +- [x] Custom port support (via ./qub.conf) - [ ] Customizable dynamic variable support (coming soon) - [ ] 301 redirects support (coming soon) - [ ] Custom 500 page support (coming soon) -- [ ] Custom port support (coming soon) - [ ] More customizable templating support (coming soon) It does not (and probably won't) support HTTPS or HTTP2. I recommend putting CloudFlare in front of it in production (more in the [deploy guide](#deploy-guide) below). @@ -130,13 +130,13 @@ Once I had a working website, I realized that I wanted to make it easier for oth Qub is not particularly important to modern technology in the grand scheme of things, but it's been a blast to work on. I hope you enjoy it! +## Future + +I'd love to build more QBasic-powered server capabilities. I'm still learning how to utilize QB64 -- for example, in old QBasic, you couldn't include external files, but in QB64 you can. + ## TODO -- [ ] Fill out the README, documentation, screenshots -- [ ] Set up CI -- [ ] Add a Deployment doc - [ ] Make the default template look nicer, better template README -- [ ] htmx version maybe - [ ] YouTube video on Jamon's Code Quests ## License diff --git a/src/cli.sh b/src/cli.sh index 5e2253f..0e715f4 100755 --- a/src/cli.sh +++ b/src/cli.sh @@ -94,8 +94,10 @@ main() { curl -s $GITHUB_TEMPLATE/README.md > "${DOMAIN}/README.md" replace_in_file "$DOMAIN/README.md" "DOMAIN" "$DOMAIN" echo -e "${GREEN}✓${END} README.md" - curl -s $GITHUB_TEMPLATE/app.bas > "${DOMAIN}/app.bas" - echo -e "${GREEN}✓${END} app.bas" + curl -s $GITHUB_TEMPLATE/qub/server.bas > "${DOMAIN}/qub/server.bas" + echo -e "${GREEN}✓${END} qub/server.bas" + curl -s $GITHUB_TEMPLATE/qub/qub.conf > "${DOMAIN}/qub/qub.conf" + echo -e "${GREEN}✓${END} qub/qub.conf" curl -s $GITHUB_TEMPLATE/.gitignore > "${DOMAIN}/.gitignore" echo -e "${GREEN}✓${END} .gitignore" curl -s $GITHUB_TEMPLATE/bin/install_qb64 > "${DOMAIN}/bin/install_qb64" @@ -149,7 +151,7 @@ main() { echo -e " ./bin/install_qb64" fi echo -e " ./bin/build" - echo -e " ./app" + echo -e " ./server" echo "" echo -e "${YELLOW}Support Qub development:${END}" echo "" @@ -167,11 +169,11 @@ main() { echo "Updating Qub-powered QB64 website project..." echo "" - # If we don't have an app.bas file, exit + # If we don't have a server.bas file, exit - if [[ ! -f app.bas ]]; then + if [[ ! -f qub/server.bas ]]; then echo "" - echo -e "${RED}app.bas file not found.${END} Are you in the right folder?" + echo -e "${RED}qub/server.bas file not found.${END} Are you in the right folder?" echo "" return 1 fi @@ -194,10 +196,10 @@ main() { return 1 fi - # Download the latest app.bas from the template + # Download the latest server.bas from the template - curl -s $GITHUB_TEMPLATE/app.bas > app.bas - echo -e "${GREEN}✓${END} app.bas updated to latest" + curl -s $GITHUB_TEMPLATE/qub/server.bas > qub/server.bas + echo -e "${GREEN}✓${END} qub/server.bas updated to latest" echo "" return 0 diff --git a/template/README.md b/template/README.md index 1c9b0c0..ee19ba8 100644 --- a/template/README.md +++ b/template/README.md @@ -4,14 +4,16 @@ This is a website powered by [QB64](https://www.qb64.org/) and [Qub](https://qub ## Running Locally -Run `./bin/install_qb64` to install dependencies. +Run `./bin/install_qb64` to install the QB64 dependency into `./qb64`. -Run `./bin/build` to build the website locally. +Run `./bin/build` to build the Qub web server. -Run `./app` once you've built to start the webserver on port 6464. +Run `./server` once you've built to start the webserver on port 6464. Visit [http://localhost:6464/](http://localhost:6464/) to view the website. +Modify `./qub/qub.conf` to change the port and other settings. + ## Acknowledgements - Original QB64 webserver found here: https://github.com/smokingwheels/Yacy_front_end/blob/master/yacyfrontend.bas diff --git a/template/bin/build b/template/bin/build index 3e7efc5..c62de64 100755 --- a/template/bin/build +++ b/template/bin/build @@ -1,3 +1,3 @@ #!/bin/bash -./qb64/qb64 -x -o ../app ./app.bas +./qb64/qb64 -x -o ../server ./qub/server.bas diff --git a/template/bin/install_qb64 b/template/bin/install_qb64 index 57ea5b3..61fd38e 100755 --- a/template/bin/install_qb64 +++ b/template/bin/install_qb64 @@ -4,9 +4,9 @@ echo "This installs QB64 on a computer or server." ROOT_FOLDER=$(pwd) -# Check if $ROOT_FOLDER/app.bas exists (if not, exit with an error) -if [ ! -f "${ROOT_FOLDER}/app.bas" ]; then - echo "Error: app.bas not found in ${ROOT_FOLDER}" +# Check if $ROOT_FOLDER/qub/server.bas exists (if not, exit with an error) +if [ ! -f "${ROOT_FOLDER}/qub/server.bas" ]; then + echo "Error: qub/server.bas not found in ${ROOT_FOLDER}" echo "(are you running this from the right folder?)" exit 1 fi diff --git a/template/qub/qub.conf b/template/qub/qub.conf new file mode 100644 index 0000000..758abee --- /dev/null +++ b/template/qub/qub.conf @@ -0,0 +1,5 @@ +# Configuration file for Qub +# Uncomment key=value lines to override the defaults + +# The port to use for the Qub server +# port=6462 diff --git a/template/app.bas b/template/qub/server.bas similarity index 97% rename from template/app.bas rename to template/qub/server.bas index f784f1d..1ab0f0e 100644 --- a/template/app.bas +++ b/template/qub/server.bas @@ -40,11 +40,31 @@ Dim client_host(1 To MAX_CLIENTS) As String Dim client_browser(1 To MAX_CLIENTS) As String connections = 0 +port$ = DEFAULT_PORT -Print "Starting QB64 webserver on port " + DEFAULT_PORT +' check if qub.conf exists, and read in the configuration if so +If _FILEEXISTS("./qub/qub.conf") Then + Print "Reading configuration from qub.conf" + Open "./qub/qub.conf" For Input As #1 + Do While Not EOF(1) + Line Input #1, line$ + ' Strip any preceding whitespace from line$ + Do While Left$(line$, 1) = " " + line$ = Mid$(line$, 2) + Loop + + ' Read in the config options + If Left$(line$, 5) = "port=" Then + port$ = Mid$(line$, 6) + End If + Loop + Close #1 +End If + +Print "Starting QB64 webserver on port " + port$ ' kick off the listener -host = _OpenHost("TCP/IP:" + DEFAULT_PORT) +host = _OpenHost("TCP/IP:" + port$) ' main loop! Do @@ -112,7 +132,6 @@ Loop Until InKey$ = Chr$(27) ' escape quits Close #host System ' Quits to system - StaticFileError: Print "File error: " + Error$ Resume Next @@ -311,7 +330,7 @@ Function handle_request% (c As Integer) If Len(client_host(c)) = 0 Then client_host(c) = DEFAULT_HOST End If - 'assume the request can be completed; set to 0 if it can't. + ' assume the request can be completed; set to 0 if it can't. handle_request = 1 code$ = "200 OK" content_type$ = "text/html" @@ -435,16 +454,16 @@ Function handle_request% (c As Integer) Exit Function - not_found: +not_found: respond c, "HTTP/1.1 404 Not Found", "404 Not Found", "text/html" Exit Function - large_request: +large_request: respond c, "HTTP/1.1 413 Request Entity Too Large", "", "text/html" handle_request = 1 Exit Function - bad_request: +bad_request: respond c, "HTTP/1.1 400 Bad Request", "", "text/html" handle_request = 1 Exit Function @@ -453,11 +472,11 @@ Function handle_request% (c As Integer) handle_request = 1 Exit Function - runtime_internal_error: +runtime_internal_error: Print "RUNTIME ERROR: Error code"; Err; ", Line"; _ErrorLine Resume internal_error - internal_error: +internal_error: respond c, "HTTP/1.1 500 Internal Server Error", "", "text/html" handle_request = 1 Exit Function