Skip to content

Commit

Permalink
And we have a new service
Browse files Browse the repository at this point in the history
  • Loading branch information
The1Penguin committed Mar 13, 2023
1 parent 44f7e00 commit 982bdb8
Show file tree
Hide file tree
Showing 5 changed files with 176 additions and 5 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
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.
10 changes: 5 additions & 5 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
GNU AFFERO GENERAL PUBLIC LICENSE
Version 3, 19 November 2007

Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.

Expand Down Expand Up @@ -633,8 +633,8 @@ the "copyright" line and a pointer to where the full notice is found.
Copyright (C) <year> <name of author>

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
Expand All @@ -643,7 +643,7 @@ the "copyright" line and a pointer to where the full notice is found.
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
along with this program. If not, see <http://www.gnu.org/licenses/>.

Also add information on how to contact you by electronic and paper mail.

Expand All @@ -658,4 +658,4 @@ specific requirements.
You should also get your employer (if you work as a programmer) or school,
if any, to sign a "copyright disclaimer" for the program, if necessary.
For more information on this, and how to apply and follow the GNU AGPL, see
<https://www.gnu.org/licenses/>.
<http://www.gnu.org/licenses/>.
76 changes: 76 additions & 0 deletions app/Main.hs
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
86 changes: 86 additions & 0 deletions dtek-link.cabal
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
4 changes: 4 additions & 0 deletions hie.yaml
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"

0 comments on commit 982bdb8

Please sign in to comment.