diff --git a/.gitignore b/.gitignore index 63e8fcb..9f13567 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +target/ node_modules .serverless .env @@ -6,5 +7,5 @@ nostr/* dist .clj-kondo/ .lsp/ -config.edn .cpcache +.env diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..19cc3e8 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,43 @@ +# The default deploy instructions (https://biffweb.com/docs/reference/production/) don't +# use Docker, but this file is provided in case you'd like to deploy with containers. +# +# When running the container, make sure you set any environment variables defined in config.env, +# e.g. using whatever tools your deployment platform provides for setting environment variables. +# +# Run these commands to test this file locally: +# +# docker build -t your-app . +# docker run --rm -e BIFF_PROFILE=dev -v $PWD/config.env:/app/config.env your-app + +# This is the base builder image, construct the jar file in this one +# it uses alpine for a small image +FROM clojure:temurin-21-tools-deps-alpine AS jre-build + +# Install the missing packages and applications in a single layer +RUN apk add curl rlwrap + +WORKDIR /app +COPY deps.edn ./ +RUN clj -P +COPY build.clj config.edn ./ +COPY src ./src + +# construct the application jar +RUN clj -T:build uber && cp target/micro-blog-standalone.jar ./app.jar && rm -r target + +# This stage (see multi-stage builds) is a bare Java container +# copy over the uberjar from the builder image and run the application +FROM eclipse-temurin:21-alpine +WORKDIR /app + +# Take the uberjar from the base image and put it in the final image +COPY --from=jre-build /app/app.jar /app/app.jar +COPY --from=jre-build /app/config.edn /app/ + +EXPOSE 8080 + +# By default, run in PROD profile +# ENV BIFF_PROFILE=prod +ENV HOST=0.0.0.0 +ENV PORT=8080 +CMD ["/opt/java/openjdk/bin/java", "-XX:-OmitStackTraceInFastThrow", "-XX:+CrashOnOutOfMemoryError", "-jar", "app.jar"] diff --git a/README.md b/README.md index 8c6b724..977f65e 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,24 @@ instance. ## Deployment -TODO +Repl: + +```shell +$ clj +``` + +Build and run uber jar manually: + +```shell +$ clj -T:build uber +$ java -jar ./target/micro-blog-standalone.jar +``` + +Build new docker image and push it to ECR + +```shell +$ docker buildx build --platform linux/amd64,linux/arm64 -t 853019563312.dkr.ecr.eu-central-1.amazonaws.com/micro-blog-fetchers:2.0.1 --push . +``` ## Project history diff --git a/build.clj b/build.clj index e425d86..bfd21ff 100644 --- a/build.clj +++ b/build.clj @@ -1,7 +1,7 @@ (ns build (:require [clojure.tools.build.api :as b])) -(def lib 'snippets) +(def lib 'micro-blog) (def class-dir "target/classes") (def uber-file (format "target/%s-standalone.jar" (name lib))) @@ -17,9 +17,9 @@ (b/copy-dir {:src-dirs ["src" "resources"] :target-dir class-dir}) (b/compile-clj {:basis @basis - :ns-compile '[snippets.main] + :ns-compile '[micro-blog.main] :class-dir class-dir}) (b/uber {:class-dir class-dir :uber-file uber-file :basis @basis - :main 'snippets.main})) + :main 'micro-blog.main})) diff --git a/config.edn b/config.edn new file mode 100644 index 0000000..4a6ad7f --- /dev/null +++ b/config.edn @@ -0,0 +1,8 @@ +{:mistral-agent-id "ag:e4f9e422:20250716:ismicroblogposttech:99ac9759" + :mistral-host "https://api.mistral.ai" + + :pocket-base-user "micro_blog_fetchers" + :pocket-base-host "http://aemos:5000" + + :blue-sky-host "https://bsky.social/xrpc" + :blue-sky-username "travisshears.bsky.social"} diff --git a/config.sample.edn b/config.sample.edn deleted file mode 100644 index 4aa25de..0000000 --- a/config.sample.edn +++ /dev/null @@ -1,10 +0,0 @@ -{:mistral-api-key "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" - :mistral-agent-id "ag:xxxxxxxx:xxxxxxxx:xxxxxxxxxxxxxxxxxxx:xxxxxxxx" - :mistral-host "https://api.mistral.ai" - - :pocket-base-pw "xxxxxxxx" - :pocket-base-user "xxxxxxxxxxxxxxxxxxx" - :pocket-base-host "xxxxxxxxxxxxx" - - :blue-sky-api-key "xxxxxxxxxxxxxxxxxxx" - :blue-sky-username "coolguy.bsky.social"} diff --git a/deps.edn b/deps.edn index 54acfac..3a11f98 100644 --- a/deps.edn +++ b/deps.edn @@ -2,10 +2,11 @@ :deps {clj-http/clj-http {:mvn/version "3.13.1"} ;; logging com.taoensso/telemere {:mvn/version "1.0.0"} - ;; environment variables - environ/environ {:mvn/version "1.2.0"} ;; schema validation metosin/malli {:mvn/version "0.18.0"} + ;; scheduling + jarohen/chime {:mvn/version "0.3.3"} + org.clojure/core.async {:mvn/version "1.8.741"} ;; json decoding cheshire/cheshire {:mvn/version "6.0.0"} ;; metosin/muuntaja {:mvn/version "0.6.11"} diff --git a/src/micro_blog/blue_sky.clj b/src/micro_blog/blue_sky.clj index b8e2bef..bb1c5ce 100644 --- a/src/micro_blog/blue_sky.clj +++ b/src/micro_blog/blue_sky.clj @@ -3,8 +3,7 @@ [clj-http.client :as http-client] [micro-blog.pocket-base :as pb] [micro-blog.utils :as utils] - [malli.core :as m] - [clojure.pprint :refer [pprint]] + [taoensso.telemere :as tel] [micro-blog.config :refer [config]])) (defn create-session [] @@ -97,6 +96,7 @@ (map #(vector (:fullsize %) (:alt %)) images))) (defn run [] + (tel/log! :info "Running blue sky fetcher") (let [session (create-session) last-saved-id (pb/get-latest-post-remote-id-by-source :blue_sky) new-posts (reverse (get-posts-until-id session last-saved-id))] diff --git a/src/micro_blog/config.clj b/src/micro_blog/config.clj index a30bf2c..d51c50b 100644 --- a/src/micro_blog/config.clj +++ b/src/micro_blog/config.clj @@ -1,14 +1,18 @@ (ns micro-blog.config - (:require [environ.core :refer [env]])) + (:require + [taoensso.telemere :as tel])) (def ^:private env-overrides - {:minstral-api-key "MISTRAL_API_KEY"}) + {:pocket-base-pw "POCKET_BASE_PW" + :pocket-base-host "POCKET_BASE_HOST" + :blue-sky-api-key "BLUE_SKY_API_KEY" + :minstral-api-key "MISTRAL_API_KEY"}) (defn- load-config [] - (println "config is being loaded") + (tel/log! :info "Config is being loaded") (merge (read-string (slurp "config.edn")) (into {} (for [[k env-var] env-overrides - :let [env-val (env env-var)] + :let [env-val (System/getenv env-var)] :when env-val] [k env-val])))) diff --git a/src/micro_blog/main.clj b/src/micro_blog/main.clj new file mode 100644 index 0000000..6041778 --- /dev/null +++ b/src/micro_blog/main.clj @@ -0,0 +1,14 @@ +(ns micro-blog.main + (:gen-class) + (:import [java.time Instant Duration]) + (:require [chime.core :as chime] + [taoensso.telemere :as tel] + [micro-blog.blue-sky :as blue-sky])) + +(defn -main [] + (tel/log! :info "Setting up crons") + (chime/chime-at (chime/periodic-seq (Instant/now) (Duration/ofMinutes 30)) + (fn [_time] + (blue-sky/run))) + + @(promise))