From 80b70bb868075dd3c9e663ed2a6f13f9a6205afc Mon Sep 17 00:00:00 2001 From: sternenseemann Date: Mon, 2 Jan 2017 14:26:31 +0100 Subject: Distribute a default css --- README.md | 5 +++-- gopher-proxy.cabal | 6 +++--- gopher-proxy.nix | 10 +++++----- src/GopherProxy/Params.hs | 6 +++--- src/Main.hs | 13 +++++++++++-- 5 files changed, 25 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 58c48c7..04e407c 100644 --- a/README.md +++ b/README.md @@ -6,15 +6,16 @@ gopher-proxy is a tiny [wai](https://hackage.haskell.org/package/wai) applicatio Example usage: - gopher-proxy --host foo.org --http-port 8080 --css-path ./gopher-proxy.css + gopher-proxy --host foo.org --http-port 8080 -In this particular example, gopher-proxy would proxy the foo.org gopher server, bind its http service on `127.0.0.1:8080` (to be proxied to by another web server like `nginx`) and use the specified css file. +In this particular example, gopher-proxy would proxy the foo.org gopher server and bind its http service on `127.0.0.1:8080` (to be proxied to by another web server like `nginx`). There are these additional flags which allow tweaking of exact behavior as well: option | meaning ------------------|-------------------------------------------------------------------------------------------------------- `--port` | The port of the gopher server, defaults to `70` +`--css-url` | Use some specific css file instead of the default one. `--css-url` | The http path of the css file, defaults to `/gopher-proxy.css` (should be changed, if your gopher server has a file with the same name `--base-url` | The path of the directory which will appear as root directory of gopher-proxy to the user, defaults to `/`. Should be changed if you configured your proxying web server to expose gopher-proxy as, say `/gopher-space/`. `--listen-public` | If this flag is set, gopher-proxy will accept connections on its public IP address(es). diff --git a/gopher-proxy.cabal b/gopher-proxy.cabal index 82461d1..391b55a 100644 --- a/gopher-proxy.cabal +++ b/gopher-proxy.cabal @@ -8,17 +8,16 @@ license: GPL-3 license-file: LICENSE author: sternenseemann maintainer: git@lukasepple.de --- copyright: category: Network build-type: Simple extra-source-files: ChangeLog.md cabal-version: >=1.10 +data-files: gopher-proxy.css executable gopher-proxy main-is: Main.hs default-extensions: CPP - -- other-modules: - -- other-extensions: + other-modules: Paths_gopher_proxy build-depends: base , wai , warp @@ -31,5 +30,6 @@ executable gopher-proxy , errors , mime-types , optparse-applicative + , directory hs-source-dirs: src default-language: Haskell2010 diff --git a/gopher-proxy.nix b/gopher-proxy.nix index 9d8e448..d943fc4 100644 --- a/gopher-proxy.nix +++ b/gopher-proxy.nix @@ -1,6 +1,6 @@ -{ mkDerivation, attoparsec, base, bytestring, errors, http-types -, lucid, mime-types, network, optparse-applicative -, optparse-generic, stdenv, text, wai, warp +{ mkDerivation, attoparsec, base, bytestring, directory, errors +, http-types, lucid, mime-types, network, optparse-applicative +, stdenv, text, wai, warp }: mkDerivation { pname = "gopher-proxy"; @@ -9,8 +9,8 @@ mkDerivation { isLibrary = false; isExecutable = true; executableHaskellDepends = [ - attoparsec base bytestring errors http-types lucid mime-types - network optparse-applicative optparse-generic text wai warp + attoparsec base bytestring directory errors http-types lucid + mime-types network optparse-applicative text wai warp ]; description = "proxy gopher over http"; license = stdenv.lib.licenses.gpl3; diff --git a/src/GopherProxy/Params.hs b/src/GopherProxy/Params.hs index c99d099..e39f8f1 100644 --- a/src/GopherProxy/Params.hs +++ b/src/GopherProxy/Params.hs @@ -18,7 +18,7 @@ data Params { hostname :: HostName , port :: PortNumber , httpPort :: Int - , cssPath :: FilePath + , cssPath :: Maybe FilePath , cssUrl :: BS.ByteString , baseUrl :: Text , listenPublic :: Bool @@ -41,10 +41,10 @@ params = Params (long "http-port" <> metavar "PORT" <> help "port gopher-proxy should listen on") - <*> strOption + <*> optional (strOption (long "css-path" <> metavar "PATH" - <> help "path of the css to be used") + <> help "path of the css to be used")) <*> optionalWithDefault "/gopher-proxy.css" (option auto (long "css-url" <> metavar "PATH" diff --git a/src/Main.hs b/src/Main.hs index b521a5a..2ce71d8 100644 --- a/src/Main.hs +++ b/src/Main.hs @@ -8,6 +8,7 @@ import GopherProxy.Types import GopherProxy.Protocol import GopherProxy.Params +import Paths_gopher_proxy import Prelude hiding (takeWhile) import Control.Exception @@ -32,6 +33,7 @@ import Network.Wai.Handler.Warp import Lucid import qualified Options.Applicative as O import System.IO (stderr) +import System.Directory (doesFileExist) gopherProxy :: Params -> Application gopherProxy cfg r resp @@ -44,8 +46,15 @@ gopherProxy cfg r resp cssResponse :: Params -> Application cssResponse cfg _ respond = do - css <- B.readFile . cssPath $ cfg - respond $ responseLBS status200 [("Content-type", "text/css")] css + path <- case cssPath cfg of + Just p -> pure p + Nothing -> getDataFileName "gopher-proxy.css" + exists <- doesFileExist path + if exists + then B.readFile path >>= + respond . responseLBS status200 [("Content-type", "text/css")] + else respond $ + responseLBS status404 [("Content-type", "text/plain")] "Could not find css" gopherResponse :: Params -> Application gopherResponse cfg r respond = do -- cgit 1.4.1