Skip to content

Commit bee38d6

Browse files
committed
[project] setup for initial stage
1 parent 995b026 commit bee38d6

File tree

13 files changed

+263
-21
lines changed

13 files changed

+263
-21
lines changed

.github/workflows/ci.yml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
name: Tests
2+
3+
on:
4+
push:
5+
paths-ignore:
6+
- "**.md"
7+
8+
jobs:
9+
build:
10+
runs-on: "ubuntu-latest"
11+
12+
steps:
13+
- name: "Checkout code"
14+
uses: "actions/checkout@v3"
15+
16+
- name: "Setup Java"
17+
uses: "actions/setup-java@v3"
18+
with:
19+
distribution: "temurin"
20+
java-version: "19"
21+
22+
- name: "Setup Clojure"
23+
uses: "DeLaGuardo/setup-clojure@master"
24+
with:
25+
cli: "latest"
26+
27+
- name: "Apply Cache"
28+
uses: "actions/cache@v3"
29+
with:
30+
path: |
31+
~/.m2/repository
32+
~/.gitlibs
33+
key: "todos-${{ hashFiles('deps.edn') }}"
34+
restore-keys: "$todos-"
35+
36+
- name: "Run tests"
37+
run: "make test"

.gitignore

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,8 @@
1-
.clj-kondo/
2-
.lsp/
1+
.cache
2+
.clj-kondo
3+
.cpcache
4+
.lsp
5+
.nrepl-port
6+
todos.sqlite
7+
target/
8+
*.jar

Dockerfile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
FROM docker.io/eclipse-temurin:19-jre
2+
3+
WORKDIR /opt
4+
5+
COPY todos.jar .
6+
7+
ENTRYPOINT ["java", "-jar", "todos.jar"]

Makefile

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
.PHONY: test
2+
3+
test:
4+
clojure -X:test
5+
6+
start:
7+
clojure -M -m todos.main
8+
9+
uberjar:
10+
clojure -T:build uber
11+
12+
image: uberjar
13+
podman build -t docker.io/workshop/todo .

README.md

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,31 @@
11
# Babashka Workshop
22

3+
A simple TODO app backed by SQLite3 and a CLI intended for the workshop demonstrating the various features and aspects of Babashka.
4+
35
## System Requirements
46

5-
- [Babashka][babashka]
7+
Latest versions recommended for the following
8+
9+
- [Babashka][babashka].
10+
- Clojure [CLI][cli]
11+
- JDK 11+.
12+
- Docker or Podman.
13+
- [Gum][gum].
614

715
To verify things are set up properly, you can run this:
816
```
917
bb --version
1018
11-
babashka v<version-installed>
19+
clojure --version
1220
```
1321

1422
## Run
1523

16-
```
17-
git clone https://github.com/clojurestream/babashka-workshop
24+
### Server
1825

19-
cd babashka-workshop/exercies/00-run
20-
```
26+
- Run tests: `make test`.
27+
- Start app locally: `make start`. Set the env vars `TODOS_HOST` and `TODOS_PORT` to customise.
28+
- Build container image: `make image`.
2129

2230
## Workshop Feedback
2331

@@ -26,3 +34,5 @@ At the end of the workshop, please [provide short feedback][feedback-form].
2634

2735
[babashka]: https://github.com/babashka/babashka#installation
2836
[feedback-form]: https://forms.gle/iZ8YMfftWdu3MsSPA
37+
[gum]: https://github.com/charmbracelet/gum#installation
38+
[cli]: https://clojure.org/guides/install_clojure

build.clj

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
(ns build
2+
(:require [clojure.tools.build.api :as b]))
3+
4+
(def uber-file "todos.jar")
5+
6+
(defn clean
7+
[_]
8+
(b/delete {:path "target"})
9+
(b/delete {:path uber-file}))
10+
11+
(defn uber
12+
[_]
13+
(let [basis (b/create-basis {:project "deps.edn"})
14+
class-dir "target/classes"
15+
src-dirs ["src"]]
16+
(clean nil)
17+
(b/write-pom {:class-dir class-dir
18+
:lib 'workshop/todos
19+
:version "1.0.0"
20+
:basis basis
21+
:src-dirs src-dirs})
22+
(b/copy-dir {:src-dirs src-dirs
23+
:target-dir class-dir})
24+
(b/compile-clj {:basis basis
25+
:src-dirs src-dirs
26+
:class-dir class-dir
27+
:ns-compile '[todos.main]
28+
:compile-opts {:direct-linking true}})
29+
(b/uber {:class-dir class-dir
30+
:uber-file uber-file
31+
:basis basis
32+
:main 'todos.main})))

deps.edn

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{:paths ["src"]
2+
:deps {org.clojars.askonomm/ruuter {:mvn/version "1.3.2"}
3+
com.github.seancorfield/next.jdbc {:mvn/version "1.2.659"}
4+
org.xerial/sqlite-jdbc {:mvn/version "3.40.1.0"}
5+
cheshire/cheshire {:mvn/version "5.11.0"}
6+
http-kit/http-kit {:mvn/version "2.6.0"}}
7+
:aliases {:test {:extra-paths ["test" "fetch_api"]
8+
:extra-deps {io.github.cognitect-labs/test-runner {:git/tag "v0.5.1" :git/sha "dfb30dd"}}
9+
:main-opts ["-m" "cognitect.test-runner"]
10+
:exec-fn cognitect.test-runner.api/test}
11+
:build {:deps {io.github.clojure/tools.build {:git/tag "v0.9.3" :git/sha "e537cd1"}}
12+
:ns-default build}}}

exercises/00-run/README.md

Lines changed: 0 additions & 11 deletions
This file was deleted.

exercises/00-run/script.clj

Lines changed: 0 additions & 2 deletions
This file was deleted.

src/todos/handlers.clj

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
(ns todos.handlers
2+
(:require
3+
[cheshire.core :as json]
4+
[clojure.java.io :as io]
5+
[next.jdbc :as jdbc]
6+
[next.jdbc.result-set :as rs]))
7+
8+
(defn respond
9+
([msg]
10+
(respond msg 200))
11+
([msg status]
12+
{:status status
13+
:headers {"Content-Type" "application/json"}
14+
:body (json/generate-string {:message msg})}))
15+
16+
(defn hello
17+
[_]
18+
(respond "Hello!"))
19+
20+
(defn ls
21+
[{db :db}]
22+
(let [todos (jdbc/execute! db ["SELECT * FROM todos"] {:builder-fn rs/as-unqualified-lower-maps})]
23+
(respond todos)))
24+
25+
(defn todo
26+
[{:keys [db body]}]
27+
(let [{:keys [title due]} (json/parse-stream (io/reader body) true)
28+
id (random-uuid)]
29+
(jdbc/execute-one! db ["INSERT INTO todos (id, title, due) VALUES (?, ?, ?)" id title due])
30+
(respond {:id id})))
31+
32+
(defn done
33+
[{db :db
34+
{:keys [id]} :params}]
35+
(jdbc/execute-one! db ["DELETE FROM todos WHERE id = ?" id])
36+
(respond "Ok"))

0 commit comments

Comments
 (0)