5 ms·
Here's a quick attempt at the same thing in Java (although note that the command line format is different): https://gist.github.com/tomwhoiscontrary/b4888b8605
by twic 9y ago
Here's a quick attempt at the same thing in Java (although note that the command line format is different):
https://gist.github.com/tomwhoiscontrary/b4888b86057c74a636c455235c756354 https://gist.github.com/tomwhoiscontrary/b4888b86057c74a636c...
The main takeaway is that the JDK's built-in web server is poor:
* There is no way to configure timeouts
* Filters have to be added to each handler separately, by mucking with its filter list
* Filters have to extend an abstract class with two methods (one totally pointless), so you can't use lambdas for them
* Handlers use a simple string prefix match, so a request for "/healthzone" will hit the health handler
* The stop method always blocks for the specified timeout, and never returns early as the docs promise (IME)
Other minor irritations:
* There is no method to parse an InetSocketAddress from a string
* Java's support for handling clean process shutdown is not great; i think there's some sort of race between the shutdown hook and the logging system getting shut down, so you never see the "Server stopped" message
- meddlepal 9y agoYea, the built-in JDK HTTP server is pretty poor, but I don't think I've ever run across it in the wild thankfully. > There is no way to configure timeouts They're configured by properties if I recall. > Filters have to be added to each handler separately, by mucking with its filter list I personally prefer the explicitness of this. As for the rest... I think a lot of these issues stem from the fact it's an old, not particularly well-known API in the JDK.
- setheron 9y agoJust wrap the handlers with middleware rather than using that filter abstraction. Same thing!
- twic 9y agoBy 'middleware', do you mean another handler? That would work, but i'd still have to wrap each handler separately.
- skanga 9y agoNice. Does anyone have a server like this but with the addition of * serving of files and directories * LetsEncrypt certificates and SSL
- deleted 9y ago[deleted]
- wyattjoh 9y agoThe builtin golang.org/x/crypto/acme/autocert [0] package manages SSL for you easily through Lets Encrypt. [0]: https://godoc.org/golang.org/x/crypto/acme/autocert https://godoc.org/golang.org/x/crypto/acme/autocert
- TheDong 9y agogolang.org/x/ packages are not builtin. You still must download them and use them just as you do any other dependencies, they just share some contribution process and developers with golang's stdlib.
- sd8dgf8ds8g8dsg 9y agostatic file server in go: package main import ( "fmt" "net/http" "path/filepath" ) var ( host = "0.0.0.0" path = "/var/www/public" port = 8080 ) func main() { path, err := filepath.Abs(path) if err != nil { panic(err) } listenAt := fmt.Sprintf("%s:%v", host, port) fmt.Println("static-serve-dir serving", path, "over http at", listenAt) err = http.ListenAndServe(listenAt, http.FileServer(http.Dir(path))) if err != nil { panic(err) } }
- SEJeff 9y agoIf you're using java, just use finagle's http server and be done with it. It is really good and runs most all of twitter.com's production web services. The very definition of "web scale": https://twitter.github.io/finagle/guide/Quickstart.html https://twitter.github.io/finagle/guide/Quickstart.html
- michaelmcmillan 9y agoNot sure if I would use Twitter as a success story for handling scale. Not exactly the poster boy of reliable software.
- ianamartin 9y agoReally good; web scale; reliable. Pick two.
- SEJeff 9y agoYou're kidding, right? After they ditched ruby and wrote finagle, they've been super reliable. They've got one of the biggest Apache Mesos clusters in the world (second only to Apple's Siri backend per Apple employees who presented at MesosCon) and run it all with Apache Aurora, a really nice mesos framework for long running jobs. Twitter, Soundcloud, Salesforce, Pinterest, FitBit, Tumblr, Box, Foursquare... What do they all have in common? They all run finagle in production. For java, it is about as battletested and as good as you'll get unless you want to write your own. Personally, I'm more of a golang and python developer, but respect where it is due. Twitter is massively more scalable than the overwhelmingly majority of the sites on the internet and they've gotten their act together since abandoning Ruby and the fail whale.
- xstartup 9y agoIs it Java or Scala? I only see Scala on this page: https://twitter.github.io/finagle/guide/Quickstart.html#a-minimal-http-server https://twitter.github.io/finagle/guide/Quickstart.html#a-mi...
- 9y ago
- enricofoltran 9y agoHi twic, thanks for sharing this! I love to see implementation in other languages. I'm particularly interested in Rust, it would be awesome if anyone can make a attempt! :)
- AsyncAwait 9y agoThat would be awesome, but note that Rust, due to it being a low-level language, doesn't really have a net/http equivalent in its standard library and therefore any implementation would be more of a representation of the 3rd party libraries used.
- rb808 9y agoJetty does everything, else Spring with tomcat. I don't see any reason to rely on JDK web server (which I've never seen anyone seriouosly use.
- dboreham 9y agoI've been developing in Java for around 20 years and I never knew the JDK included a web server. This must be the thing I learned today ;)