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

Dump the swagger-ui files instead of serving them #63

Open
nmattia opened this issue Mar 30, 2019 · 0 comments
Open

Dump the swagger-ui files instead of serving them #63

nmattia opened this issue Mar 30, 2019 · 0 comments

Comments

@nmattia
Copy link

nmattia commented Mar 30, 2019

I couldn't find a function for this:

-- | Dump the swagger schema and swagger-ui files to a directory.
swaggerSchemaUiDump
  :: forall dir api schema
  . (KnownSymbol dir, KnownSymbol schema, Servant.HasSwagger api)
  => FilePath -- ^ directory in which to write
  -> Proxy (Servant.SwaggerSchemaUI dir schema)
  -> Proxy api
  -> IO ()
swaggerSchemaUiDump outDir Proxy p = do
    let dir = symbolVal @dir Proxy
        schema = symbolVal @schema Proxy
        index = T.encodeUtf8 $
          T.replace "SERVANT_SWAGGER_UI_SCHEMA" (T.pack schema) $
          T.replace "SERVANT_SWAGGER_UI_DIR" (T.pack dir) $
          Servant.swaggerUiIndexTemplate
        swagger = Servant.toSwagger p
        -- The paths are prepended with '/' which confuses </>
        uiFiles = first (dropWhile (== '/')) <$> Servant.swaggerUiFiles
        prefix = case dir of
          "" -> outDir
          _ -> outDir </> dir
    let allFiles =
          [(outDir </> schema, BL.toStrict $ Aeson.encode swagger)] <>
          [(prefix </> "index.html", index)] <>
          (first (prefix </>) <$> uiFiles)

    forM_ allFiles $ \(path, content) -> do
      Directory.createDirectoryIfMissing
        True (FilePath.takeDirectory path)
      BS.writeFile path content

Happy to open a PR if you think this'll be useful to others.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant