fission-codes / fission

Fission CLI & server
https://runfission.com/docs
119 stars 14 forks source link

Servant Generics (record-style router) #547

Closed expede closed 3 years ago

expede commented 3 years ago

I started this as an experimental riff on #546. It tuns out that it's actually pretty nice!

Before

-- Unversioned top level API
type V_ 
  =    ("ipfs"   :> IPFS)
  :<|> ("app"    :> App)
  :<|> ("heroku" :> "resources" :> Heroku)
  :<|> ("user"   :> User)
  :<|> ("dns"    :> DNS)
  :<|> ("auth"   :> "ucan" :> Auth)
  :<|> (Summary "Simple Ping"
            :> Description "DEPRECATED β›” A quick way to check for liveness"
            --
            :> "ping"
            :> Get '[JSON, PlainText] Ping.Pong)

----------------------------
-- Client (apps subroute) -- 
----------------------------

index :<|> create :<|> update :<|> destroy = client $ Proxy @App

------------
-- Server --
------------

appHandler = index :<|> create :<|> update :<|> destroy

After

data RoutesV_ mode = RoutesV_
  { ipfs   :: mode :- "ipfs"                  :> ToServantApi IPFS.RoutesV_
  , app    :: mode :- "app"                   :> ToServantApi App.Routes
  , heroku :: mode :- "heroku" :> "resources" :> ToServantApi Heroku.Routes
  , user   :: mode :- "user"                  :> ToServantApi User.RoutesV_
  , dns    :: mode :- "dns"                   :> ToServantApi DNS.Routes
  , auth   :: mode :- "auth"   :> "ucan"      :> ToServantApi UCAN.Routes

  , ping ::
      mode
      :- Summary "Simple Ping"
      :> Description "DEPRECATED β›” A quick way to check for liveness"
      --
      :> "ping"
      :> Get '[JSON, PlainText] Ping.Pong
  }
  deriving Generic

------------
-- Client --
------------

--  Subclients come into scope from generated client
appClient@App.Routes {index, create, update, destroy} = genericClient

-- When dot syntax lands (GHC 9.2.0)
topClient.app.create

-------------
-- Server  --
-------------

appHandler = App.Routes {destroy, index, create, update} -- Order independent

What is mode?

mode is the parameter that Servant will use to generate API, server, or client code. For example:

Routes AsApi
Routes (AsClientT ClientM)
MyMonad m => Routes (AsServerT m)

How to Get Plain Servant

They have a bunch of helpers to go to- and from-Servant, aptly namedtoServant and fromServant. There's also some for the type-level, like ToServantApi MyRoutes

Analysis

Upside πŸ˜€

Downside ☹️


Semantic Changes

We haven't deployed #546 yet. This PR removes all of the deprecated paths from /v2/api.

TODO

expede commented 3 years ago

LOL right, maybe updating the tests would be a good idea πŸ˜† πŸ€¦β€β™€οΈ

expede commented 3 years ago

Hey reviewers πŸ‘‹

Just flipped this to ready for review. It's mostly a mechanical conversion to a record of routes, but that does mean a lot of small changes to lots of files. I'm happy to give a live walkthrough if anyone would like

expede commented 3 years ago

βœ… Tests βœ… Build artifacts βœ… Deployed to staging (to spot test existing tools like Drive)

expede commented 3 years ago

CLI (new /v2 routes)

Β»  ~/Desktop/fission setup -R staging
🌱 Setting up environment
πŸͺ Downloading managed IPFS for macOS
πŸ•πŸŽ›οΈ  Configuring managed IPFS
πŸ”‘ Setting up keys
🏠 Do you have an existing account? [Y/n] n
Username: expede-210909-staging2
Email: be.zelenka@gmail.com
βœ… Registration successful! Head over to your email to confirm your account.
πŸŽ›οΈ  Initializing user config file
βœ… Done! Welcome to Fission, expede-210909-staging2 ✨

Β»  ~/Desktop/fission whoami -R staging
πŸ’» Currently logged in as: expede-210909-staging2

Β»  ~/Desktop/fission app register -R staging
πŸ‘· Choose build directory (.):
βœ… App initialized as rich-short-square-alien.fissionapp.net
⏯️  Next run fission app publish [--open|--watch] to sync data
πŸ’ It may take DNS time to propagate this initial setup globally. In this case, you can always view your app at https://ipfs.runfission.com/ipns/rich-short-square-alien.fissionapp.net

Β»  ~/Desktop/fission up -R staging
πŸ•˜πŸ›« App publish local preflight
✈️  Pushing to remote
πŸš€ Now live on the network
πŸ“ DNS updated! Check out your site at:
πŸ”— rich-short-square-alien.fissionapp.net

Drive (backwards-compatible, unversioned API)

Screen Shot 2021-09-09 at 17 43 42

LOL cut off the important line:

πŸͺ΄ DNSLink updated: bafybeifdp2zr5u5o4htv5ssbjqiwib5z7evt7rw2dwd3tzu2wtgkic6ndq