-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
44f7e00
commit 982bdb8
Showing
5 changed files
with
176 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Revision history for dtek.link | ||
|
||
## 0.1.0.0 -- YYYY-mm-dd | ||
|
||
* First version. Released on an unsuspecting world. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
{-# LANGUAGE BlockArguments #-} | ||
{-# LANGUAGE OverloadedStrings #-} | ||
{-# LANGUAGE DeriveAnyClass #-} | ||
{-# LANGUAGE DeriveGeneric #-} | ||
|
||
module Main where | ||
|
||
import Web.Scotty hiding (Options) | ||
import WithCli | ||
import Network.Wai.Middleware.RequestLogger | ||
import Network.Wai.Middleware.Cors | ||
import Control.Monad.IO.Class | ||
import Network.HTTP.Types.Status | ||
import Data.Aeson hiding (Options) | ||
import Data.Aeson.Types hiding (Options) | ||
import Data.Text.Internal.Lazy | ||
import Data.Text hiding (Text, head, filter) | ||
import Network.HTTP.Simple | ||
|
||
data Options = Options | ||
{ | ||
port :: Int | ||
, apiURL :: String | ||
} deriving (Show, Generic, HasArguments) | ||
|
||
data Redirects = Redirects | ||
{ | ||
from :: Text | ||
, to :: Text | ||
} deriving (Show, Generic) | ||
|
||
instance FromJSON Redirects where | ||
parseJSON (Object v) = Redirects <$> | ||
v .: "url" <*> | ||
v .: "link" | ||
parseJSON invalid = | ||
prependFailure "parsing JSONResponse failed, " | ||
(typeMismatch "Object" invalid) | ||
|
||
data Data = Data {redirs :: [Redirects]} | ||
deriving (Show, Generic) | ||
|
||
instance FromJSON Data where | ||
parseJSON (Object v) = Data <$> | ||
v .: "data" | ||
parseJSON invalid = | ||
prependFailure "parsing JSONResponse failed, " | ||
(typeMismatch "Object" invalid) | ||
|
||
|
||
routes :: String -> ScottyM () | ||
routes address = do | ||
middleware logStdout | ||
middleware simpleCors | ||
|
||
get "/:word" $ do | ||
url :: Text <- param "word" | ||
redirects <- liftIO $ fetchRedirs address | ||
let link = to $ head $ filter ((url ==) . from) redirects | ||
redirect link | ||
|
||
notFound $ | ||
status $ mkStatus 404 "Link not found" | ||
|
||
fetchRedirs :: String -> IO [Redirects] | ||
fetchRedirs address = do | ||
request <- parseRequest address | ||
res <- httpJSON request | ||
return $ redirs (getResponseBody res :: Data) | ||
|
||
main :: IO () | ||
main = withCli run | ||
|
||
run :: Options -> IO () | ||
run (Options port api) = | ||
scotty port $ routes api |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
cabal-version: 3.4 | ||
-- The cabal-version field refers to the version of the .cabal specification, | ||
-- and can be different from the cabal-install (the tool) version and the | ||
-- Cabal (the library) version you are using. As such, the Cabal (the library) | ||
-- version used must be equal or greater than the version stated in this field. | ||
-- Starting from the specification version 2.2, the cabal-version field must be | ||
-- the first thing in the cabal file. | ||
|
||
-- Initial package description 'dtek.link' generated by | ||
-- 'cabal init'. For further documentation, see: | ||
-- http://haskell.org/cabal/users-guide/ | ||
-- | ||
-- The name of the package. | ||
name: dtek-link | ||
|
||
-- The package version. | ||
-- See the Haskell package versioning policy (PVP) for standards | ||
-- guiding when and how versions should be incremented. | ||
-- https://pvp.haskell.org | ||
-- PVP summary: +-+------- breaking API changes | ||
-- | | +----- non-breaking API additions | ||
-- | | | +--- code changes with no API change | ||
version: 0.1.0.0 | ||
|
||
-- A short (one-line) description of the package. | ||
synopsis: Dtek's link forwarder | ||
|
||
-- A longer description of the package. | ||
-- description: | ||
|
||
-- The license under which the package is released. | ||
license: AGPL-3.0-or-later | ||
|
||
-- The file containing the license text. | ||
license-file: LICENSE | ||
|
||
-- The package author(s). | ||
-- author: | ||
|
||
-- An email address to which users can send suggestions, bug reports, and patches. | ||
maintainer: [email protected] | ||
|
||
-- A copyright notice. | ||
-- copyright: | ||
category: Web | ||
build-type: Simple | ||
|
||
-- Extra doc files to be distributed with the package, such as a CHANGELOG or a README. | ||
extra-doc-files: CHANGELOG.md | ||
|
||
-- Extra source files to be distributed with the package, such as examples, or a tutorial module. | ||
-- extra-source-files: | ||
|
||
common warnings | ||
ghc-options: -Wall | ||
|
||
executable dtek.link | ||
-- Import common warning flags. | ||
import: warnings | ||
|
||
-- .hs or .lhs file containing the Main module. | ||
main-is: Main.hs | ||
|
||
-- Modules included in this executable, other than Main. | ||
-- other-modules: | ||
|
||
-- LANGUAGE extensions used by modules in this package. | ||
-- other-extensions: | ||
|
||
-- Other library packages from which modules are imported. | ||
build-depends: aeson >=2.1.2.1 | ||
, base ^>=4.17.0.0 | ||
, bytestring >= 0.11.3.1 | ||
, getopt-generics >=0.13.1.0 | ||
, http-conduit >=2.3.8 | ||
, http-types >= 0.12.3 | ||
, scotty >=0.12.1 | ||
, text >= 2.0.1 | ||
, wai-cors >=0.2.7 | ||
, wai-extra >=3.1.13.0 | ||
|
||
-- Directories containing source files. | ||
hs-source-dirs: app | ||
|
||
-- Base language which the package is written in. | ||
default-language: GHC2021 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
cradle: | ||
cabal: | ||
- path: "app/Main.hs" | ||
component: "dtek-link:exe:dtek.link" |