Add example config and CGI

Also wrap lines in readme
This commit is contained in:
hedy 2022-04-01 09:45:56 +08:00
parent f7f6a27898
commit ba2692b5e9
Signed by: hedy
GPG Key ID: B51B5A8D1B176372
8 changed files with 114 additions and 9 deletions

View File

@ -27,8 +27,10 @@ A static spartan server with many features:
you have two options for now:
### with `go get`
first, you need to have go installed and have a folder `~/go` with `$GOPATH` pointing to it.
### Option 1: with `go get`
first, you need to have go installed and have a folder `~/go` with `$GOPATH`
pointing to it.
```
go get git.sr.ht/~hedy/spsrv
@ -38,30 +40,40 @@ there will be a binary at `~/go/bin/` with the source code at `~/go/src/`
feel free to move the binary somewhere else like `/usr/sbin/`
### or just build it yourself
### Option 2: just build it yourself
run `git clone https://git.sr.ht/~hedy/spsrv` from any directory and `cd spsrv`
make sure you have go installed and working.
```
git checkout v0.0.0 # optionally pin a specific tag
go build
```
when it finishes, the binary will be in the current directory.
### otherwise...
If you don't have/want go installed, you can contact me, and if you're lucky, I have the same OS as you and you can use my compiled binary (lol). I'll eventually have automated uploads of binaries for various architectures for each release in the future.
If you don't have/want go installed, you can contact me, and if you're lucky, I
have the same OS as you and you can use my compiled binary (lol). I'll
eventually have automated uploads of binaries for various architectures for
each release in the future.
## configuration
The default config file location is `/etc/spsrv.conf` you can specify your own path by running spsrv like
```
spsrv -c /path/to/file.conf
```
You don't need a config file to have spsrv running, it will just use the default values.
You don't need a config file to have spsrv running, it will just use the
default values.
### config options
@ -93,6 +105,8 @@ Here are the config options and their default values
* `CGIPaths=["cgi/"]`: list of paths where world-executable files will be run as CGI processes. These paths would be checked if it prefix the requested path. For the default value, a request of `/cgi/hi.sh` (requesting to `./public/cgi/hi.sh`, for example) will run `hi.sh` script if it's world executable.
* `usercgiEnable=false`: enable running user's CGI scripts too. This is dangerous as spsrv does not (yet) change the Uid of the CGI process, hence the process would be ran by the same user that is running the server, which could mean write access to configuration files, etc. Note that this option will be assumed `false` if `userdirEnable` is set to `false`. Which means if user directories are not enabled, there will be no per-user CGI.
Check out some example configuraton in the [examples/](examples/) directory.
## CLI
You can override values in config file if you supply them from the command line:
@ -106,7 +120,10 @@ Usage: spsrv [ [ -c <path> -h <hostname> -p <port> -d <path> ] | --help ]
-p, --port int Port to listen to
```
Note that you *cannot* set the hostname or the dir path to `,` because spsrv uses that to check whether you provided an option. You can't set port to `0` either, sorry, this limitation comes with the advantage of being able to override config values from the command line.
Note that you *cannot* set the hostname or the dir path to `,` because spsrv
uses that to check whether you provided an option. You can't set port to `0`
either, sorry, this limitation comes with the advantage of being able to
override config values from the command line.
There are no arguments wanted when running spsrv, only options as listed above :)
@ -128,12 +145,15 @@ DATA_LENGTH # Input data length
The data block, if any, will be piped as stdin to the CGI process.
Keep in mind that CGI scripts (as of now) are run by the same user as the server process, hence it is generally dangerous for allowing users to have their own CGI scripts. See configuration section for more details.
Keep in mind that CGI scripts (as of now) are run by the same user as the
server process, hence it is generally dangerous for allowing users to have
their own CGI scripts. See configuration section for more details.
Check out some example CGI scripts in the [examples/](examples/) directory.
## todo
```
- [x] /folder to /folder/ redirects
- [x] directory listing
- [ ] logging to files
@ -159,4 +179,3 @@ Keep in mind that CGI scripts (as of now) are run by the same user as the server
README:
- [ ] Add example confs
- [ ] Add example .service files
```

6
examples/cgi/echo Executable file
View File

@ -0,0 +1,6 @@
#!/usr/bin/env sh
# Echos back whatever data you send it
printf "2 application/octet-stream\r\n"
cat /dev/stdin

5
examples/cgi/env.sh Executable file
View File

@ -0,0 +1,5 @@
#!/usr/bin/env sh
printf "2 text/gemini\r\n"
whoami
printenv

10
examples/cgi/greet.sh Executable file
View File

@ -0,0 +1,10 @@
#!/usr/bin/env sh
# User can enter their name and this will greet them
# Example inpu link:
#
# =: greet.sh Enter your name
printf "2 text/plain\r\n"
name=$(cat /dev/stdin)
echo "Hello, ${name:-World}!"

View File

@ -0,0 +1,13 @@
# Example config for a local testing server
hostname="localhost"
port=3000
rootDir = "./public"
# attempt CGI for all requests
cgiPaths=[""]
dirlistEnable=true
dirlistSort="time"
dirlistTitles=true

View File

@ -0,0 +1,19 @@
# Example config for a multi-user unix server
# accept any hostname
hostname=""
port=300
rootdir="/var/spartan"
# allow CGI for all files - so you can have your root index.gmi do a user
# listing
cgipaths=[""]
userdirEnable=true
# each user would have their content be at /home/user/public_spartan,
# accessible via spartan://host.name/~user/
userdir="public_spartan"
# enable per-user CGI (you might wanna be running the spartan server under
# another user, such as spartan:nogroup)
usercgiEnable=true

View File

@ -0,0 +1,18 @@
# Example config for allowing per-user vhosts on your pubnix, i.e.: allownig
# spartan://user.example.org
# you must set a non-empty hostname for user-vhost to work
hostname="example.org"
port=300
userdirEnable=true
# each user would have their content be at /home/user/public_spartan,
# accessible via both spartan://example.org/~user/ and
# spartan://user.example.org/
userdir="public_spartan"
userSubdomains=true
# enable per-user CGI (you might wanna be running the spartan server under
# another user, such as spartan:nogroup)
usercgiEnable=true

View File

@ -0,0 +1,15 @@
[Unit]
Description=spsrv
After=network.target
[Service]
Type=simple
Restart=always
RestartSec=5
User=spartan
Group=spartan
ExecStart=/usr/local/bin/spsrv -c /etc/spsrv.conf
[Install]
WantedBy=multi-user.target