For today's deposit into the Google karma bucket, I'm going to run through the setup for a super duper simple Cowboy web server so you can move past the plumbing stage faster. I started from the BigWig project and stripped stuff away until I had a web server that would serve static pages and 404s.
Pre-requisites (Blogger, why do I have to bold this instead of just selecing an <H2>?)
Erlang (R15B), rebar, Unix-y OS (Mac OSX in my case)
Process
Setup
Set up directories and copy over rebar binary
Create rebar.config in your project directory
Run rebar commands to create a basic server and release. It's easier to name everything the same as the project directory than it is to figure out what to change in the configs.
Modify rel/reltool.config so that lib_dirs points to the directory that contains the src directory - ["../.."]
Make a simple web server
Add 5 new files and modify one of the generated files. I think there's a way to have rebar generate all of this, but I'm not going to bother figuring that out until I've got a better sense of what all I want to have generated for me on future projects.
Create src/simple_server.erl
Create src/simple_server_http.erl
Create src/simple_server_http_catchall.erl
Create src/simple_server_http_static.erl
Create priv/html/index.html
Modify the init/1 function in src/simple_server_sup.erl to include your HTTP handler
Build and test
The following will pull down Cowboy, compile all your files, and generate a release that you can run:
./rebar get-deps compile generate
From the rebar tutorials I've seen, you should be able to call:
./rel/simple_server/bin/simpler_server start
This call returns without error and if you look at your processes it looks like something's going on, but there'll be nothing for you in your web browser.
Instead, call the following the start up your server:
sudo erl -sname simple_server -pa $PWD/ebin $PWD/deps/*/ebin -boot start_sasl -s simple_server
Make sure to call it with sudo since we have the port set to 80. If you set it to something like 8000 you don't have to call it with sudo.
Open up http://127.0.0.1 in your browser and celebrate. Open up http://127.0.0.1/blah in your browser and celebrate some more.
More
Yay, now we have a lame server. Next up, we need to add templating and a datastore to do more interesting stuff. See part 2.
This comment has been removed by the author.
ReplyDelete// , For a basic setup, this seems somewhat complex.
ReplyDeleteThis comment has been removed by the author.
ReplyDelete