📜 ⬆️ ⬇️

Yesod = Haskell $ Web

Haskell is still the only programming language in which there is a floppy operator (>> =)

Absurdopedia


I planned to begin with a description of how people are mistaken who think Haskell is useless from a practical point of view, spherical language in a vacuum, etc. But, I'm afraid, the authors of Real World Haskell have already done everything for me. There was an idea to talk about how beautiful and powerful this programming language was, but it was summed up by a heavy syllable and inability to come up with an exciting plot with an unexpected outcome. Therefore, I will reject all unnecessary and talk about my Haskell web development misadventures.

')

Characters


I have long wanted to try Haskell for web development. First of all, it’s pretty close to my professional activities, and secondly, it’s wildly interesting. Well, we end up with a way to learn Haskell and don’t fall asleep behind the sorting and search algorithms.

Fortunately, for this idea there are a lot of ready-made frameworks (see here ) and the necessary packages (however, there are unknown mountains for everything ).
The bottom line is, I chose the Yesod Web Framework. Although it is quite common and has good functionality, I confess, the choice was made by the method of scientific tying and has little logical basis.
In addition, I wanted to be able not only to look at the results of my work locally, but also to boast to my colleagues. Therefore, it was intended to publish the results on Heroku. This will require a haskell buildpack, for example, this one (although I will inform you in advance that only he has earned in my case))

Yesod


I will try to describe everything in sufficient detail and in full - just in case. So, first you need to install the Glasgow Haskell Compiler. Any package manager:

emerge -av ghc 

Cabal wo n't hurt either :

 emerge -av cabal cabal update 

In order not to puzzle over the dependencies, you should use cabal-dev , which allows you to install all the necessary packages in the sandbox in the future (similar to virtualenv).

 cabal install cabal-dev 

Now create a stub for the first application. Actually, yesod will do everything for us:

 yesod init 

Specify the name of the application, the method of storing data (for example, in postgresql) and voila - we get a directory with a fully functioning web application. It remains only to install the dependencies using cabal-dev :

 cd MyApp cabal-dev install 

A little meditation on the console exhaust - and we are one step closer to the realization of the idea. It remains to remember to specify the configuration for the database. It can be seen in the file config / postgresql.yml - and here everything is already stolen before us thought out for us! The file contains 4 types of configurations (development, testing, staging, production) and the default configuration, from which all are inherited. It remains to specify the user name DB, password and database name.
Now start the server:

 yesod devel --dev 

The --dev option tells you to use cabal-dev - otherwise Yesod will look for packages installed on the system.
You can, with bated breath, open http: // localhost: 3000 in the browser and enjoy the rather simple, but the result.

There is one question that made me doubt my programming skills and just an adequate person. The fact is that when it came time to play around with the project, I couldn’t see the result of my efforts (even if these were changes in the templates!). It turned out that for a full restart, you need to do not

 cabal-dev build / yesod build 

or just restart the development server - you also need to reinstall the application

 cabal-dev install 

It seems to me that it is never obvious, although it is not yet clear whether this is a bug or a feature .

Heroku


The next episode will be devoted to the publication of the received web application on Heroku. All this is possible thanks to a runtime stack called Cedar . When using this stack, support for various languages ​​and frameworks rests entirely on the shoulders of buildpacks, which prepare the environment for running the application.
Heroku supports several such buildpacks, for example, for Ruby (well, who would doubt), Node.js, Python, etc. In addition to this list there is a notable number of third-party buildpacks .
Of course, we need one for Haskell, which is easily on the list above. And to my surprise, because I discovered this quite recently, and some time ago he was not there. At that time, it encouraged me to search on github and iterate over several repositories, among which I earned this one in my hands.

So, create an application on heroku:

 heroku create my-app --stack=cedar --buildpack https://github.com/<   buildpack>.git 

Next, you need to initialize the git repository and add MyApp to it. Before the deployment you need to make some changes that you can easily forget. First of all, you need to transfer Procfile from the deploy directory to the project root - this file contains information on how to start the project. After it is worth correcting the production configuration of the database and the site:

Regarding the database - naturally, first you need to add the add-on Heroku Postgres. After adding you can find out the connection string with the command

 heroku pg:info # HEROKU_POSTGRESQL_URL heroku pg:credentials HEROKU_POSTGRESQL_URL 

After the commit of all changes, specify the remote repository of heroku:

 heroku git:remote -a MyApp 

and send the changes to the server

 git push heroku master 

We are waiting ... Before deployment with the help of a buildpack, an environment will be prepared, all dependencies will be installed, etc.
And here may be a problem that makes you think. The fact is that heroku sets a limit that may not allow the completion of the deployment. In an effort to figure out how and why, I cloned the buildpack repository and examined it. Subject was asking for experiments. To be able to use buildpack locally and, therefore, immediately see the results of all modifications, you can use the heroku push plugin. In this case, the project will be produced by skipping git:

 heroku push -b ~/mybuildpack 

After making some changes:

 -export PATH=$FIXED_HOME/ghc/bin:$FIXED_HOME/.cabal/bin$PATH +export PATH=$FIXED_HOME/ghc/bin:$FIXED_HOME/.cabal/bin:$PATH -cabal install -j5 --disable-library-profiling --disable-executable-profiling --disable-shared +cabal install -j2 --disable-library-profiling --disable-executable-profiling 


To my surprise, everything worked and the deployment was successful! (heated discussion in the comments is encouraged, because I myself did not understand why it worked)

Conclusion


Yesod is an interesting and competitive framework that has a large set of features. It is interesting to look at Yesod vs Django . Before that, only the fact that none of the cloud services like GAE, OpenShift, etc., stopped me. are not going to deal with Haskell. Well, once the problem is settled, you can and even need to please yourself with the study of another interesting thing. I hope I'm not alone in my impulses)
In the future, I will try to tell you how Yesod works and works. There will be a lot of Shakespeare and other buns)

PS:
A couple of presentations about using Haskell in production
mth.io/talks/haskell-in-production
www.shimweasel.com/hs_gbu

Source: https://habr.com/ru/post/184058/


All Articles