Web server on Linux hardware



  • So, I want to build a web app. I'm not jinxing it by saying what it's going to do, but I imagine a standard RESTful architecture with heavy JS on the client and JSON endpoints on the server. The app in question is also somewhat involved with file management and some other non-Web-related stuff, and obviously needs a DB.

    Normally, I'd do it in ASP.NET MVC and not give it a second thought, but the problem is, I want it to run on Raspberry Pi. From what I've heard, ASP.NET support is dodgy at best in Raspbian, and even then, the performance is sluggish.

    So basically, I want all the coolness of C#/.NET, but working on Linux hardware. What stack do you guys suggest?


  • sockdevs

    Unless things have changed recently, I believe your only real option for C# on Linux is Mono



  • I meant "or something else, but with similar coding style and features to C#", not necessarily "C# and ASP".


  • Winner of the 2016 Presidential Election

    I don't think there's anything, really?

    PHP, Ruby, Python, Node. Did I miss any other major player?

    EDIT: Oh, yes, Java, thanks @RaceProUK. Yeah, my brain is blocking any knowledge of Tomcat, I think... shudders


  • sockdevs

    Hmm… the closest language I know to C# is Java… guess that's not really a good choice though :laughing:

    As @Onyx said, you're really looking at something like Python or Node.js; never used Python, but Node's OK once you get your head around CPS.



  • @RaceProUK said:

    Hmm… the closest language I know to C# is Java… guess that's not really a good choice though

    Right... I wouldn't mind if it wasn't RPi. If ASP is going to be sluggish, then probably so would Java. It's a 1gig version, but still.

    Maybe I'll try to learn Node... it seems hellish for big projects, but oh well, it's cool and new.


  • sockdevs

    servercooties.com runs on Node; it's not necessarily the easiest example to understand if you're not familiar with Node, but it's a fairly small codebase, so hopefully it isn't too difficult to work out.

    Though, to be honest, it's probably better to read through a few Node tutorials first ;)



  • @RaceProUK said:

    Though, to be honest, it's probably better to read through a few Node tutorials first

    I know jQuery, I'll be fine, right?

    The code looks like any JS code, a bloody mess. I'll get to those tutorials...


  • sockdevs

    @RaceProUK said:

    but it's a fairly small codebase, so hopefully it isn't too difficult to work out.

    discopaedia.com is a amaller code base still needs better documentation of course i'm slowly working on that.

    node is pretty nice for light to moderate IO bound web stuff. pair it with varnish and maybe some on server load balancing (running multiple instances on the one box and load balancing them) and it can handle heavy IO bound web load pretty well too.

    down side is it's javascript so you get to deal with that. also the designers of nodejs designed the library in such a way that using Continuation Passing Style coding is practically mandatory. I'm actually okay with that given that they wanted to change nothing about V8, just slap a bunch of libraries in, and because it's a super powerful way to program, but it does tend to lead to callback hell.

    if you go that route take a moment to look at the async library which solves a lot of the callback hell issues.

    Python is another option and has a much longer history of being used to serve web things. there are a number of awesome web frameworks for it ranging from the minimalist web.py all the way up to the "and the kitchen sink too" django



  • If you're starting node.js fresh. I suggest you immediately jump into io.js and ES6.

    Also, you'll screw up your first app. Guaranteed. If this is something important, I'd stick with what you know and use node in a throwaway project first.

    If you want something "cool", ruby and python are contenders too. For example, ruby + some lightweight framework, like Sinatra (which was inspiration for Nancy and ASP.NET Web API).

    I'm learning python myself, and it's meh. 2.x feels creaky, 3.x still unsupported. It's OK, but I like node better.


  • Winner of the 2016 Presidential Election

    @Maciejasjmj said:

    The code looks like any JS code, a bloody mess.

    Well yeah, except lots and lots of async because Node :P



  • @cartman82 said:

    ruby + some lightweight framework, like Sinatra

    might be the way to go if you don't want to go far from C#/ASP

    node is good, but, as others said, CPS will cause you a couple of headaches until you master it.

    PHP 5.5~ + a good framework may be an option too.

    EDIT: or c++ http://www.deitel.com/books/cpphtp4/images/cppHTP4_large.jpg :trolleybus:



  • For a Raspberry Pi, Node.js is probably your best bet. I know it has Apache/PHP support but from what I hear over on their official forums, Apache is extremely slow on that device.

    And except for the callback hell part, Node.js is pretty decent for a new hipster platform. Documentation is good, performance is good, libraries are good, and the package manager is good.



  • @Maciejasjmj said:

    So basically, I want all the coolness of C#/.NET, but working on Linux hardware. What stack do you guys suggest?

    Other than mono, that doesn't exist.

    How does it run Python or Ruby? Both of those languages are decently "cool", have a lot of established web toolkits, etc. (Ruby's better. Also IronRuby if that's still around adapts Visual Studio pretty well into a Ruby IDE.)

    The other option is to go far-out-astro-nuts and do something like Go or Rust. (Rust's better. Both have many many problems.) Maybe eschew the web server altogether, and just have your compiled app serve-up the API directly.



  • Python exists on the Pi, I'm currently using it to prototype a hardware project since it has a GPIO library. No idea about Ruby but I assume it exists.



  • Write it in Go.

    Failing that, write it in BIT.


  • mod

    You can run just about anything you'd put on a linux server on the pi... so long as you don't care about performance ;)

    So Ruby is a yes as well.



  • @blakeyrat said:

    The other option is to go far-out-astro-nuts and do something like Go or Rust. (Rust's better. Both have many many problems.) Maybe eschew the web server altogether, and just have your compiled app serve-up the API directly.

    Yikes, no. It's not an Arduino, it can handle a web server.

    @Yamikuronue said:

    So Ruby is a yes as well.

    Well, it can be installed, but will it blendnot fall down under load? I'm not going to be hosting another Google, but it's still a 1GB, 1Ghz machine.


  • mod

    @Maciejasjmj said:

    will it not fall down under load?

    That I don't know. All my server-style projects I've done on the pi are for internal use only within my household, so the load is minimal. I haven't used ruby at scale either, so I don't know how it does with performance.


  • sockdevs

    @Yamikuronue said:

    I haven't used ruby at scale either, so I don't know how it does with performance.

    Discourse is written in Ruby…


  • mod

    I should say, I haven't used Ruby to develop at scale. I've not sure competent developers would produce Discourse-level performance gaffs writing in Ruby ;) :trolleybus:



  • @Yamikuronue said:

    I've not sure competent developers would produce Discourse

    True.

    :passport_control:



  • @Maciejasjmj said:

    Yikes, no. It's not an Arduino, it can handle a web server.

    If you use node, you won't need a web server.



  • @Maciejasjmj said:

    Yikes, no. It's not an Arduino, it can handle a web server.

    If it's a REST API, it's not exposed to the actual web, and you're not serving up files, and you're running on low-performance hardware, you might seriously consider not bothering with a web server. HTTP's a really simple protocol to get right.



  • @Maciejasjmj said:

    So Ruby is a yes as well.

    Well, it can be installed, but will it blendnot fall down under load? I'm not going to be hosting another Google, but it's still a 1GB, 1Ghz machine.

    I'm pretty sure ruby can't scale past small-to-mid size. A bunch of successful startups that started with ruby, later had to transition to something faster as they overgrew it.

    But at your scale, the ease of development trumps performance, so it's a viable option.



  • I'd use Java. It is super trivial to create a REST service in Java.



  • If you really wanted to do Java similar to ASP.NET MVC, you'd probably want:

    • Gradle or Maven to do dependency management.
    • For REST services, a JAX-RS implementation, such as Jersey. May already be provided by the JavaEE server.
    • Spring MVC to provide the MVC stuffs.

    Optional stuff includes

    • A JPA provider such as Hibernate, unless you want to do raw JDBC calls.
    • A template library such as Thymeleaf, unless you really want to use JSP as the views.

    Note: I was going to include a JSON parser here, but JAX-RS should already have one built-in... you just have to set the @Produces or @Consumes mime-types to include "application/json"


  • Discourse touched me in a no-no place

    @RaceProUK said:

    Node's OK once you get your head around CPS.

    What's CPS mean in this context?


  • sockdevs

    Continuation Passing Style



  • The style of writing JavaScript that makes me go :wtf:


  • Discourse touched me in a no-no place

    From the trivial examples on wikipedia I would agree.



  • It's really not that bad once you get used to it. I think of the passed function as an OnEventComplete handler, and it almost always works out. Most methods will also accept an error handler. no biggie.


  • sockdevs

    @loopback0 said:

    The style of writing JavaScript that makes me go

    it does that to me to.

    and i program in it for fun!


  • sockdevs

    You crazy!

    ...wait... I program in it for fun too...


  • Winner of the 2016 Presidential Election

    @blakeyrat said:

    Ruby's better[ than python].

    False.


  • Winner of the 2016 Presidential Election

    @powerlord said:

    Gradle or Maven

    You should probably use Gradle from the start, because if your build is ever going to get more complex than "compile all my shit at once and produce a JAR/WAR", you will start to hate Maven and its XML files pretty quickly.


  • Winner of the 2016 Presidential Election

    @jaming said:

    I'd use Java. It is super trivial to create a REST service in Java.

    I'd say yes, except Raspberry Pi...



  • it's not so bad. it almost feel like doing haskell, but without the cool language constructions to support it :stuck_out_tongue:



  • Still running... Still no output.

    Edit:


  • Discourse touched me in a no-no place

    @ben_lubar said:

    Still running... Still no output.

    So how long, Señor Quixote, do you intend to keep that running?





  • @ben_lubar said:

    Still running... Still no output.

    Edit:

    How do you go that long without upgrading a video card or HD or something?



  • It's on a Core 2 Duo and it doesn't use any disk IO or GPU stuff.



  • Wait, do you mean the uptime thing? It's because it's Linux and it can stay running when parts of it get updated.



  • Go actually has tutorials that are writing a simple HTTP server.

    You start it like this:

    package main
    
    import (
      "encoding/json"
      "net/http"
      "net/url"
    )
    
    type FooResponse struct {
    	Id, Count int
    	Name string
    }
    
    func fooDirectoryHandler(w ResponseWriter, req *Request) {
    	// code for /foo/* goes here....
    
    	var resp FooResponse
    	resp.id = 3; resp.count = 5; resp.name = "Example"
    
    	enc := json.NewEncoder(w)
    	enc.Encode(resp)
    }
    
    func main() {
    	http.Handle("/foo/", fooDirectoryHandler)
    
    	http.HandleFunc("/bar", func(w http.ResponseWriter, r *http.Request) {
    		fmt.Fprintf(w, "Hello, %s", html.EscapeString(url.QueryUnescape(r.URL.Path)))
    	})
    
    	log.Fatal(http.ListenAndServe(":8080", nil))
    }
    


  • @riking said:

    fmt.Fprintf(w, "Hello, %q", html.EscapeString(r.URL.Path))

    Why are you Go-escaping an HTML-escaped URL-escaped string?



  • @riking said:

    Go actually has tutorials that are writing a simple HTTP server.

    Yeah, but then you're using Go. :frowning:



  • @ben_lubar said:

    Why are you Go-escaping an HTML-escaped URL-escaped string?

    Fixed

    also, added package main, it's now a complete program

    @loopback0 said:

    Yeah, but then you're using Go.

    zoom zoom coroutines multithreading



  • You're still Go-escaping it for no reason.



  • Anyways, the point is, you can run this program on Linux or Windows.

    $ GOOS=windows go build
    $ go build
    $ ls
    webserver webserver.exe webserver.go

Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.