A few words first.
One of the projects I liked to realize was a rewrite of my own small-business website, which I had jumbled together years ago. The site should be responsive and have some functionality. By looking to build it in Haskell I felt using the Yesod-Webframework the most convincing way to go. Yesod is a big framework, very fast and well constructed. For building only one website maybe a bit of overdo, you might argue. To learn Haskell in a practical way, it is actually the contrary. As you will see we "move on inside it's structure" full of good examples and best practices.
I assume that you use linux.
While we are learning a bunch of things here, I suggest to take notes in some way. One good per project related examples is depicted below, but for more general approach we will do well using something like Zim
Ok, let's jump in and prepare our environment.
Take the Terminal of your choice and edit the hidden .bashrc file in your home-directory. With
you may switch to view hidden files in your file manager.
In my case, to edit it (I use vim as an editor), the command will be
Inside .bashrc we will set some aliases and prepare a route (PATH) that our Haskell-Environment, which we install later on will use.
Aliases are a nice way to make live more comfortable. As from now often using a Terminal, we alias the ls -lha command with a simple ll. Move to the
# some more ls aliases
section and insert
alias ll='ls -lha'
save the file and exit. If using the nano-editor you have to do Ctrl x and confirm with y. Take care not to mistype.
Now we can restart the .bashrc by giving
If you now type
in your Terminal, you will get a nice list of files in the directory, if there are any. This is only true for your actual user.
Other useful entries in .bashrc might be:
now gives you a list of commands you have used. By typing
and the respective number of the command you like to (re-)use, you will execute that command directly. Take care!
Another nice trick to find history commands is using
which change your shell prompt and let you (reverse-)search by letters.
Now we go to the very end of .bashrc and enter
where /home/ye is my home-directory. Change accordingly. This allows our Haskell-Environment to work properly.
we may insert also
where n calls a really useful and lightweight commandline note and logging tool. It is written by Justin Le
who runs a very interesting blog for mostly advanced topics. Follow his instructions to install.
If you want to dig deeper in bash and how to configure
Next we will install the Geany-Texteditor. (Feel free to use anything else, but please give Geany a try.)
sudo apt install geany
The Geany wiki is located here:
So let's move on.
As quite a lot of data everywhere even our blogpostarticles will be saved in a database. For to keep things reusable, also for non-web projects, Postgresql may be the best fit. It is easy to install. In Debian based systems we insert
sudo apt install postgresql
into a terminal.
After it installed
sudo su - postgres
we enter the database editor and are now ready to insert something like:
CREATE USER blogpost WITH PASSWORD 'blogpost'; CREATE DATABASE blogpost; GRANT ALL PRIVILEGES ON DATABASE blogpost TO blogpost;
So our database here has the name blogpost and the user blogpost with the password blogpost. Feel at least urged to change the password. The settings for USER and PASSWORD are crucial for the persistent part of our App, so better take them down somewhere.
Don't be afraid if you get an error from Postgres. Database is hard and severe. The above is true for Postgresql 11 and is all the preparation we need for our BlogPost.
you may exit psql and with
you may exit postgres.
Ok, now let's jump to the Haskell side of things:
Read the page carefully before executing any command. You might have to install curl from your distribution repositories and the indicated libraries if not already done so. For now this site gives us all we need, especially a link to the Yesod book, which is recommended to read.
The author of the book and creator of the Yesod Webframework, Michael Snoymann has done a fantastic work in both and is one of the most recognized Haskell developers.
So, time to set up a working directory. Let's name it blog and have it inside our home directory. In a terminal, inside the blog directory we execute
curl -sSL https://get.haskellstack.org/ | sh
In the next step, instead of picking the sqlite-template, we choose to install
stack new blogpost yesodweb/postgres && cd blogpost
Time for a break? Aah, yeah later maybe. Let's dig in a bit deeper:
will certainly go to your Haskell-Bookmarks, as you will need more information about Stack later on. For now, it is fine that we use Stack to install our enviroment.
stack install yesod-bin --install-ghc
You may notice that
Copied executables to /home/ye/.local/bin: - yesod ...
is printed by Stack.
At time of writing this, stack select the (package-)resolver: lts 14.27. Every resolver-version meets with a certain Ghc-version. From
you get an overview.
Will let you learn some things about Ghc. With Ghc, and here I am quite certain, you will begin a most ambivalent relationship. Hint: consider to let him become a helpful friend.
And after that
stack exec -- yesod devel
will need some time to finish, but then, uuh check:
we get an error! And what a bold one. May this have to do with the password not known by our Yesod application but only by the database? Yup, if we look into config/settings.yml we find in the database section exactly what we mean and change
according to. Save the file and
let's get it going...
stack exec -- yesod devel
in your browser... and yeah, there we are.
All well? Thanks for reading.
If you like to go on: