clojutre2015: kekkonen - making your clojure web apis more awesome

29
Kekkonen making your Clojure web APIs more awesome ClojuTRE 2015 Tommi Reiman @ikitommi

Upload: metosin-oy

Post on 12-Feb-2017

1.011 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: ClojuTRE2015: Kekkonen - making your Clojure web APIs more awesome

Kekkonen ���making your Clojure web APIs more awesome

ClojuTRE 2015Tommi Reiman

@ikitommi

Page 2: ClojuTRE2015: Kekkonen - making your Clojure web APIs more awesome

Topics•  API all the things!•  Challenges•  Kekkonen•  Done

Page 3: ClojuTRE2015: Kekkonen - making your Clojure web APIs more awesome

API all the things!•  Clojure(Script) APIs•  RPC•  Web APIs•  REST APIs•  Future?

Page 4: ClojuTRE2015: Kekkonen - making your Clojure web APIs more awesome

Clojure(Script) APIs•  Namespaces and functions (Vars)•  Extension via multimethods & protocols•  Simple, beautiful, no remoting

Page 5: ClojuTRE2015: Kekkonen - making your Clojure web APIs more awesome

RPC•  Expose the tagged Clojure functions outwards•  Easy remoting•  refactoring? external clients?

Page 6: ClojuTRE2015: Kekkonen - making your Clojure web APIs more awesome

Web APIs•  Map Clojure functions into http endpoints•  Thinking in HTTP terms•  Swagger docs

Page 7: ClojuTRE2015: Kekkonen - making your Clojure web APIs more awesome

REST Apis•  Map Clojure functions into resources•  Resource containers•  Swagger docs (yada!)

Page 8: ClojuTRE2015: Kekkonen - making your Clojure web APIs more awesome

Future?•  Datomic & DataScript•  Falcor/Netflix

–  A JavaScript library for efficient data fetching

•  Relay/Facebook–  A JAVASCRIPT FRAMEWORK FOR BUILDING

DATA-DRIVEN REACT APPLICATIONS

•  The Web after Tomorrow–  http://tonsky.me/blog/the-web-after-tomorrow/

©  Nikita  Prokopov  

Page 9: ClojuTRE2015: Kekkonen - making your Clojure web APIs more awesome

Hmph.•  We build complex UIs•  Reagent is cool•  Need to do remoting, public api-docs too•  Business rules need to be enforced both on

server (all) & frontend (part)–  Shared code is cool, but number of combinations…

•  REST doesn’t even try to solve these issues

Page 10: ClojuTRE2015: Kekkonen - making your Clojure web APIs more awesome

CQRS? ���making your Clojure web APIs more awesome

Page 11: ClojuTRE2015: Kekkonen - making your Clojure web APIs more awesome

Commands & Queries•  http://martinfowler.com/bliki/CQRS.html •  Verbs (actions) instead of Nouns (resources)•  Command log (auditing) as a bonus•  != Event Sourcing•  Great for user interactions

–  Rules usually per interaction, not resource–  Fine-grained à lot’s of actions

©  Mar.n  Fowler  

Page 12: ClojuTRE2015: Kekkonen - making your Clojure web APIs more awesome

Commands & Queries

Watch  

GiveStar  GetRepository  

Fork  

Unwatch   RemoveStar  ListRespotories  

Page 13: ClojuTRE2015: Kekkonen - making your Clojure web APIs more awesome

Commands & Queries

Watch  

GiveStar  GetRepository  

Fork  

Unwatch   RemoveStar  

[repo-­‐id  :-­‐  s/Str]  check-­‐repo-­‐auth  (watched?  false)    

[repo-­‐id  :-­‐  s/Str]  check-­‐repo-­‐auth  

[repo-­‐id  :-­‐  s/Str]  check-­‐repo-­‐auth  (watched?  true)  

[repo-­‐id  :-­‐  s/Str]  check-­‐repo-­‐auth  (starred?  false)  

[repo-­‐id  :-­‐  s/Str]  check-­‐repo-­‐auth  

[repo-­‐id  :-­‐  s/Str]  check-­‐repo-­‐auth  (starred?  true)  

ListRespotories  

Page 14: ClojuTRE2015: Kekkonen - making your Clojure web APIs more awesome

DIY CQRS lib

Page 15: ClojuTRE2015: Kekkonen - making your Clojure web APIs more awesome
Page 16: ClojuTRE2015: Kekkonen - making your Clojure web APIs more awesome

Rethinking the APIs•  Idea–  Expose simple Clojure functions as message handlers–  Manage handlers in virtual namespaces–  Data-driven, no macros, no magic–  Explicit extensions via protocols, options and meta-data–  Transports abstracted away, http via ring

•  Lessons learned from ring-swagger & compojure-api–  Clients as first-class citizens

•  Remote api documentation as data: rules as data•  Public http api documentation via Swagger

Page 17: ClojuTRE2015: Kekkonen - making your Clojure web APIs more awesome

Thanks to•  Prismatic Schema, (Plumbing)•  Elegance of fnhouse•  Ring-swagger•  Best parts of compojure-api•  Schema-tools

Page 18: ClojuTRE2015: Kekkonen - making your Clojure web APIs more awesome

Concepts•  Handlers & namespaces

–  Functions with meta-data, contextàresponse•  Context

–  A message context consumed by the handler–  Opinion: should contain :data –key with the actual payload

•  Registry–  Collects and enriches handlers into namespaces–  Handler invocation & input (pre-)validation–  Holds global state

•  Ring-adapter–  http-bindings for invoking the handlers + schema coercion

•  API–  Wrapping everything up + exception handling

Page 19: ClojuTRE2015: Kekkonen - making your Clojure web APIs more awesome

registry  

handler  

handler  

handler  

ns  

ns  

ns  

API  

Ring-­‐hander  

SwaggerUI   O  

O  O  

O  

CQRS-­‐API  O  

ac.on  &  context  

M  

M  M  

request  

Page 20: ClojuTRE2015: Kekkonen - making your Clojure web APIs more awesome

Show  me  

Page 21: ClojuTRE2015: Kekkonen - making your Clojure web APIs more awesome

Extensions•  Registry/Ring/API Options–  Declare things, plug-in transformers

Page 22: ClojuTRE2015: Kekkonen - making your Clojure web APIs more awesome

Extensions•  Handler & namespace meta-data–  Explicit way to extend handler functionality at runtime–  Context enrichment, Authorization, Api-doc info, …

Page 23: ClojuTRE2015: Kekkonen - making your Clojure web APIs more awesome

HTTP API

Page 24: ClojuTRE2015: Kekkonen - making your Clojure web APIs more awesome

Demo  

Page 25: ClojuTRE2015: Kekkonen - making your Clojure web APIs more awesome
Page 26: ClojuTRE2015: Kekkonen - making your Clojure web APIs more awesome

Client-side?•  Expose handler data to the clients as clojure (or

JSON) data–  list all, available or validated

•  Either ask from server or apply the rules for the local dataset–  Server: simple, more traffic–  Client: shared validation-functions, 2+ datasources

Page 27: ClojuTRE2015: Kekkonen - making your Clojure web APIs more awesome
Page 28: ClojuTRE2015: Kekkonen - making your Clojure web APIs more awesome

TODO•  Finalize the lib (1-2 months?)•  Feedback from the community•  RE-Kekkonen (a reagent template)•  Async (just rewrite the api-middleware)•  Event sourcing

Page 29: ClojuTRE2015: Kekkonen - making your Clojure web APIs more awesome

Thanks.  hVps://github.com/metosin/kekkonen  

[email protected]    #ring-­‐swagger  at  Slack  @metosin  at  TwiVer  

 hiring  are  we.