Skip to content

Commit

Permalink
Merge branch 'master' into 223-post-order-form-field
Browse files Browse the repository at this point in the history
  • Loading branch information
skydread1 committed Jul 28, 2023
2 parents f52d125 + 1ce664d commit afdedb6
Show file tree
Hide file tree
Showing 18 changed files with 120 additions and 100 deletions.
73 changes: 2 additions & 71 deletions server/src/flybot/server/core.clj
Original file line number Diff line number Diff line change
@@ -1,76 +1,7 @@
(ns flybot.server.core
(:require [flybot.server.core.handler :as handler]
[flybot.server.core.handler.operation.db :as db]
[flybot.server.core.init-data :as sample]
[aleph.http :as http]
[clojure.edn :as edn]
[datalevin.core :as d]
[ring.middleware.session.memory :refer [memory-store]]
[robertluo.fun-map :refer [fnk life-cycle-map closeable touch]])
(:require [flybot.server.systems :refer [prod-system]]
[robertluo.fun-map :refer [touch]])
(:gen-class))

(def oauth2-default-config
{:google
{:project-id "flybot-website"
:scopes ["https://www.googleapis.com/auth/userinfo.email" "https://www.googleapis.com/auth/userinfo.profile"],
:redirect-uri "https://flybot.sg/oauth/google/callback",
:access-token-uri "https://oauth2.googleapis.com/token",
:authorize-uri "https://accounts.google.com/o/oauth2/auth",
:launch-uri "/oauth/google/login"
:landing-uri "/oauth/google/success"
:client-root-path "/"}})

(defn load-initial-data
"Loads the initial posts and the owner-user to the db
If the db already has some content, do nothing."
[conn init-data]
(when-not (seq (db/get-all-posts (d/db conn)))
@(d/transact conn init-data)))

;;---------- System ----------

(defn system
[{:keys [http-port db-uri google-creds oauth2-callback client-root-path]}]
(life-cycle-map
{:db-uri db-uri
:db-conn (fnk [db-uri]
(let [conn (d/get-conn db-uri db/initial-datalevin-schema)]
(load-initial-data conn sample/init-data)
(closeable
{:conn conn}
#(d/close conn))))
:oauth2-config (let [{:keys [client-id client-secret]} google-creds]
(-> oauth2-default-config
(assoc-in [:google :client-id] client-id)
(assoc-in [:google :client-secret] client-secret)
(assoc-in [:google :redirect-uri] oauth2-callback)
(assoc-in [:google :client-root-path] client-root-path)))
:session-store (memory-store)
:injectors (fnk [db-conn]
[(fn [] {:db (d/db (:conn db-conn))})])
:executors (fnk [db-conn]
[(handler/mk-executors (:conn db-conn))])
:saturn-handler handler/saturn-handler
:ring-handler (fnk [injectors saturn-handler executors]
(handler/mk-ring-handler injectors saturn-handler executors))
:reitit-router (fnk [ring-handler oauth2-config session-store]
(handler/app-routes ring-handler oauth2-config session-store))
:http-port http-port
:http-server (fnk [http-port reitit-router]
(let [svr (http/start-server
reitit-router
{:port http-port})]
(closeable
svr
#(.close svr))))}))

(def oauth2-config
(edn/read-string (or (System/getenv "OAUTH2")
(slurp "config/oauth2.edn"))))

(def prod-system
(let [prod-cfg (edn/read-string (System/getenv "SYSTEM"))]
(system (merge prod-cfg oauth2-config))))

(defn -main [& _]
(touch prod-system))
103 changes: 84 additions & 19 deletions server/src/flybot/server/systems.clj
Original file line number Diff line number Diff line change
@@ -1,16 +1,59 @@
(ns flybot.server.systems
"Systems for backend dev and frontend dev/test with figwheel"
(:require [flybot.server.core :as core]
"Systems for the different environments:
- figwheel-system: automatically touched when you launch the clj/cljs repl
- dev-system: can be use anytime to start a system with aleph on port 8123
- prod-system: similar to dev-system but without loading/deleting db data."
(:require [flybot.server.core.handler :as handler]
[flybot.server.core.handler.operation.db :as db]
[flybot.server.core.init-data :as ds]
[clojure.edn :as edn]
[flybot.server.systems.init-data :as id]
[flybot.server.systems.config :as config]
[aleph.http :as http]
[datalevin.core :as d]
[robertluo.fun-map :refer [fnk closeable touch halt!]]))
[ring.middleware.session.memory :refer [memory-store]]
[robertluo.fun-map :refer [fnk life-cycle-map closeable touch halt!]]))

(defn system-config
[env]
(let [env-cfg (-> (slurp "config/system.edn") edn/read-string env)]
(merge env-cfg core/oauth2-config)))
(defn load-initial-data
"Loads the initial posts and the owner-user to the db
If the db already has some content, does nothing."
[conn init-data]
(when-not (seq (db/get-all-posts (d/db conn)))
@(d/transact conn init-data)))

(defn system
[{:keys [http-port db-uri google-creds oauth2-callback client-root-path]
:or {client-root-path "/"}}]
(life-cycle-map
{:db-uri db-uri
:db-conn (fnk [db-uri]
(let [conn (d/get-conn db-uri db/initial-datalevin-schema)]
(load-initial-data conn id/init-data)
(closeable
{:conn conn}
#(d/close conn))))
:oauth2-config (let [{:keys [client-id client-secret]} google-creds]
(-> config/oauth2-default-config
(assoc-in [:google :client-id] client-id)
(assoc-in [:google :client-secret] client-secret)
(assoc-in [:google :redirect-uri] oauth2-callback)
(assoc-in [:google :client-root-path] client-root-path)))
:session-store (memory-store)
:injectors (fnk [db-conn]
[(fn [] {:db (d/db (:conn db-conn))})])
:executors (fnk [db-conn]
[(handler/mk-executors (:conn db-conn))])
:saturn-handler handler/saturn-handler
:ring-handler (fnk [injectors saturn-handler executors]
(handler/mk-ring-handler injectors saturn-handler executors))
:reitit-router (fnk [ring-handler oauth2-config session-store]
(handler/app-routes ring-handler oauth2-config session-store))
:http-port http-port
:http-server (fnk [http-port reitit-router]
(let [svr (http/start-server
reitit-router
{:port http-port})]
(closeable
svr
#(.close svr))))}))

(defn db-conn-system
"On touch: empty the db and get conn.
Expand All @@ -20,23 +63,27 @@
(let [conn (d/get-conn db-uri)
_ (d/clear conn)
conn (d/get-conn db-uri db/initial-datalevin-schema)]
(core/load-initial-data conn init-data)
(load-initial-data conn init-data)
(closeable
{:conn conn}
#(d/clear conn)))))

;;---------- System for front-end dev ----------
;; Figwheel automatically start the system for us via the figwheel-main.edn on port 9500
;; If some changes are made in one of the component (such as handler for instance),
;; just reload this namespace and refresh your browser.
;; Figwheel automatically touches the system on repl launch via the figwheel-main.edn on port 9500

(def figwheel-system
(-> (system-config :figwheel)
core/system
(assoc :db-conn (db-conn-system ds/init-data))
"Figwheel automatically touches the system via the figwheel-main.edn on port 9500.
Figwheel just needs a handler and starts its own server hence we dissoc the http-server.
If some changes are made in one of the backend component (such as handler for instance),
you can halt!, reload ns and touch again the system."
(-> (config/system-config :figwheel)
system
(assoc :db-conn (db-conn-system id/init-data))
(dissoc :http-port :http-server)))

(def figwheel-handler
"Provided to figwheel-main.edn.
Figwheel uses this handler to starts a server on port 9500."
(-> figwheel-system
touch
:reitit-router))
Expand All @@ -47,13 +94,31 @@
)

;;---------- System for backend dev ----------
;; be sure to have a main.js in resources/public to have the UI on port 8123
;; Be sure to have a main.js in resources/public to have the UI on port 8123

(def dev-system
(-> (core/system (system-config :dev))
(assoc :db-conn (db-conn-system ds/init-data))))
"The dev system starts a server on port 8123.
It loads some real data sample. The data is deleted when the system halt!.
It is convenient if you want to see your backend changes in action in the UI."
(-> (system (config/system-config :dev))
(assoc :db-conn (db-conn-system id/init-data))))

(comment
(touch dev-system)
(halt! dev-system) ;; reload ns after halt! before touch again.
)

;;---------- System for backend prod ----------
;; Be sure to have a main.js in resources/public to have the UI on port 8123

(def prod-system
"The prod system starts a server on port 8123.
It does not load any init-data on touch and it does not delete any data on halt!.
You can use it in your local environment as well."
(let [prod-cfg (config/system-config :prod)]
(system prod-cfg)))

(comment
(touch prod-system)
(halt! prod-system) ;; reload ns after halt! before touch again.
)
24 changes: 24 additions & 0 deletions server/src/flybot/server/systems/config.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
(ns flybot.server.systems.config
(:require [clojure.edn :as edn]))

(def oauth2-default-config
{:google
{:project-id "flybot-website"
:scopes ["https://www.googleapis.com/auth/userinfo.email" "https://www.googleapis.com/auth/userinfo.profile"],
:redirect-uri "https://flybot.sg/oauth/google/callback",
:access-token-uri "https://oauth2.googleapis.com/token",
:authorize-uri "https://accounts.google.com/o/oauth2/auth",
:launch-uri "/oauth/google/login"
:landing-uri "/oauth/google/success"
:client-root-path "/"}})

(def oauth2-config
(edn/read-string (or (System/getenv "OAUTH2")
(slurp "config/oauth2.edn"))))

(defn system-config
[env]
(let [env-cfg (or (-> (when-let [cfg (System/getenv "SYSTEM")]
(edn/read-string cfg)))
(-> (slurp "config/system.edn") edn/read-string env))]
(merge env-cfg oauth2-config)))
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(ns flybot.server.core.init-data
(ns flybot.server.systems.init-data
"Realistic sample data that can be used for api or figwheel developement."
(:require [flybot.common.utils :as u]
[clojure.edn :as edn]
Expand Down Expand Up @@ -41,7 +41,7 @@
(defn slurp-md
"Slurp the sample files with the markdown."
[page-name file-name]
(-> (str "flybot/server/core/init_data/md_content/" page-name "/" file-name)
(-> (str "flybot/server/systems/init_data/md_content/" page-name "/" file-name)
io/resource
slurp))

Expand Down
8 changes: 4 additions & 4 deletions server/test/flybot/server/core/handler/operation_test.clj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(ns flybot.server.core.handler.operation-test
(:require [flybot.server.core :as core]
[flybot.server.core.handler.operation :as sut]
(:require [flybot.server.core.handler.operation :as sut]
[flybot.server.systems :as sys]
[flybot.server.systems.config :as config]
[flybot.common.test-sample-data :as s]
[flybot.common.utils :as utils]
[clojure.test :refer [deftest is testing use-fixtures]]
Expand All @@ -11,8 +11,8 @@
(def test-data [s/post-1 s/post-2
s/bob-user s/alice-user])
(def test-system
(-> (sys/system-config :test)
core/system
(-> (config/system-config :test)
sys/system
(dissoc :oauth2-config)
(assoc :db-conn (sys/db-conn-system test-data))))

Expand Down
8 changes: 4 additions & 4 deletions server/test/flybot/server/core/handler_test.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(ns flybot.server.core.handler-test
(:require [flybot.server.core :as core]
[flybot.server.systems :as sys]
(:require [flybot.server.systems :as sys]
[flybot.server.systems.config :as config]
[flybot.server.core.handler :as sut]
[flybot.server.core.handler.auth :as auth]
[flybot.common.test-sample-data :as s]
Expand All @@ -15,8 +15,8 @@
s/bob-user s/alice-user])
(defn test-system
[]
(-> (sys/system-config :test)
core/system
(-> (config/system-config :test)
sys/system
(dissoc :oauth2-config)
(assoc :db-conn (sys/db-conn-system test-data))))

Expand Down

0 comments on commit afdedb6

Please sign in to comment.