Skip to content

coast-framework/coast

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

What is this?

Coast is a full stack web framework written in Clojure for small teams or solo developers. It uses a relational database and renders HTML on the server without Javascript which allows you to ship your web applications faster.

(ns server
  (:require [coast]))

(def routes [[:get "/" ::home]])

(defn home [request]
  "You're coasting on clojure!")

(def app (coast/app {:routes routes}))

(coast/server app {:port 1337})

🚨 New version alert 🚨

Check out the next branch for the changes coming to coast v1.0

Discussion

Feel free to ask questions on the coast gitter channel.

Docs

More comprehensive docs are available here

Quickstart

If you don't want to read the docs, and just want to jump in, you're in the right place.

Installation on Mac

  1. Make sure clojure is installed first
brew install clojure
  1. Install the coast cli script
curl -o /usr/local/bin/coast https://raw.githubusercontent.com/coast-framework/coast/master/coast && chmod a+x /usr/local/bin/coast
  1. Create a new coast project
coast new myapp && cd myapp

Installation on Linux (Debian/Ubuntu)

  1. Make sure you have bash, curl, rlwrap, and Java installed
curl -O https://download.clojure.org/install/linux-install-1.9.0.391.sh
chmod +x linux-install-1.9.0.391.sh
sudo ./linux-install-1.9.0.391.sh
  1. Install the coast cli script
sudo curl -o /usr/local/bin/coast https://raw.githubusercontent.com/coast-framework/coast/master/coast && sudo chmod a+x /usr/local/bin/coast
  1. Create a new coast project
coast new myapp && cd myapp

This will take you from a fresh coast installation to a working todo list app.

New Project

The first thing you do when you start a coast project? coast new in your terminal:

coast new todos

This will make a new folder in the current directory named "todos". Let's get in there and see what's up:

cd todos
tree .

This will show you the layout of a default coast project:

.
β”œβ”€β”€ Makefile
β”œβ”€β”€ README.md
β”œβ”€β”€ bin
β”‚Β Β  └── repl.clj
β”œβ”€β”€ db
β”‚Β Β  β”œβ”€β”€ db.edn
β”‚Β Β  └── associations.clj
β”œβ”€β”€ deps.edn
β”œβ”€β”€ env.edn
β”œβ”€β”€ resources
β”‚Β Β  β”œβ”€β”€ assets.edn
β”‚Β Β  └── public
β”‚Β Β      β”œβ”€β”€ css
β”‚Β Β      β”‚Β Β  β”œβ”€β”€ app.css
β”‚Β Β      β”‚Β Β  └── tachyons.min.css
β”‚Β Β      └── js
β”‚Β Β          β”œβ”€β”€ app.js
β”‚Β Β          └── jquery.min.js
β”œβ”€β”€ src
β”‚Β Β  β”œβ”€β”€ components.clj
β”‚Β Β  β”œβ”€β”€ home.clj
β”‚Β Β  β”œβ”€β”€ routes.clj
β”‚Β Β  └── server.clj
└── test
    └── server_test.clj

Start the dev server

Type this in your terminal in your project directory

make server

and visit http://localhost:1337 to see the welcoming coast on clojure default page ✨

This should also create a sqlite database with the name of the database defined in db.edn and the COAST_ENV or :coast-env environment variable defined in env.edn.

Migrations

Now that the database is created, let's generate a migration:

coast gen migration create-table-todo name:text finished:bool
# db/migrations/20190926190239_create_table_todo.clj created

This will create a file in db/migrations with a timestamp and whatever name you gave it, in this case: create_table_todo

(ns migrations.20190926190239-create-table-todo
  (:require [coast.db.migrations :refer :all]))

(defn change []
  (create-table :todo
    (text :name)
    (bool :finished)
    (timestamps)))

This is Clojure, not SQL, although plain SQL migrations would work just fine. Time to apply this migration to the database:

make db/migrate
# clj -m coast.migrations migrate
#
# -- Migrating:  20190310121319_create_table_todo ---------------------
#
# create table todo ( id integer primary key, name text, finished boolean, updated_at timestamp, created_at timestamp not null default current_timestamp )
#
# -- 20190310121319_create_table_todo ---------------------
#
# 20190310121319_create_table_todo migrated successfully

This updates the database schema with a todo table. Time to move on to the clojure code.

Generators

Now that the database has been migrated, this is where coast's generators come in. Rather than you having to type everything out by hand and read docs as you go, generators are a way to get you started and you can customize what you need from there.

This will create a file in the src directory with the name of a table. Coast is a pretty humble web framework, there's no FRP or graph query languages or anything. There are just files with seven functions each: build, create, view, edit, change, delete and index.

coast gen code todo
# src/todo.clj created successfully

Coast uses a library under the hood called hiccup to generate HTML.

Routes

One thing coast doesn't do yet is update the routes file, let's do that now:

(ns routes
  (:require [coast]))

(def routes
  (coast/site
    (coast/with-layout :components/layout
      [:get "/" :home/index]
      [:resource :todo]))) ; add this line

The routes are also clojure vectors, with each element of the route indicating which HTTP method, url and function to call, along with an optional route name if you don't like the namespace/function name.

[:resource :todo] sets up basic CRUD routes in one line.

Start the server

From the command line

Let's check it out from the terminal. Run this

make server

and visit http://localhost:1337/todos to see the app in action.

From the REPL

I currently use atom with parinfer and chlorine as my REPL client, check it out if you want a smooth clojure experience.

First run, the repl socket server:

make repl

Then in your editor, connect to the repl server.

In atom with chlorine for example:

Press space + c, fill in the port with 5555 and hit Enter.

After you're connected, load the server.clj file with Chlorine: Load File.

Finally, move your cursor to (-main) and evaluate with Cmd+Enter.

Navigate to http://localhost:1337/todos and check out your handiwork.

Tested on Different Platforms

Tested on Clojure 1.10.0 on MacOS using brew to install Clojure

readline versions might clash depending on your setup. You might need to downgrade to a lower version of readline depending on your version of Clojure. For example... readline version 7.0 for clojure 1.9

Contributing

Any contribution is welcome! Submit a PR and let's get it merged in!

License

Coast on Clojure is released under the MIT License.