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
|
2022-01-24 00:42:50 +03:00
|
|
|
import MySession
|
2022-02-10 20:55:20 +03:00
|
|
|
import Pages.Manifest as Manifest
|
2021-06-01 20:36:52 +03:00
|
|
|
import Route exposing (Route)
|
2021-12-28 19:32:25 +03:00
|
|
|
import Server.Request
|
2022-01-01 03:15:41 +03:00
|
|
|
import Server.Response
|
2022-01-28 20:27:43 +03:00
|
|
|
import Server.Session as Session
|
2022-02-10 20:55:20 +03:00
|
|
|
import Site
|
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
|
2022-02-17 03:54:52 +03:00
|
|
|
, DataSource.succeed manifest |> Manifest.generator Site.canonicalUrl
|
2021-12-20 22:26:12 +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"
|
|
|
|
)
|
2022-01-26 20:01:05 +03:00
|
|
|
, Server.Request.expectJsonBody (Json.Decode.field "first" Json.Decode.string)
|
2021-12-30 00:02:45 +03:00
|
|
|
, 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-18 07:56:37 +03:00
|
|
|
Server.Response.plainText ("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-04-07 02:29:38 +03:00
|
|
|
(Route.redirectTo Route.Index)
|
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
|
2022-01-28 02:45:51 +03:00
|
|
|
"https://api.github.com/repos/dillonkearns/elm-pages"
|
2022-01-26 20:01:05 +03:00
|
|
|
(Json.Decode.field "stargazers_count" Json.Decode.int)
|
2021-12-31 22:14:23 +03:00
|
|
|
|> 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
|
2022-01-28 02:45:51 +03:00
|
|
|
("https://api.github.com/repos/dillonkearns/" ++ repoName)
|
2022-01-26 20:01:05 +03:00
|
|
|
(Json.Decode.field "stargazers_count" Json.Decode.int)
|
2021-12-20 22:26:12 +03:00
|
|
|
|> 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
|
2022-01-24 00:42:50 +03:00
|
|
|
(MySession.withSession
|
|
|
|
(Server.Request.succeed ())
|
|
|
|
(\() sessionResult ->
|
|
|
|
DataSource.succeed
|
|
|
|
( Session.empty
|
2022-04-07 02:29:38 +03:00
|
|
|
, Route.redirectTo Route.Login
|
2022-01-24 00:42:50 +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
|
2022-01-28 02:45:51 +03:00
|
|
|
("https://api.github.com/repos/dillonkearns/" ++ repoName)
|
2022-01-26 20:01:05 +03:00
|
|
|
(Json.Decode.field "stargazers_count" Json.Decode.int)
|
2021-12-31 22:14:23 +03:00
|
|
|
|> 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
|
2022-01-28 02:45:51 +03:00
|
|
|
("https://api.github.com/repos/dillonkearns/" ++ repoName)
|
2022-01-26 20:01:05 +03:00
|
|
|
(Json.Decode.field "stargazers_count" Json.Decode.int)
|
2021-12-21 20:08:17 +03:00
|
|
|
|> 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
|
2022-01-28 02:45:51 +03:00
|
|
|
("https://api.github.com/repos/dillonkearns/" ++ repoName)
|
2022-01-26 20:01:05 +03:00
|
|
|
(Json.Decode.field "stargazers_count" Json.Decode.int)
|
2021-12-21 18:47:47 +03:00
|
|
|
|> 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"
|
2022-02-10 20:55:20 +03:00
|
|
|
|
|
|
|
|
|
|
|
manifest : Manifest.Config
|
|
|
|
manifest =
|
|
|
|
Manifest.init
|
|
|
|
{ name = "Site Name"
|
|
|
|
, description = "Description"
|
|
|
|
, startUrl = Route.Index |> Route.toPath
|
|
|
|
, icons = []
|
|
|
|
}
|