Live data from Hacker News

static-server: an HTTP server in Go for static content

eli.thegreenplace.net

51–60 of 67 posts

Re: static-server: an HTTP server in Go for static content

#52
post #27

This is cool - I didn't know you could have Go one-liners that look like this: go run github.com/eliben/static-server@latest I had to upgrade to Go 1.21 for this to work - I was previously on Go 1.20. "brew upgrade go" worked for me. Looks like almost the entire implementation is here, it's mostly CLI option parsing logic: https://github.com/eliben/static-server/blob/main/internal/s...

Yeah, with the "go 1.21.0" line in the go.mod file, it only works on Go 1.21. Here's what I get on Go 1.20:

  $ go run .
  go: errors parsing go.mod:
  .../static-server/go.mod:3: invalid go version '1.21.0': must match format 1.23
Eli, you might consider changing that to "go 1.20" so it works on Go 1.20 (and older, actually -- it's the 1.2.3 format that is getting in the way on Go pre-1.21).

Re: static-server: an HTTP server in Go for static content

#53
post #35

Earlier quoted context omitted.

Caddy 1 was created because I needed a quick and easy web server for a lot of my projects. Caddy 2 was created when I got serious about it, and we had governments and enterprises starting to rely on the project. Now we exist because we advocate for (and deliver!) HTTPS on every site, memory safety, dynamic configuration, extensibility, and many more features valuable in a modern web server. Hope that helps :)

Thank you for the response. It looks interesting. >I needed a quick and easy web server I am trying to reason with what I know, but wouldn't something like "python -m http.server 9000" suffice? Or nginx? These are battle tested and python is there on most platforms. Or is there more depth to this specific web server that others don't have?

No, because python's simple HTTP server isn't production ready (doesn't support Range requests last I checked, and quite a few other limitations). And nginx is not "quick and easy" by any means, doesn't grant me the security of memory safety (it was vulnerable to Heartbleed, for example -- I wrote Caddy shortly after Heartbleed), and doesn't give me automatic HTTPS.

And Caddy is very well battle-tested too, btw. ;)

Re: static-server: an HTTP server in Go for static content

#54
post #41

Earlier quoted context omitted.

I think nginx does that too; if you send it a hup signal, it will reload the config.

Indeed, many programs interpret a kill -HUP signal as a "re-read config" command.

This is problematic as when you log out your shell, HUP is sent to the web server process unintentionally...

(One reason we don't use signals in Caddy 2. So uncivilized.)

Re: static-server: an HTTP server in Go for static content

#55
post #9

These are fun to make. I've done it several times. :) That is why I wrote Caddy: $ caddy file-server It does templates, TLS, and other production things really easily from the command line too, including automatically getting certificates: $ caddy file-server --domain example.com Done! I think projects like static-server are wonderful learning examples of how to get Useful Things done in Go.

I'm (very) unfamiliar with webdev and hosting. Is this 'oneliner' a secure way to host files? I have heard that using a reverse proxy, some 2FA on a cloud gateway that forwards to another location, with a VLAN for the file server, and containerization, etc are all the standard practice now for ensuring security. Is there anything that works toward making this goal more easily amenable (since it's not possible with one project), or should I just keep using python -m http.serve and not care?

Re: static-server: an HTTP server in Go for static content

#56
post #40
post #9

These are fun to make. I've done it several times. :) That is why I wrote Caddy: $ caddy file-server It does templates, TLS, and other production things really easily from the command line too, including automatically getting certificates: $ caddy file-server --domain example.com Done! I think projects like static-server are wonderful learning examples of how to get Useful Things done in Go.

Thank you for Caddy! Going from a complex nginx setup to the delight that's the Caddyfile was a breeze. And a special call-out to your dedication to providing amazing documentation. Caddy is what all projects should aspire to be.

Caddy is amazing. I accidentally wiped my fairly complex nginx configuration (yay apt-get purge) and have been so spoiled by Guix that I did not care about etckeeper or ansible on my one Debian server.

Anyway I decided to try Caddy to quickly get up and running and had a good setup in minutes including certificates for multiple domains. Even added wildcard certificates which I never figured out with Lets Encrypt, just because I could.

Thanks for this amazing software :-)

Re: static-server: an HTTP server in Go for static content

#57

Out of curiosity, are there alternatives to this that are already well established or distributed? For example, something you'd get in debian linux?

https://caddyserver.com/ is implemented in Go, production-ready, and easy to setup with a one-liner (though personally I would use official binaries or compile from source rather than use the builds from a distro package manager)

Re: static-server: an HTTP server in Go for static content

#58
There are many projects doing the exact same thing. Here's one approach from myself, but haven't updated since 2019:

https://github.com/philippgille/serve

One difference is that it can create a TLS cert for you, instead of having to supply one via CLI arg.

I don't recommend using it in its current form though due to its (the compiled binaries') Go version being outdated.

Re: static-server: an HTTP server in Go for static content

#59

Out of curiosity, are there alternatives to this that are already well established or distributed? For example, something you'd get in debian linux?

python -m http.server

Doesn't support HTTPS out of the box, unfortunately.

Re: static-server: an HTTP server in Go for static content

#60
post #51

Out of curiosity, are there alternatives to this that are already well established or distributed? For example, something you'd get in debian linux?

apache? nginx? Serving static content off the file system is braindead simple.

What's the zero-config way to temporarily spin up an instance of Apache or Nginx that serves my current directory? The way Debian does this out of the box seems significantly higher-ceremony than that.
Post reply on HN