[FEATURE][Haskell] Haskell-Servant serves static files (#4058)

* updated the golden files for haskell to be able to generate against those

* Haskell-servant now serves static files which are in a directory called "static"

* I missed to regenerate the docs directory
This commit is contained in:
Fjolnir-Dvorak 2019-10-31 02:47:53 +01:00 committed by Jon Schoning
parent 4958ad74e7
commit d624b28c96
5 changed files with 32 additions and 6 deletions

View File

@ -11,3 +11,4 @@ sidebar_label: haskell
|prependFormOrBodyParameters|Add form or body parameters to the beginning of the parameter list.| |false|
|modelPackage|package for generated models| |null|
|apiPackage|package for generated api classes| |null|
|serveStatic|serve will serve files from the directory 'static'.| |true|

View File

@ -43,6 +43,10 @@ public class HaskellServantCodegen extends DefaultCodegen implements CodegenConf
protected String apiVersion = "0.0.1";
private static final Pattern LEADING_UNDERSCORE = Pattern.compile("^_+");
public static final String PROP_SERVE_STATIC = "serveStatic";
public static final String PROP_SERVE_STATIC_DESC = "serve will serve files from the directory 'static'.";
public static final Boolean PROP_SERVE_STATIC_DEFAULT = Boolean.TRUE;
/**
* Configures the type of generator.
*
@ -183,6 +187,15 @@ public class HaskellServantCodegen extends DefaultCodegen implements CodegenConf
cliOptions.add(new CliOption(CodegenConstants.MODEL_PACKAGE, CodegenConstants.MODEL_PACKAGE_DESC));
cliOptions.add(new CliOption(CodegenConstants.API_PACKAGE, CodegenConstants.API_PACKAGE_DESC));
cliOptions.add(new CliOption(PROP_SERVE_STATIC, PROP_SERVE_STATIC_DESC).defaultValue(PROP_SERVE_STATIC_DEFAULT.toString()));
}
public void setBooleanProperty(String property, Boolean defaultValue) {
if (additionalProperties.containsKey(property)) {
additionalProperties.put(property, convertPropertyToBoolean(property));
} else {
additionalProperties.put(property, defaultValue);
}
}
@Override
@ -192,6 +205,8 @@ public class HaskellServantCodegen extends DefaultCodegen implements CodegenConf
if (StringUtils.isEmpty(System.getenv("HASKELL_POST_PROCESS_FILE"))) {
LOGGER.info("Hint: Environment variable HASKELL_POST_PROCESS_FILE not defined so the Haskell code may not be properly formatted. To define it, try 'export HASKELL_POST_PROCESS_FILE=\"$HOME/.local/bin/hfmt -w\"' (Linux/Mac)");
}
setBooleanProperty(PROP_SERVE_STATIC, PROP_SERVE_STATIC_DEFAULT);
}
/**

View File

@ -61,7 +61,8 @@ import Servant.Client (ClientEnv, Scheme (Http), C
mkClientEnv, parseBaseUrl)
import Servant.Client.Core (baseUrlPort, baseUrlHost)
import Servant.Client.Internal.HttpClient (ClientM (..))
import Servant.Server (Handler (..))
import Servant.Server (Handler (..)){{#serveStatic}}
import Servant.Server.StaticFiles (serveDirectoryFileServer){{/serveStatic}}
import Web.FormUrlEncoded
import Web.HttpApiData
@ -131,7 +132,8 @@ formatSeparatedQueryList char = T.intercalate (T.singleton char) . map toQueryPa
type {{title}}API
= {{#apis}}{{#operations}}{{#operation}}{{& vendorExtensions.x-routeType}} -- '{{operationId}}' route{{#hasMore}}
:<|> {{/hasMore}}{{/operation}}{{/operations}}{{#hasMore}}
:<|> {{/hasMore}}{{/apis}}
:<|> {{/hasMore}}{{/apis}}{{#serveStatic}}
:<|> Raw {{/serveStatic}}
{{/apiInfo}}
@ -183,7 +185,8 @@ create{{title}}Client = {{title}}Backend{..}
where
({{#apis}}{{#operations}}{{#operation}}(coerce -> {{operationId}}){{#hasMore}} :<|>
{{/hasMore}}{{/operation}}{{/operations}}{{#hasMore}} :<|>
{{/hasMore}}{{/apis}}) = client (Proxy :: Proxy {{title}}API)
{{/hasMore}}{{/apis}}{{#serveStatic}} :<|>
_{{/serveStatic}}) = client (Proxy :: Proxy {{title}}API)
-- | Run requests in the {{title}}Client monad.
run{{title}}Client :: Config -> {{title}}Client a -> ExceptT ClientError IO a
@ -234,5 +237,6 @@ run{{title}}MiddlewareServer Config{..} middleware backend = do
serverFromBackend {{title}}Backend{..} =
({{#apis}}{{#operations}}{{#operation}}coerce {{operationId}}{{#hasMore}} :<|>
{{/hasMore}}{{/operation}}{{/operations}}{{#hasMore}} :<|>
{{/hasMore}}{{/apis}})
{{/hasMore}}{{/apis}}{{#serveStatic}} :<|>
serveDirectoryFileServer "static"{{/serveStatic}})
{{/apiInfo}}

View File

@ -19,6 +19,7 @@ package org.openapitools.codegen.options;
import com.google.common.collect.ImmutableMap;
import org.openapitools.codegen.CodegenConstants;
import org.openapitools.codegen.languages.HaskellServantCodegen;
import java.util.Map;
@ -44,6 +45,7 @@ public class HaskellServantOptionsProvider implements OptionsProvider {
.put(CodegenConstants.ENSURE_UNIQUE_PARAMS, ENSURE_UNIQUE_PARAMS_VALUE)
.put(CodegenConstants.ALLOW_UNICODE_IDENTIFIERS, ALLOW_UNICODE_IDENTIFIERS_VALUE)
.put(CodegenConstants.PREPEND_FORM_OR_BODY_PARAMETERS, PREPEND_FORM_OR_BODY_PARAMETERS_VALUE)
.put(HaskellServantCodegen.PROP_SERVE_STATIC, HaskellServantCodegen.PROP_SERVE_STATIC_DEFAULT.toString())
.build();
}

View File

@ -62,6 +62,7 @@ import Servant.Client (ClientEnv, Scheme (Http), C
import Servant.Client.Core (baseUrlPort, baseUrlHost)
import Servant.Client.Internal.HttpClient (ClientM (..))
import Servant.Server (Handler (..))
import Servant.Server.StaticFiles (serveDirectoryFileServer)
import Web.FormUrlEncoded
import Web.HttpApiData
@ -156,6 +157,7 @@ type OpenAPIPetstoreAPI
:<|> "user" :> "login" :> QueryParam "username" Text :> QueryParam "password" Text :> Verb 'GET 200 '[JSON] Text -- 'loginUser' route
:<|> "user" :> "logout" :> Verb 'GET 200 '[JSON] () -- 'logoutUser' route
:<|> "user" :> Capture "username" Text :> ReqBody '[JSON] User :> Verb 'PUT 200 '[JSON] () -- 'updateUser' route
:<|> Raw
-- | Server or client configuration, specifying the host and port to query or serve on.
@ -237,7 +239,8 @@ createOpenAPIPetstoreClient = OpenAPIPetstoreBackend{..}
(coerce -> getUserByName) :<|>
(coerce -> loginUser) :<|>
(coerce -> logoutUser) :<|>
(coerce -> updateUser)) = client (Proxy :: Proxy OpenAPIPetstoreAPI)
(coerce -> updateUser) :<|>
_) = client (Proxy :: Proxy OpenAPIPetstoreAPI)
-- | Run requests in the OpenAPIPetstoreClient monad.
runOpenAPIPetstoreClient :: Config -> OpenAPIPetstoreClient a -> ExceptT ClientError IO a
@ -303,4 +306,5 @@ runOpenAPIPetstoreMiddlewareServer Config{..} middleware backend = do
coerce getUserByName :<|>
coerce loginUser :<|>
coerce logoutUser :<|>
coerce updateUser)
coerce updateUser :<|>
serveDirectoryFileServer "static")