You are previewing Clojure Web Development Essentials.
O'Reilly logo
Clojure Web Development Essentials

Book Description

Develop your own web application with the effective use of the Clojure programming language

In Detail

Functional programming enables developers to create and deploy applications quickly and easily. Using the Clojure programming language and the Leiningen build tool, this book teaches you how to leverage various Clojure libraries into the building blocks of a web-based application.

We'll start by generating a new, fully runnable, baseline web application using the Leiningen Luminus template. From there we'll examine the different pieces that compose our application, and learn the basics of the underlying Ring framework. Subsequent chapters will focus on each technology and leverage those technologies to build additional functionality into our application. You'll learn how to validate user submitted data, authentication, account creation, how to interact with a database, and more. Finally, we'll create an environment configuration and deploy our app.

By the end of this book you'll have created a full fledged application from start to finish, and you'll have the requisite skills to develop an efficient and robust web application.

What You Will Learn

  • Generate a fully runnable web application using the Luminus Leiningen application template

  • Explore the basics of the underlying Ring framework and the Ring Server

  • Configure URL Routing, Logging, and some testing basics

  • Create new web pages using the Selmer template rendering library

  • Validate user-submitted form data

  • Store and retrieve data to and from a database

  • Configure, package, and deploy the finished application

  • Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.

    Table of Contents

    1. Clojure Web Development Essentials
      1. Table of Contents
      2. Clojure Web Development Essentials
      3. Credits
      4. About the Author
      5. About the Reviewers
      6. www.PacktPub.com
        1. Support files, eBooks, discount offers, and more
          1. Why subscribe?
          2. Free access for Packt account holders
      7. Preface
        1. What this book covers
        2. What you need for this book
        3. Who this book is for
        4. Conventions
        5. Reader feedback
        6. Customer support
          1. Downloading the example code
          2. Errata
          3. Piracy
          4. Questions
      8. 1. Getting Started with Luminus
        1. Leiningen
          1. Using Leiningen
          2. Generating the application
          3. Getting help
        2. Dependencies of the app
        3. Luminus file structure
          1. util.clj
          2. session_manager.clj
          3. layout.clj
          4. middleware.clj
          5. routes/home.clj
          6. handler.clj
          7. repl.clj
        4. Summary
      9. 2. Ring and the Ring Server
        1. Understanding Ring in Clojure
          1. Request maps
          2. Response maps
          3. Handlers
          4. Middleware
          5. Adapters
        2. What is the Ring Server?
          1. hipstr.handler
            1. Initialization hooks
            2. Shutdown hooks
            3. App routes
            4. The application handler
          2. hipstr.repl
            1. Start-server
            2. Stop-server
            3. Get-handler
        3. Configuring and running the Ring Server
        4. Summary
      10. 3. Logging
        1. What is Timbre?
          1. What is an appender?
        2. Configuring a Timbre appender
          1. Timbre log levels
          2. Appender configuration keys
          3. Appender map
          4. Shared appender configuration
        3. Logging with Timbre
        4. Adding an appender
          1. Adding the rolling appender
        5. Summary
      11. 4. URL Routing and Template Rendering
        1. What is Compojure?
        2. Creating a Compojure route
          1. Using defroutes
          2. Anatomy of a route
            1. Defining the method
            2. Defining the URL
            3. Parameter destructuring
              1. Destructuring the request
              2. Destructuring unbound parameters
            4. Constructing the response
            5. Generating complex responses
        3. What is Selmer?
        4. Creating your first page
          1. Rendering a page
            1. Variables
            2. Filters
            3. Filter parameters
            4. Tags
          2. Template inheritance
        5. Editing the home page
        6. Serving the signup form
        7. Creating the signup page
        8. Summary
      12. 5. Handling Form Input
        1. Handling the form POST
        2. Validating the form POST
          1. The noir.validation namespace
          2. The Validateur library
            1. Adding the Validateur dependency
            2. Creating the user validation namespace
            3. Validating required fields
            4. Validating the format
            5. Validating length of values
            6. Validation predicates
            7. Making reusable validators
          3. Reporting errors to the user
        3. Summary
      13. 6. Testing in Clojure
        1. The necessity of testing
        2. Anatomy of a test
        3. Writing and running our first test
          1. Running tests
          2. Running tests automatically
          3. Refactoring tests
        4. Writing a high-level integration test
          1. Using ring.mock.request
        5. Summary
      14. 7. Getting Started with the Database
        1. Creating the database schema
        2. Maintaining the database schema
          1. Migratus
          2. Getting Migratus
          3. Configuring Migratus
          4. Creating the user table
          5. Dropping the user table
            1. Running the down scripts
          6. Migrating the database
            1. Running all migration scripts from Leiningen
            2. Running migrations programatically
            3. Adding migrations to the hipstr initialization
        3. Adding data to the database
          1. What is YeSQL?
          2. Getting YeSQL
          3. Adding a user to the database
            1. Inserting a user using SQL
            2. Inserting a user using Clojure
          4. Bringing it all together
            1. Adjusting the route
            2. Encrypting the password
        4. Summary
      15. 8. Reading Data from the Database
        1. Creating the catalog
          1. Creating the artists table
            1. Seeding the artists table
          2. Creating the albums table
        2. Fetching albums from the database
          1. Writing the SQL query
          2. Creating the albums model
          3. Refactoring the connection
        3. Creating the recently added route
        4. Rendering the results
        5. An exercise!
        6. Summary
      16. 9. Database Transactions
        1. Introduction to Database Transactions
        2. The ACID properties
        3. Importance of database transactions
        4. Implementing a transaction
          1. Transactions in Clojure
          2. Transactions in YeSQL
        5. Extending the application requirements in brief
          1. Creating the add artist/album form
            1. Creating the form
            2. Abstracting the form
          2. Creating the add artist/album endpoint
            1. Creating the Compojure route
            2. Creating the route helper function
            3. Validating the add artist/album form
          3. Expanding the album model
            1. Wrapping the whole thing in a transaction
            2. Using a transaction outside of this scope
        6. Summary
      17. 10. Sessions and Cookies
        1. Sessions
        2. Setting up sessions
        3. Interacting with the session
        4. Restricted routes
          1. Restricting route access
          2. Accessing a restricted route
            1. Access rule as a function
            2. Access rule as a map
        5. Cookies
          1. Getting a cookie's value
          2. Setting a cookie's value
            1. Setting the cookie as a map
          3. Securing a cookie
          4. Deleting a cookie
        6. Extending the application: brief requirements
          1. Creating the login form
          2. Restricting the recently-added route
            1. Restricting the route
            2. Checking if the user is authenticated
            3. Defining the access rule
          3. Authenticating the user
            1. Validating the credentials
            2. Handling the form POST
          4. Writing the "Remember Me" cookie
          5. Creating the logout route
        7. Summary
      18. 11. Environment Configuration and Deployment
        1. Environ
          1. Using environ
          2. Variable translations
        2. Setting and resolving environment configurations
          1. Resolving environment configuration
        3. Adjusting the database connection
          1. Creating the profiles.clj file
          2. Modifying the hipstr.models.connection namespace
        4. Deploying the hipstr application
          1. When to use an uberjar
          2. When to use an uberwar
          3. Deploying as a standalone
          4. Running the application behind Nginx
          5. Load balancing behind Nginx
        5. Summary
      19. A. Using Korma – a Clojure DSL for SQL
        1. Getting Korma
        2. The Quick Korma Crash Course
          1. Define the database specification
          2. Korma entities
          3. Defining the primary key
          4. Defining relationships between entities
          5. Constructing SELECT queries
          6. Constructing INSERT queries
          7. Constructing UPDATE queries
          8. Constructing DELETE queries
          9. Using raw SQL
          10. Using transactions
        3. Port the models from YeSQL to Korma
          1. Porting hisptr.models.connection
          2. Porting hisptr.models.user-model
          3. Porting hipstr.models.album-model
      20. Index