Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove legacy api #129

Draft
wants to merge 10 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,5 @@ webapp/.lsp/.cache
webapp/out
webapp/.shadow-cljs
webapp/.lein-repl-history
.clj-kondo
.lsp
85 changes: 61 additions & 24 deletions webapp/src/clj/lipas/backend/handler.clj
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
(ns lipas.backend.handler
(:require
[clojure.java.io :as io]
[clojure.spec.alpha :as s]
[lipas.backend.core :as core]
[lipas.backend.jwt :as jwt]
[lipas.backend.middleware :as mw]
[lipas.roles :as roles]
[lipas.schema.core]
[lipas.utils :as utils]
[muuntaja.core :as m]
[reitit.coercion.spec]
[reitit.ring :as ring]
[reitit.ring.coercion :as coercion]
[reitit.ring.middleware.exception :as exception]
[reitit.ring.middleware.multipart :as multipart]
[reitit.ring.middleware.muuntaja :as muuntaja]
[reitit.swagger :as swagger]
[reitit.swagger-ui :as swagger-ui]
[ring.middleware.params :as params]
[ring.util.io :as ring-io]
[taoensso.timbre :as log]))
(:require [clojure.java.io :as io]
[clojure.spec.alpha :as s]
[lipas.backend.core :as core]
[lipas.backend.jwt :as jwt]
[lipas.backend.legacy.api :as legacy.api]
[lipas.backend.middleware :as mw]
[lipas.roles :as roles]
[lipas.schema.core]
[lipas.utils :as utils]
[muuntaja.core :as m]
[reitit.coercion.spec]
[reitit.ring :as ring]
[reitit.ring.coercion :as coercion]
[reitit.ring.middleware.exception :as exception]
[reitit.ring.middleware.multipart :as multipart]
[reitit.ring.middleware.muuntaja :as muuntaja]
[reitit.swagger :as swagger]
[reitit.swagger-ui :as swagger-ui]
[ring.middleware.params :as params]
[ring.util.io :as ring-io]
[taoensso.timbre :as log]))

(defn exception-handler
([status type]
Expand All @@ -40,7 +40,6 @@
:user-not-found (exception-handler 404 :user-not-found)
:email-not-found (exception-handler 404 :email-not-found)
:reminder-not-found (exception-handler 404 :reminder-not-found)

:qbits.spandex/response-exception (exception-handler 500 :internal-server-error :print-stack)

;; Return 500 and print stack trace for exceptions that are not
Expand All @@ -57,7 +56,6 @@
[{:keys [db emailer search mailchimp aws]}]
(ring/ring-handler
(ring/router

[["/favicon.ico"
{:get
{:no-doc true
Expand Down Expand Up @@ -774,7 +772,45 @@
:handler
(fn [{:keys [body-params identity]}]
{:status 200
:body (core/save-ptv-integration-definitions db search identity body-params)})}}]]]
:body (core/save-ptv-integration-definitions db search identity body-params)})}}]]

;; legacy routes
["/v1/api"
Copy link
Contributor

@vharmain vharmain Nov 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there's currently a top-level endpoint /rest that nginx captures and proxies to the legacy-api. I think we could just swap in this thing with the path /rest/api once it's ready and remove the nginx rule. https://github.com/lipas-liikuntapaikat/lipas/blob/master/nginx/proxy.conf#L141

["/swagger.json"
{:get
{:no-doc true
:swagger {:id ::legacy
:info {:title "Lipas-API (legacy) v1"}}
:handler (swagger/create-swagger-handler)}}]
["/sports-place-types"
{:swagger {:id ::legacy}
:parameters {:query (s/keys :opt-un [:lipas.api/lang])}
:get
{:handler
(fn [req]
(let [locale (or (-> req :parameters :query :lang keyword) :en)]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Legacy-api defaults to :fi, let's not change that. :)

http://lipas.cc.jyu.fi/api/categories

{:status 200
:body (legacy.api/sports-place-types locale)}))}}]
["/sports-place-types/:type-code"
{:swagger {:id ::legacy}
:parameters {:query (s/keys :opt-un [:lipas.api/lang])
:path {:type-code int?}}
:get
{:handler
(fn [req]
(let [locale (or (-> req :parameters :query :lang keyword) :en)
type-code (-> req :parameters :path :type-code)]
{:status 200
:body (legacy.api/sports-place-by-type-code locale type-code)}))}}]
["/categories"
{:swagger {:id ::legacy}
:parameters {:query (s/keys :opt-un [:lipas.api/lang])}
:get
{:handler
(fn [req]
(let [locale (or (-> req :parameters :query :lang keyword) :en)]
{:status 200
:body (legacy.api/categories locale)}))}}]]]

{:data
{:coercion reitit.coercion.spec/coercion
Expand All @@ -800,4 +836,5 @@
mw/privilege-middleware]}})
(ring/routes
(swagger-ui/create-swagger-ui-handler {:path "/api/swagger-ui" :url "/api/swagger.json"})
(swagger-ui/create-swagger-ui-handler {:path "/v1/api/swagger-ui" :url "/v1/api/swagger.json"})
(ring/create-default-handler))))
68 changes: 68 additions & 0 deletions webapp/src/clj/lipas/backend/legacy/api.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
(ns lipas.backend.legacy.api
(:require
[lipas.data.prop-types :as prop-types]
[lipas.data.types :as types]
[lipas.utils :as utils]))

(def keys-vec [:type-code :name :description :geometry-type :sub-category])

(defn fill-properties [m]
(reduce (fn [acc k] (assoc-in acc [:props k] (prop-types/all k)))
m
(-> m :props keys)))
Comment on lines +11 to +14
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you could just (assoc m :props (select-keys prop-types/all (keys (:props m))))

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.. or (update m :props #(select-keys prop-types/all (keys %)))



(defn localize-properties [m locale]
(reduce (fn [acc k]
(-> acc
(update-in [:props k :description] locale)
(update-in [:props k :name] locale))) m (-> m :props keys)))

(defn- localize [m lang]
(-> m
(update :name lang)
(update :description lang)))


(defn- ->legacy-api-with-properties [m lang]
(-> m
(fill-properties)
(select-keys (conj keys-vec :props))
(update :props #(-> % (dissoc :schoolUse? :freeUse?)))
(localize-properties lang)
(localize lang)
utils/->camel-case-keywords))

(defn- ->legacy-api [m lang]
(-> m
(select-keys keys-vec)
(localize lang)
utils/->camel-case-keywords))
Copy link
Contributor

@vharmain vharmain Nov 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice if it was this simple. ;) However, the differences between lipas and legacy-api are not 1:1 kebab-case -> camelCase... there are a few edge-cases and exceptions that are covered in this mapping: https://github.com/lipas-liikuntapaikat/lipas/blob/master/webapp/src/clj/lipas/integration/old_lipas/sports_site.clj#L13

...and it's inverted version https://github.com/lipas-liikuntapaikat/lipas/blob/master/webapp/src/clj/lipas/integration/old_lipas/sports_site.clj#L203

You can use rename-keys to change "new keys" to "old keys" with the hardcoded map.


(defn sports-place-types [locale]
(->> (vals types/all)
(map #(->legacy-api % locale))))

(defn sports-place-by-type-code [locale type-code]
(-> (types/all type-code)
(->legacy-api-with-properties locale)))

(defn collect-sport-place-types [sub-category-type-code]
(->> (vals types/all)
(filter #(= (% :sub-category) sub-category-type-code))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor thing, but you can change filter to just a map lookup. We already have types indexed by sub-category (and also by main-category) here https://github.com/lipas-liikuntapaikat/lipas/blob/remove-legacy-api-v2/webapp/src/cljc/lipas/data/types.cljc#L23-L24

(map :type-code)))

(defn- collect-subcategories [type-code locale]
(->>
(vals types/sub-categories)
(filter #(= (% :main-category) (str type-code)))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above :)

(map (fn [x]
{:typeCode (x :type-code)
:name (-> x :name locale)
:sportsPlaceTypes (collect-sport-place-types (x :type-code))}))))

(defn categories [locale]
(mapv (fn [cat] {:name (-> cat :name locale)
:typeCode (cat :type-code)
:subCategories (collect-subcategories (cat :type-code) locale)})
(vals types/main-categories)))
16 changes: 16 additions & 0 deletions webapp/src/cljc/lipas/schema/core_legacy.cljc
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
(ns lipas.schema.core-legacy
(:require [clojure.spec.alpha :as s]
[lipas.schema.core]))

(s/def :lipas-legacy.api.parameter/lang :lipas.api/lang)

(s/def :lipas-legacy.api.response/sports-place-types
(s/keys :req-un [:lipas-legacy.sports-site/typeCode
:lipas.sports-site/name
:lipas.sports-site/description
:lipas-legacy.sports-site/geometryType
:lipas-legacy.sports-site/subCategory]))

(s/def :lipas-legacy.sports-site/geometryType #{"Point" "LineString"})
(s/def :lipas-legacy.sports-site/typeCode (s/int-in 1 5000))
(s/def :lipas-legacy.sports-site/subCategory (s/int-in 1 5000))