Web Programming
Nowadays, RESTful API consumed by a single page application is a popular way to deliver product functionality over the Internet. This approach is more preferable if the user needs to do many state-changing operations to the application, because it will fe
- PDF / 373,203 Bytes
- 23 Pages / 504 x 720 pts Page_size
- 28 Downloads / 211 Views
Web Programming Nowadays, RESTful API consumed by a single page application is a popular way to deliver product functionality over the Internet. This approach is more preferable if the user needs to do many state-changing operations to the application, because it will feel more responsive. However, if the application is meant to be mostly consumed, such as forums and blogs, then having a more traditional, MVC-style web application could be more preferable because it is simpler. In this chapter, we will learn about building an MVC web application.
Serving Multiple WAI Applications We can represent our RESTful API web application and our MVC web application as two different WAI applications. We will then use a WAI middleware to route the request to the right application. So, we will structure our HTTP module of the application as follows: API/ Auth.hs Common.hs Main.hs Web/ static/ images/ logo.png Auth.hs Common.hs Main.hs Common.hs Main.hs
© Ecky Putrady 2018 E. Putrady, Practical Web Development with Haskell, https://doi.org/10.1007/978-1-4842-3739-7_8
165
Chapter 8
Web Programming
Anything under the API folder is only related to RESTful API, while anything under the Web folder is only related to the web MVC part. Both folders contain a Common module. The Common module here is meant to be shared by other modules within the same folder. For example, Common in the API folder might contain a function to build a JSON response, while Common in the Web folder might contain a function to build HTML layouts. There is also a Common module at the top level. This Common module is meant to be shared by both API and Web. It contains functions like setting a cookie. Main modules are responsible for running the application. In API, the Main module is responsible for creating the RESTful API application. The same thing also happens in the Main module under the Web folder. The Main module at the top level depends on both Main modules and sets up the necessary logic to route the request to the correct application. Now that we have understood the overall architecture, let’s hit our first milestone of properly routing the request to the correct application. First off, list the required dependencies in package.yaml: dependencies: –– blaze-html –– digestive-functors-blaze –– digestive-functors-scotty –– wai-middleware-static –– warp blaze-html1 is a package to help in building HTMLs. digestive-functors-blaze2 is a package to bind digestive-functors’s View to HTML defined with blaze-html. digestive-functors-scotty3 is a package to do form validation with scotty. wai- middleware-static4 is a package to serve static assets. We could use it to serve favicon, CSS, or javascript files. In our case, we will only use it to serve favicon. warp is a package to run a web server that is compatible with wai specification. Internally, scotty uses this. However, since we want to serve Web and API separately, we will need this package.
w ww.stackage.org/lts-10.3/package/blaze-html-0.9.0.1 ht
Data Loading...