2021-06-01 20:36:52 +03:00
|
|
|
module Api exposing (routes)
|
|
|
|
|
2021-12-20 22:26:12 +03:00
|
|
|
import ApiRoute exposing (ApiRoute)
|
2021-06-01 20:36:52 +03:00
|
|
|
import DataSource exposing (DataSource)
|
2021-12-20 22:26:12 +03:00
|
|
|
import DataSource.Http
|
2021-06-01 20:36:52 +03:00
|
|
|
import Html exposing (Html)
|
2021-12-31 22:14:23 +03:00
|
|
|
import Json.Decode
|
2021-12-20 22:26:12 +03:00
|
|
|
import Json.Encode
|
|
|
|
import OptimizedDecoder as Decode
|
2021-06-01 20:36:52 +03:00
|
|
|
import Route exposing (Route)
|
2021-12-20 22:26:12 +03:00
|
|
|
import Secrets
|
2021-12-28 19:32:25 +03:00
|
|
|
import Server.Request
|
2022-01-01 03:15:41 +03:00
|
|
|
import Server.Response
|
2021-12-24 06:25:51 +03:00
|
|
|
import Server.SetCookie as SetCookie
|
2021-06-01 20:36:52 +03:00
|
|
|
|
|
|
|
|
|
|
|
routes :
|
|
|
|
DataSource (List Route)
|
|
|
|
-> (Html Never -> String)
|
2021-07-31 01:35:02 +03:00
|
|
|
-> List (ApiRoute.ApiRoute ApiRoute.Response)
|
2021-06-01 20:36:52 +03:00
|
|
|
routes getStaticRoutes htmlToString =
|
2021-12-21 18:47:47 +03:00
|
|
|
[ nonHybridRoute
|
2021-12-21 00:35:27 +03:00
|
|
|
, noArgs
|
2021-12-21 01:45:18 +03:00
|
|
|
, redirectRoute
|
2021-12-21 18:47:47 +03:00
|
|
|
, repoStars
|
2021-12-21 20:08:17 +03:00
|
|
|
, repoStars2
|
2021-12-23 08:29:10 +03:00
|
|
|
, logout
|
2021-12-28 22:53:33 +03:00
|
|
|
, greet
|
2021-12-29 18:58:40 +03:00
|
|
|
, fileLength
|
2021-12-31 22:14:23 +03:00
|
|
|
, jsonError
|
2021-12-20 22:26:12 +03:00
|
|
|
]
|
|
|
|
|
|
|
|
|
2021-12-31 22:17:12 +03:00
|
|
|
jsonError : ApiRoute ApiRoute.Response
|
2021-12-31 22:14:23 +03:00
|
|
|
jsonError =
|
2021-12-31 22:17:12 +03:00
|
|
|
ApiRoute.succeed
|
|
|
|
(Server.Request.oneOf
|
|
|
|
[ Server.Request.jsonBodyResult (Decode.field "name" Decode.string)
|
|
|
|
|> Server.Request.map
|
|
|
|
(\result ->
|
|
|
|
case result of
|
|
|
|
Ok firstName ->
|
2022-01-01 03:15:41 +03:00
|
|
|
Server.Response.stringBody
|
2021-12-31 22:17:12 +03:00
|
|
|
("Hello " ++ firstName)
|
2021-12-31 22:14:23 +03:00
|
|
|
|
2021-12-31 22:17:12 +03:00
|
|
|
Err decodeError ->
|
|
|
|
decodeError
|
|
|
|
|> Json.Decode.errorToString
|
2022-01-01 03:15:41 +03:00
|
|
|
|> Server.Response.stringBody
|
|
|
|
|> Server.Response.withStatusCode 400
|
2021-12-31 22:17:12 +03:00
|
|
|
)
|
2022-01-01 03:15:41 +03:00
|
|
|
, Server.Request.succeed (Server.Response.stringBody "Hello anonymous!")
|
2021-12-31 22:17:12 +03:00
|
|
|
]
|
|
|
|
|> Server.Request.map DataSource.succeed
|
|
|
|
)
|
|
|
|
|> ApiRoute.literal "api"
|
|
|
|
|> ApiRoute.slash
|
|
|
|
|> ApiRoute.literal "validate-json"
|
|
|
|
|> ApiRoute.serverRender
|
2021-12-31 22:14:23 +03:00
|
|
|
|
|
|
|
|
2021-12-28 22:53:33 +03:00
|
|
|
greet : ApiRoute ApiRoute.Response
|
|
|
|
greet =
|
|
|
|
ApiRoute.succeed
|
2021-12-30 00:02:45 +03:00
|
|
|
(Server.Request.oneOf
|
|
|
|
[ Server.Request.expectFormPost
|
2021-12-31 06:53:37 +03:00
|
|
|
(\{ field, optionalField } ->
|
2021-12-30 00:02:45 +03:00
|
|
|
field "first"
|
|
|
|
)
|
|
|
|
, Server.Request.expectJsonBody (Decode.field "first" Decode.string)
|
|
|
|
, Server.Request.expectQueryParam "first"
|
|
|
|
, Server.Request.expectMultiPartFormPost
|
|
|
|
(\{ field, optionalField } ->
|
|
|
|
field "first"
|
|
|
|
)
|
2021-12-28 22:53:33 +03:00
|
|
|
]
|
2021-12-31 22:14:23 +03:00
|
|
|
|> Server.Request.map
|
2021-12-30 00:02:45 +03:00
|
|
|
(\firstName ->
|
2022-01-01 03:15:41 +03:00
|
|
|
Server.Response.stringBody ("Hello " ++ firstName)
|
2021-12-30 00:02:45 +03:00
|
|
|
|> DataSource.succeed
|
|
|
|
)
|
2021-12-28 22:53:33 +03:00
|
|
|
)
|
|
|
|
|> ApiRoute.literal "api"
|
|
|
|
|> ApiRoute.slash
|
|
|
|
|> ApiRoute.literal "greet"
|
|
|
|
|> ApiRoute.serverRender
|
|
|
|
|
|
|
|
|
2021-12-29 18:58:40 +03:00
|
|
|
fileLength : ApiRoute ApiRoute.Response
|
|
|
|
fileLength =
|
|
|
|
ApiRoute.succeed
|
|
|
|
(Server.Request.expectMultiPartFormPost
|
|
|
|
(\{ field, optionalField, fileField } ->
|
|
|
|
fileField "file"
|
|
|
|
)
|
2021-12-31 22:14:23 +03:00
|
|
|
|> Server.Request.map
|
2021-12-29 18:58:40 +03:00
|
|
|
(\file ->
|
2022-01-01 03:15:41 +03:00
|
|
|
Server.Response.json
|
2021-12-29 18:58:40 +03:00
|
|
|
(Json.Encode.object
|
|
|
|
[ ( "File name: ", Json.Encode.string file.name )
|
|
|
|
, ( "Length", Json.Encode.int (String.length file.body) )
|
|
|
|
, ( "mime-type", Json.Encode.string file.mimeType )
|
|
|
|
, ( "First line"
|
|
|
|
, Json.Encode.string
|
|
|
|
(file.body
|
|
|
|
|> String.split "\n"
|
|
|
|
|> List.head
|
|
|
|
|> Maybe.withDefault ""
|
|
|
|
)
|
|
|
|
)
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|> DataSource.succeed
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|> ApiRoute.literal "api"
|
|
|
|
|> ApiRoute.slash
|
|
|
|
|> ApiRoute.literal "file"
|
|
|
|
|> ApiRoute.serverRender
|
|
|
|
|
|
|
|
|
2021-12-21 01:45:18 +03:00
|
|
|
redirectRoute : ApiRoute ApiRoute.Response
|
|
|
|
redirectRoute =
|
|
|
|
ApiRoute.succeed
|
2021-12-31 22:14:23 +03:00
|
|
|
(Server.Request.succeed
|
|
|
|
(DataSource.succeed
|
2022-01-01 03:15:41 +03:00
|
|
|
(Server.Response.temporaryRedirect "/")
|
2021-12-31 22:14:23 +03:00
|
|
|
)
|
2021-12-21 01:45:18 +03:00
|
|
|
)
|
|
|
|
|> ApiRoute.literal "api"
|
|
|
|
|> ApiRoute.slash
|
|
|
|
|> ApiRoute.literal "redirect"
|
2021-12-24 00:34:19 +03:00
|
|
|
|> ApiRoute.serverRender
|
2021-12-21 01:45:18 +03:00
|
|
|
|
|
|
|
|
2021-12-21 00:35:27 +03:00
|
|
|
noArgs : ApiRoute ApiRoute.Response
|
|
|
|
noArgs =
|
|
|
|
ApiRoute.succeed
|
2021-12-31 22:14:23 +03:00
|
|
|
(Server.Request.succeed
|
|
|
|
(DataSource.Http.get
|
|
|
|
(Secrets.succeed "https://api.github.com/repos/dillonkearns/elm-pages")
|
|
|
|
(Decode.field "stargazers_count" Decode.int)
|
|
|
|
|> DataSource.map
|
|
|
|
(\stars ->
|
|
|
|
Json.Encode.object
|
|
|
|
[ ( "repo", Json.Encode.string "elm-pages" )
|
|
|
|
, ( "stars", Json.Encode.int stars )
|
|
|
|
]
|
2022-01-01 03:15:41 +03:00
|
|
|
|> Server.Response.json
|
2021-12-31 22:14:23 +03:00
|
|
|
)
|
|
|
|
)
|
2021-12-21 00:35:27 +03:00
|
|
|
)
|
|
|
|
|> ApiRoute.literal "api"
|
|
|
|
|> ApiRoute.slash
|
|
|
|
|> ApiRoute.literal "stars"
|
2021-12-24 00:34:19 +03:00
|
|
|
|> ApiRoute.serverRender
|
2021-12-21 00:35:27 +03:00
|
|
|
|
|
|
|
|
2021-12-20 22:26:12 +03:00
|
|
|
nonHybridRoute =
|
|
|
|
ApiRoute.succeed
|
|
|
|
(\repoName ->
|
|
|
|
DataSource.Http.get
|
|
|
|
(Secrets.succeed ("https://api.github.com/repos/dillonkearns/" ++ repoName))
|
|
|
|
(Decode.field "stargazers_count" Decode.int)
|
|
|
|
|> DataSource.map
|
|
|
|
(\stars ->
|
2021-12-21 01:45:18 +03:00
|
|
|
Json.Encode.object
|
|
|
|
[ ( "repo", Json.Encode.string repoName )
|
|
|
|
, ( "stars", Json.Encode.int stars )
|
|
|
|
]
|
|
|
|
|> Json.Encode.encode 2
|
2021-12-20 22:26:12 +03:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|> ApiRoute.literal "repo"
|
|
|
|
|> ApiRoute.slash
|
|
|
|
|> ApiRoute.capture
|
2021-12-24 02:01:38 +03:00
|
|
|
|> ApiRoute.preRender
|
2021-12-20 22:26:12 +03:00
|
|
|
(\route ->
|
|
|
|
DataSource.succeed
|
|
|
|
[ route "elm-graphql"
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-12-23 08:29:10 +03:00
|
|
|
logout : ApiRoute ApiRoute.Response
|
|
|
|
logout =
|
|
|
|
ApiRoute.succeed
|
2021-12-31 22:14:23 +03:00
|
|
|
(Server.Request.succeed
|
|
|
|
(DataSource.succeed
|
2022-01-01 03:15:41 +03:00
|
|
|
(Server.Response.stringBody "You are logged out"
|
|
|
|
|> Server.Response.withHeader "Set-Cookie"
|
2021-12-31 22:14:23 +03:00
|
|
|
(SetCookie.setCookie "username" ""
|
|
|
|
|> SetCookie.httpOnly
|
|
|
|
|> SetCookie.withPath "/"
|
|
|
|
|> SetCookie.withImmediateExpiration
|
|
|
|
|> SetCookie.toString
|
2021-12-24 06:25:51 +03:00
|
|
|
)
|
2021-12-23 08:29:10 +03:00
|
|
|
)
|
2021-12-31 22:14:23 +03:00
|
|
|
)
|
2021-12-23 08:29:10 +03:00
|
|
|
)
|
|
|
|
|> ApiRoute.literal "api"
|
|
|
|
|> ApiRoute.slash
|
|
|
|
|> ApiRoute.literal "logout"
|
2021-12-24 00:34:19 +03:00
|
|
|
|> ApiRoute.serverRender
|
2021-12-23 08:29:10 +03:00
|
|
|
|
|
|
|
|
2021-12-21 18:47:47 +03:00
|
|
|
repoStars : ApiRoute ApiRoute.Response
|
|
|
|
repoStars =
|
2021-12-20 22:26:12 +03:00
|
|
|
ApiRoute.succeed
|
2021-12-28 19:32:25 +03:00
|
|
|
(\repoName ->
|
2021-12-31 22:14:23 +03:00
|
|
|
Server.Request.succeed
|
|
|
|
(DataSource.Http.get
|
|
|
|
(Secrets.succeed ("https://api.github.com/repos/dillonkearns/" ++ repoName))
|
|
|
|
(Decode.field "stargazers_count" Decode.int)
|
|
|
|
|> DataSource.map
|
|
|
|
(\stars ->
|
|
|
|
Json.Encode.object
|
|
|
|
[ ( "repo", Json.Encode.string repoName )
|
|
|
|
, ( "stars", Json.Encode.int stars )
|
|
|
|
]
|
2022-01-01 03:15:41 +03:00
|
|
|
|> Server.Response.json
|
2021-12-31 22:14:23 +03:00
|
|
|
)
|
|
|
|
)
|
2021-12-20 22:26:12 +03:00
|
|
|
)
|
2021-12-21 18:47:47 +03:00
|
|
|
|> ApiRoute.literal "api"
|
|
|
|
|> ApiRoute.slash
|
2021-12-20 22:26:12 +03:00
|
|
|
|> ApiRoute.literal "repo"
|
|
|
|
|> ApiRoute.slash
|
|
|
|
|> ApiRoute.capture
|
2021-12-21 18:47:47 +03:00
|
|
|
--|> ApiRoute.literal ".json"
|
2021-12-24 00:34:19 +03:00
|
|
|
|> ApiRoute.serverRender
|
2021-12-20 22:26:12 +03:00
|
|
|
|
|
|
|
|
2021-12-21 20:08:17 +03:00
|
|
|
repoStars2 : ApiRoute ApiRoute.Response
|
|
|
|
repoStars2 =
|
|
|
|
ApiRoute.succeed
|
|
|
|
(\repoName ->
|
|
|
|
DataSource.Http.get
|
|
|
|
(Secrets.succeed ("https://api.github.com/repos/dillonkearns/" ++ repoName))
|
|
|
|
(Decode.field "stargazers_count" Decode.int)
|
|
|
|
|> DataSource.map
|
|
|
|
(\stars ->
|
|
|
|
Json.Encode.object
|
|
|
|
[ ( "repo", Json.Encode.string repoName )
|
|
|
|
, ( "stars", Json.Encode.int stars )
|
|
|
|
]
|
2022-01-01 03:15:41 +03:00
|
|
|
|> Server.Response.json
|
2021-12-21 20:08:17 +03:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|> ApiRoute.literal "api2"
|
|
|
|
|> ApiRoute.slash
|
|
|
|
|> ApiRoute.literal "repo"
|
|
|
|
|> ApiRoute.slash
|
|
|
|
|> ApiRoute.capture
|
2021-12-24 00:34:19 +03:00
|
|
|
|> ApiRoute.preRenderWithFallback
|
2021-12-21 20:08:17 +03:00
|
|
|
(\route ->
|
|
|
|
DataSource.succeed
|
|
|
|
[ route "elm-graphql"
|
|
|
|
, route "elm-pages"
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-12-21 18:47:47 +03:00
|
|
|
route1 =
|
2021-12-20 22:26:12 +03:00
|
|
|
ApiRoute.succeed
|
2021-12-21 18:47:47 +03:00
|
|
|
(\repoName ->
|
|
|
|
DataSource.Http.get
|
|
|
|
(Secrets.succeed ("https://api.github.com/repos/dillonkearns/" ++ repoName))
|
|
|
|
(Decode.field "stargazers_count" Decode.int)
|
|
|
|
|> DataSource.map
|
|
|
|
(\stars ->
|
|
|
|
Json.Encode.object
|
|
|
|
[ ( "repo", Json.Encode.string repoName )
|
|
|
|
, ( "stars", Json.Encode.int stars )
|
|
|
|
]
|
|
|
|
|> Json.Encode.encode 2
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|> ApiRoute.literal "repo"
|
|
|
|
|> ApiRoute.slash
|
|
|
|
|> ApiRoute.capture
|
|
|
|
|> ApiRoute.literal ".json"
|