11 ms·
Sherver: Bash lightweight web server
- BossingAround 5y ago> I didn't want to install and configure Apache or nGinx. In fact, I didn't want any configuration. Personally, in that case, I'd use: python3 -m http.server
- rascul 5y agoThere are many similar options. I think that awhile ago there may have been a list of one liner web servers posted to HN. Either way, I've been using python's for years and it's just muscle memory now.
- em500 5y agoBig list of http static servers: https://gist.github.com/willurd/5720255 https://gist.github.com/willurd/5720255
- nickjj 5y agoThat's my goto solution for a simple server if I'm testing something locally but it has issues dealing with concurrent requests in its simple form. For example I ran a simple hello world test service on Kubernetes once using Python's http.server. Just having 3 Kubernetes health checks happen on regular intervals would routinely cause the server to respond with errors. I think what ends up happening is if you have 2 requests coming in within a short enough period of time one of them will fail. You have to use a more complicated threaded http server if you want a zero dependency solution.
- adamddev1 5y ago`npm install -g live-server` Also works great (but a bit bulkier sure).
- montroser 5y agoFantastic. For a more sinatra-inspired take, see also https://github.com/guigo2k/ganesh https://github.com/guigo2k/ganesh
- deleted 5y ago[deleted]
- vkazanov 5y agoOne those things that is s scarily beautiful
- medv 5y agoIt is a socat server, not bash. Basically a bash script calling socat. Definitely not “pure”.
- prussian 5y agoSomeone beat me to it. When I think of pure bash, I don't really think of invoking anything that isn't a builtin or a grammatical feature of bash. If socat is fair-play, almost anything you can spawn on a shell should count then. edit: on top of that, I already found a perl script utility in the repo: https://github.com/remileduc/sherver/blob/master/scripts/utils/mimetype https://github.com/remileduc/sherver/blob/master/scripts/uti...
- fragbait65 5y agoI think I'll just write something similar and wrap Apache with a shell scrip, claim that it's a web server written in bash, post it here and see what reaction I get, just for shits and giggles.
- prussian 5y agoHere: https://github.com/remileduc/sherver/issues/1 https://github.com/remileduc/sherver/issues/1 that way you can have HTTP/1.1 support even with arbitrary output lengths. I was planning on sending this to the linked repo so the owner doesn't need to use HTTP/1.0. EDIT: changed link to the github issue I created with inlined utility.
- bluetomcat 5y agoJokingly, "sudo service apache2 start" may also be considered pure Bash :-)
- fragbait65 5y agoExactly!
- junon 5y ago
- throwaway984393 5y agoThey reinvented CGI scripts but didn't actually implement CGI. Could have implicitly supported a lot of apps by default. There's some pretty decent CGI libraries for Bash, too. Personally I use busybox's httpd (which does support CGI) for a small portable web server.
- laumars 5y ago> I didn't want to install and configure Apache or nGinx. In fact, I didn't want any configuration. If you ignore all of the manual installation instructions and any dev time you took to create this, then sure. I’m not going to argue that the author should have used something else instead though. It’s a personal project used purely on an internet system of his house. For those kinds it projects “because I could” is as good a reason as any. I just think more authors should be honest and say “I just did it because it seemed fun at the time”
- tjoff 5y agoManual instructions and dev time doesn't necessarily pollute the state of the machine though and are harder to replicate, which is how I would interpret it.
- laumars 5y agoNot having your application packaged up for quick removal strikes me as more polluting. And I don’t really see how an independent solution with almost no documentation and zero configuration management support (eg Ansible) is easier to replicate than a battle tested industry standard with widespread support. Don’t get me wrong, I have nothing against building things for fun. But the technical arguments made for why this is preferable over an established solution doesn’t stack up.
- tjoff 5y agoI do. Just as I would use this (would look into it more first though), the netcat trick mentioned here or python3 -m http.server over ever considering installing Ansible and/or nginx/apache. Totally different use case though.
- laumars 5y agoIf I was rolling my own and concerned about polluting, I’d favour a language like Rust or Go because I then don’t need to install the Python runtime, have random undocumented dependencies for the bash etc. Or I’d use Docker. But as said before, there’s no wrong answer here since it’s just a hobby project running on a HTPC.
- montroser 5y agoFor the occasional service where you are essentially just wrapping an existing binary, a bash server behind uwsgi can be totally sensible.
- remote 5y agoKudos for scripting a great hack in BASH. Since the title reads "pure bash" however I was expecting to see an implementation using builtin bash features such as /dev/tcp. Have you considered solving this problem using BASH builtin support for TCP?
- rascul 5y agoI recall that bash can't create a server socket, because bash doesn't bind().
- IceWreck 5y agoIf this is pure bash then so is ``` caddy file-server --browse --listen :80 ```
- rascul 5y agoThis seems like an odd usage of cat and command substitution to me: HEAD_TEMPLATE=$(cat <<EOF <title>Sherver example</title> <meta name="description" content="Sherver example"> EOF ) https://github.com/remileduc/sherver/blob/master/scripts/index.sh#L21 https://github.com/remileduc/sherver/blob/master/scripts/ind...
- junon 5y agoIt achieves multiline block text in bash, which is otherwise a PITA to reason about. It can keep certain scripts cleaner, especially since `cat` is often inlined, without process spawning.
- steerablesafe 5y ago> especially since `cat` is often inlined, without process spawning. That would be surprising. How would that work? `cat` is definitely not a bash built-in.
- junon 5y agoCat is not a specified built-in but many implementations do build it in to speed up scripts that use it quite heavily. Other examples include `which`, `time`, `echo`, etc. They're usually promoted to built-ins when it's clear that's the intended command to run, but there are still analogs on the PATH if need-be - I'm not sure exactly how bash would detect a non-standard executable there to know when it's unsafe to promote to a built-in, though. For example with echo: $ bash -c 'echo hi' hi $ strace bash -c 'echo hi' | grep 'exec' execve("/usr/bin/bash", ["bash", "-c", "echo hi"], 0x7fffe264aad0 /* 24 vars */) = 0 $ strace bash -c '$(which echo) hi' | grep 'exec' execve("/usr/bin/bash", ["bash", "-c", "$(which echo) hi"], 0x7fffe36c6cd0 /* 24 vars */) = 0 execve("/usr/bin/echo", ["/usr/bin/echo", "hi"], 0x7fffe2e16ff0 /* 25 vars */) = 0 In fact for me, `cat` always gets inlined, even if I call it with its full path or with `which cat`. There are cases where the built-in and the actual executable differ - the most prominent case being `time`. The built-in does not have the command line arguments to print out things like pre-empted scheduling (forced context swiches). $ time --help --help: command not found $ /usr/bin/time --help Usage: /usr/bin/time [-apvV] [-f format] [-o file] [--append] [--verbose] [--portability] [--format=format] [--output=file] [--version] [--quiet] [--help] command [arg...]
- Brian_K_White 5y agoAh, no. Bold claim unrealized. Immediately I see many unnecessary forks and externals to do things that bash actually can do itself.
- _wldu 5y agoHere's my small webserver: #!/bin/bash while : ; do cat conference.txt | nc -l 80; done Here's the story behind it: https://www.go350.com/posts/finding-a-hacked-server/ https://www.go350.com/posts/finding-a-hacked-server/
- Tepix 5y agoI liked the story and the impromptu web server! Two comments: - using "ping -a" creates an audible ping so you can find the server if you're by yourself without having to look at the screen. - No need for cat: while :; do nc -l 80 < conference.txt; done
- vincnetas 5y agoTIL. thanx for -a.
- _wldu 5y agoInteresting. I don't hear anything when I use the -a flag.
- thanatos519 5y agoLet's check with "reveal codes": $ ping -a 127.0.0.1 | less PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data. 64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.029 ms^G 64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.023 ms^G Looks like you need a terminal that has a BEL sound.
- vesinisa 5y agoIt just echoes a Bell character to your terminal. Make sure your terminal emulator supports it, and is configured properly: https://en.wikipedia.org/wiki/Bell_character https://en.wikipedia.org/wiki/Bell_character
- rzzzt 5y agoWindows Terminal shows a "visual bell" in the tab bar whenever BEL is emitted (even though I have it set to "audible" in advanced settings of the profile, hmm). Edit: Uhm, right. I have also silenced all system sounds in Windows.
- moondev 5y agogo run github.com/patrickhener/goshs@latest
- notRobot 5y agoSee also: darkttpd: When you need a web server in a hurry. https://github.com/emikulic/darkhttpd https://github.com/emikulic/darkhttpd
- pkrumins 5y agoAlso `php -S` is very handy.
- MisterTea 5y agoPlan 9's rc-httpd is quite useful, supports cgi and serves many plan 9 web sites, many of which run werc http://werc.cat-v.org/ http://werc.cat-v.org/ It can also run on Unix via plan 9 port (p9p) or derivatives. https://github.com/9front/9front-test/blob/master/rc/bin/rc-httpd/rc-httpd https://github.com/9front/9front-test/blob/master/rc/bin/rc-...
- zoomablemind 5y agoJust an idea, along the lines of a single binary webserver: one may use Fossil SCM [1] to easily spawn a webserver ('fossil server', or even 'fossil ui'), no need for root (port 8080 by default). The pages could be served as part of a project (all version-controlled), or as documentation pages, or as wiki. Styling is somewhat limited, but could still be done creatively. Additionally, a static website or even a whole application (PHP or the likes) could be rolled out via /ext, the 'extension' path. [1]: https://www.fossil-scm.org https://www.fossil-scm.org
- adamddev1 5y agoVery cool. Was hoping to see Sean Connery in the README.