8 ms·
static-server: an HTTP server in Go for static content
- simonw 3y agoThis 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/server/server.go https://github.com/eliben/static-server/blob/main/internal/s...
- eliben 3y agoIndeed, the actual heavy lifting is done by the Go standard library; this line (https://github.com/eliben/static-server/blob/main/internal/server/server.go#L113 https://github.com/eliben/static-server/blob/main/internal/s...) is where the real magic happens (ignoring the logging middleware): fileHandler := serveLogger(serveLog, http.FileServer(http.Dir(rootDir)))
- benhoyt 3y agoYeah, 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).
- eliben 3y agoDone now; thanks for the note!
- canadaduane 3y agoOut of curiosity, are there alternatives to this that are already well established or distributed? For example, something you'd get in debian linux?
- bryancoxwell 3y agopython -m http.server
- ameliaquining 3y agoDoesn't support HTTPS out of the box, unfortunately.
- TylerE 3y agoapache? nginx? Serving static content off the file system is braindead simple.
- ameliaquining 3y agoWhat'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.
- xmodem 3y agohttps://caddyserver.com/ 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)
- quinnftw 3y agoLooking at the code this appears [1] to be a very thin wrapper around go's built in file server from the http package. [1]: https://github.com/eliben/static-server/blob/main/internal/server/server.go#L113 https://github.com/eliben/static-server/blob/main/internal/s...
- PhilippGille 3y agoThere 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 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.
- gabereiser 3y agoWhile I love Go, have we gotten this lazy that we need a package for this? Go does this in 3 lines minimum, like you describe in your blogpost. However, in your package you expose the ability to kill your server [0] without any security. That’s a huge vulnerability. I know you’ll say “It’s just a static server, meant for serving static stuff” but it will be indexed by pkg.go.dev, people will use this outside your intent. It is the way. At the very least, use a secret token. [0] https://github.com/eliben/static-server/blob/3ce83524ed5429860cd602bfc59f67039c9cd83f/internal/server/server.go#L108 https://github.com/eliben/static-server/blob/3ce83524ed54298...
- badrequest 3y agoPeople are allowed to write code/tools that are useful to only themselves. They're also allowed to post about these tools. Nobody is going to make you use them.
- deleted 3y ago[deleted]
- nicoburns 3y agoThese kind of servers are useful for quickly serving a folder of files locally. Security isn't a primary concern for these kind of use cases.
- tgv 3y ago$ python -m http.server
- inChargeOfIT 3y agoAssuming you have python installed on the system, yeah.
- sdf4j 3y agopython 2 and not 3
- mholt 3y agoThese 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.
- eliben 3y agoThanks for the comment, Matt! I love Caddy. For some reason I thought it needs a configuration file. The simplest cmdline I found to run it as a local server on a non-priveledged port with listings is: $ caddy file-server --listen localhost:8099 --browse Is there a simpler way I'm missing?
- mholt 3y agoOn Mac, the defaults aren't privileged. (Not sure why. That's just how it is.) The --browse flag is optional and unrelated to ports/privileges. Otherwise, that sounds about right. Caddy defaults to port 80 unless you give it a domain name, then it defaults to port 443 and redirects HTTP on port 80 to HTTPS.
- mnstngr 3y agoThank 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.
- mbakke 3y agoCaddy 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 :-)
- ssddanbrown 3y agoI wrote something like this [1] for the team at my old workplace. We mainly worked on static html files but some functionality required loading via http. Many of the devs were quite new to development in general, so I built a simple static server in Go, as an exe which they could set as the default for `.html` files, and therefore they could just open html files on the webserver via double-click as normal. The program would watch the directory (if not already open and watching) then open the right path in the default browser. I also built-in livereload, management via tray icon and a basic web UI. [1] https://github.com/ssddanbrown/webby https://github.com/ssddanbrown/webby
- flashgordon 3y agoAwesome. There is a very surreal joy in building such standalone tools even just for learning. I have been working on a kitchen sync style "chat" app to experiment with new ideas (like integration with X/y/z). I just need to muster the courage to publish it!
- bbkane 3y ago100% agree. I've been "reinventing the wheel" (my own terminal color library, CLI framework, GNU Stow alternative, etc) and loving it! I get a lot of satisfaction from using these simple tools [0]. I say publish your work. Chances are, no one will care and you have a nice backup of your code. And if someone does find it useful, then great! https://github.com/bbkane/ https://github.com/bbkane/