Merge pull request #249 from asciinema/clj-a2png

Start Clojure app and route to it
This commit is contained in:
Marcin Kulik 2017-04-19 22:20:49 +02:00 committed by GitHub
commit 858210391c
11 changed files with 72 additions and 22 deletions

View File

@ -12,7 +12,7 @@ BASE_URL=http://localhost:3000
SECRET_KEY_BASE= SECRET_KEY_BASE=
## PostgreSQL connection URL. ## PostgreSQL connection URL.
## Default: postgresql://postgres@postgres/postgres ## Default: postgresql://postgres/postgres?user=postgres
# DATABASE_URL= # DATABASE_URL=
## Redis connection URL. ## Redis connection URL.

View File

@ -97,11 +97,20 @@ RUN cd a2png && npm install
COPY a2png /app/a2png COPY a2png /app/a2png
RUN cd a2png && lein cljsbuild once main && lein cljsbuild once page RUN cd a2png && lein cljsbuild once main && lein cljsbuild once page
# build uberjar
COPY project.clj /app/
RUN lein deps
COPY src /app/src
COPY resources /app/resources
RUN lein uberjar
# copy the rest of the source code # copy the rest of the source code
COPY . /app COPY . /app
ENV DATABASE_URL "postgresql://postgres@postgres/postgres" ENV DATABASE_URL "postgresql://postgres/postgres?user=postgres"
ENV REDIS_URL "redis://redis:6379" ENV REDIS_URL "redis://redis:6379"
# compile terminal.c # compile terminal.c
@ -125,6 +134,11 @@ COPY docker/nginx/asciinema.conf /etc/nginx/sites-available/default
RUN mkdir -p /var/log/supervisor RUN mkdir -p /var/log/supervisor
COPY docker/supervisor/asciinema.conf /etc/supervisor/conf.d/asciinema.conf COPY docker/supervisor/asciinema.conf /etc/supervisor/conf.d/asciinema.conf
# add start script for Clojure app
COPY docker/start.sh /app/start.sh
RUN chmod a+x /app/start.sh
VOLUME ["/app/log", "/app/uploads"] VOLUME ["/app/log", "/app/uploads"]
CMD ["/usr/bin/supervisord"] CMD ["/usr/bin/supervisord"]

View File

@ -1,12 +1,9 @@
{:components {:components
{:file-store #var asciinema.component.local-file-store/local-file-store {:exp-set #var asciinema.component.mem-expiring-set/mem-expiring-set}
:exp-set #var asciinema.component.mem-expiring-set/mem-expiring-set}
:config :config
{:http {:http
{:port 4000} {:port 4000}
:db :db
{:uri "jdbc:postgresql://localhost:15432/asciinema_development?user=vagrant"} {:uri "jdbc:postgresql://localhost:15432/asciinema_development?user=vagrant"}
:file-store
{:path "uploads/"}
:png-gen :png-gen
{:bin-path "a2png/a2png.sh"}}} {:bin-path "a2png/a2png.sh"}}}

View File

@ -2,6 +2,10 @@ upstream rails-server {
server 127.0.0.1:3000 fail_timeout=0; server 127.0.0.1:3000 fail_timeout=0;
} }
upstream clj-server {
server 127.0.0.1:4000 fail_timeout=0;
}
server { server {
listen 80 default_server; listen 80 default_server;
listen [::]:80 default_server; listen [::]:80 default_server;
@ -12,6 +16,10 @@ server {
client_max_body_size 16m; client_max_body_size 16m;
location ~ ^/a/[^.]+\.(json|png)$ {
try_files $uri $uri/index.html $uri.html @clj;
}
location / { location / {
try_files $uri $uri/index.html $uri.html @rails; try_files $uri $uri/index.html $uri.html @rails;
} }
@ -33,4 +41,13 @@ server {
proxy_redirect off; proxy_redirect off;
error_page 500 502 504 /500.html; # Rails error pages error_page 500 502 504 /500.html; # Rails error pages
} }
location @clj {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $http_host;
proxy_pass http://clj-server;
proxy_redirect off;
}
} }

8
docker/start.sh Normal file
View File

@ -0,0 +1,8 @@
#!/usr/bin/env bash
export DATABASE_URL=jdbc:${DATABASE_URL}
export S3_ACCESS_KEY=${AWS_ACCESS_KEY_ID}
export S3_SECRET_KEY=${AWS_SECRET_ACCESS_KEY}
export A2PNG_BIN_PATH="/app/a2png/a2png.sh"
exec java -server -Xmx3g -jar /app/target/uberjar/asciinema-0.1.0-SNAPSHOT-standalone.jar

View File

@ -15,3 +15,11 @@ stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0 stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0 stderr_logfile_maxbytes=0
[program:clj]
command=/app/start.sh
directory=/app
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0

View File

@ -2,7 +2,7 @@
{:http #var asciinema.component.yada-listener/yada-listener {:http #var asciinema.component.yada-listener/yada-listener
:db #var asciinema.component.db/hikaricp :db #var asciinema.component.db/hikaricp
:ragtime #var duct.component.ragtime/ragtime :ragtime #var duct.component.ragtime/ragtime
:file-store #var asciinema.component.s3-file-store/s3-file-store :file-store #var asciinema.component.auto-file-store/auto-file-store
:exp-set #var asciinema.component.redis-client/redis-client :exp-set #var asciinema.component.redis-client/redis-client
:png-gen #var asciinema.component.a2png/a2png :png-gen #var asciinema.component.a2png/a2png
:executor #var asciinema.component.fixed-thread-executor/fixed-thread-executor} :executor #var asciinema.component.fixed-thread-executor/fixed-thread-executor}
@ -20,13 +20,12 @@
:ragtime :ragtime
{:resource-path "asciinema/migrations"} {:resource-path "asciinema/migrations"}
:file-store :file-store
{:cred {:access-key s3-access-key {:path "uploads/"
:secret-key s3-secret-key} :s3-bucket s3-bucket
:bucket s3-bucket :s3-cred {:access-key s3-access-key
:path-prefix "uploads/"} :secret-key s3-secret-key}}
:exp-set :exp-set
{:host redis-host {:uri redis-url}
:port redis-port}
:png-gen :png-gen
{:bin-path a2png-bin-path} {:bin-path a2png-bin-path}
:executor :executor

View File

@ -0,0 +1,8 @@
(ns asciinema.component.auto-file-store
(:require [asciinema.component.local-file-store :refer [local-file-store]]
[asciinema.component.s3-file-store :refer [s3-file-store]]))
(defn auto-file-store [config]
(if (:s3-bucket config)
(s3-file-store config)
(local-file-store config)))

View File

@ -5,12 +5,12 @@
[com.stuartsierra.component :as component] [com.stuartsierra.component :as component]
[taoensso.carmine :as car])) [taoensso.carmine :as car]))
(defrecord RedisClient [host port] (defrecord RedisClient [uri]
component/Lifecycle component/Lifecycle
(start [component] (start [component]
(if (:listener component) (if (:listener component)
component component
(let [conn {:pool {} :spec {:host host :port port}}] (let [conn {:pool {} :spec {:uri uri}}]
(assoc component :conn conn)))) (assoc component :conn conn))))
(stop [component] (stop [component]
(if (:conn component) (if (:conn component)
@ -24,5 +24,5 @@
(contains? [this value] (contains? [this value]
(car/as-bool (car/wcar (:conn this) (car/exists value))))) (car/as-bool (car/wcar (:conn this) (car/exists value)))))
(defn redis-client [{:keys [host port]}] (defn redis-client [{:keys [uri]}]
(->RedisClient host port)) (->RedisClient uri))

View File

@ -60,5 +60,5 @@
(assoc :status 302) (assoc :status 302)
(update :headers assoc "location" url))))) (update :headers assoc "location" url)))))
(defn s3-file-store [{:keys [cred bucket path-prefix]}] (defn s3-file-store [{:keys [s3-cred s3-bucket path]}]
(->S3FileStore cred bucket path-prefix)) (->S3FileStore s3-cred s3-bucket path))

View File

@ -27,13 +27,12 @@
(defn -main [& args] (defn -main [& args]
(binding [y/*exception-notifier* (create-exception-notifier)] (binding [y/*exception-notifier* (create-exception-notifier)]
(let [bindings {'http-port (Integer/parseInt (:port env "3000")) (let [bindings {'http-port (Integer/parseInt (:port env "4000"))
'db-uri (:database-url env) 'db-uri (:database-url env)
's3-bucket (:s3-bucket env) 's3-bucket (:s3-bucket env)
's3-access-key (:s3-access-key env) 's3-access-key (:s3-access-key env)
's3-secret-key (:s3-secret-key env) 's3-secret-key (:s3-secret-key env)
'redis-host (:redis-host env "localhost") 'redis-url (:redis-url env "redis://localhost")
'redis-port (Integer/parseInt (:redis-port env "6379"))
'a2png-bin-path (:a2png-bin-path env "a2png/a2png.sh")} 'a2png-bin-path (:a2png-bin-path env "a2png/a2png.sh")}
system (->> (load-system [(io/resource "asciinema/system.edn")] bindings) system (->> (load-system [(io/resource "asciinema/system.edn")] bindings)
(component/start))] (component/start))]