8 Commits
Author SHA1 Message Date
paul 77887bb29e Bump version to 0.6 2023-04-06 14:24:33 +02:00
paul 7c4fea02d1 Add root route 2023-04-06 14:09:07 +02:00
paul e344ddefb5 Add plaintext root 2023-04-06 13:56:57 +02:00
paul 2024e230a8 Use phases whitelist instead of dont* 2023-04-06 12:54:41 +02:00
paul ce75f2ae7d Fix flake.nix 2023-04-06 12:44:00 +02:00
paul 18946f2d57 Clean up flake.nix a little 2023-04-06 12:35:31 +02:00
paul 7a91c520ee Add hints about keeping stack and nix versions in sync 2023-04-03 14:12:06 +02:00
paul bff14baa1f Flakeify the docker image 2023-04-03 14:11:01 +02:00
7 changed files with 82 additions and 33 deletions
+11 -6
View File
@@ -6,24 +6,29 @@
$ nix build $ nix build
``` ```
## Running
```
$ nix run
```
## Building the Docker image ## Building the Docker image
``` ```
# TODO $ docker load < $(nix build .#docker --print-out-paths)
$ docker load < $(nix-build nix/docker-image.nix)
``` ```
## Development Shell ## Development Shell
Includes Stack, `haskell-language-server`, `gen-hie` etc. Includes Stack, GHC, `haskell-language-server`, `gen-hie` etc.
``` ```
nix develop $ nix develop
``` ```
## Running Stack and GHC ## Running Stack and GHC
``` ```
nix run .#stack $ nix run .#stack
nix run .#ghc $ nix run .#ghc
``` ```
+41 -4
View File
@@ -24,6 +24,7 @@ import Network.Wai (Application)
import Servant import Servant
( Accept (..) ( Accept (..)
, Handler , Handler
, Header
, MimeRender (..) , MimeRender (..)
, Server , Server
, ServerError (..) , ServerError (..)
@@ -61,14 +62,18 @@ app :: Application
app = serve (Proxy :: Proxy API) server app = serve (Proxy :: Proxy API) server
type API = type API =
"utf8" :> Capture "bytes" Text :> Get '[PlainText, HTML] Utf8Model Header "Host" Text :> Get '[PlainText, HTML] RootModel
:<|> "utf8" :> Capture "bytes" Text :> Get '[PlainText, HTML] Utf8Model
:<|> "codepoints" :> Capture "codepoints" Text :> Get '[PlainText, HTML] CodepointsModel :<|> "codepoints" :> Capture "codepoints" Text :> Get '[PlainText, HTML] CodepointsModel
:<|> "text" :> Capture "text" Text :> Get '[PlainText, HTML] TextModel :<|> "text" :> Capture "text" Text :> Get '[PlainText, HTML] TextModel
server :: Server API server :: Server API
server = server =
utf8R :<|> codepointsR :<|> textR rootR :<|> utf8R :<|> codepointsR :<|> textR
where where
rootR host = do
pure $ RootModel $ fromMaybe "" host
utf8R bytesP = do utf8R bytesP = do
bytes <- Parsers.parseHexBytes bytesP `orThrow` const err400 bytes <- Parsers.parseHexBytes bytesP `orThrow` const err400
pure $ mkUtf8Model bytes pure $ mkUtf8Model bytes
@@ -80,6 +85,38 @@ server =
textR textP = do textR textP = do
pure $ TextModel textP pure $ TextModel textP
-- /
newtype RootModel = RootModel
{ host :: Text
}
examples :: [Text]
examples =
[ "/text/✅🤔"
, "/codepoints/x2705+x1F914"
, "/utf8/e2.9c.85.f0.9f.a4.94"
]
instance MimeRender PlainText RootModel where
mimeRender _ model = renderText $ Text.unlines $
[ "⚞ utoy ⚟"
, ""
, "This is utoy, a URL-based Unicode playground. Examples:"
, ""
] ++ map (urlBase <>) examples
where
-- We assume HTTPS here. Doesn't work for development on localhost.
urlBase = "https://" <> model.host
instance MimeRender HTML RootModel where
mimeRender _ model = renderHtml $ documentWithBody $ do
H.h1 $ H.toHtml ("⚞ utoy ⚟" :: Text)
H.p $ H.toHtml ("This is utoy, a URL-based Unicode playground. Examples:" :: Text)
H.ul $ for_ examples $ \example -> do
let url = "https://" <> model.host <> example
H.li $ H.a ! A.href (H.toValue url) $ H.toHtml example
-- /bytes/<bytes> -- /bytes/<bytes>
newtype Utf8Model = Utf8Model newtype Utf8Model = Utf8Model
@@ -197,7 +234,7 @@ instance MimeRender HTML TextModel where
instance MimeRender PlainText TextModel where instance MimeRender PlainText TextModel where
mimeRender _ model = renderText $ Table.render " " mimeRender _ model = renderText $ Table.render " "
[ map (Table.cl) [ map Table.cl
[ Text.pack [c] [ Text.pack [c]
, Text.pack $ printf "U+%04X" c , Text.pack $ printf "U+%04X" c
, Text.pack $ intercalate ", " $ allNames c , Text.pack $ intercalate ", " $ allNames c
@@ -241,7 +278,7 @@ documentWithBody body =
H.meta ! A.charset "utf-8" H.meta ! A.charset "utf-8"
H.title "utoy" H.title "utoy"
H.style $ H.toHtml $ Encoding.decodeUtf8 $(embedFile "static/utoy.css") H.style $ H.toHtml $ Encoding.decodeUtf8 $(embedFile "static/utoy.css")
H.body body H.body $ H.main body
-- HTML routes -- HTML routes
+26 -12
View File
@@ -13,27 +13,37 @@
haskellPackages = pkgs.haskell.packages."${settings.ghc}"; haskellPackages = pkgs.haskell.packages."${settings.ghc}";
ghc = haskellPackages.ghcWithPackages haskellDeps; ghc = haskellPackages.ghcWithPackages haskellDeps;
# Wrap stack to disable its slow Nix integration.
# Instead, make it use the GHC defined above.
stack = pkgs.stdenv.mkDerivation { stack = pkgs.stdenv.mkDerivation {
name = "stack"; name = "stack";
dontUnpack = true;
dontConfigure = true; # The build is simply a call to makeWrapper, so we don't have to
dontBuild = true; # do any of the typical build steps.
phases = [ "installPhase" ];
nativeBuildInputs = [ pkgs.makeWrapper ]; nativeBuildInputs = [ pkgs.makeWrapper ];
# makeBinaryWrapper creates a stack executable for us that uses
# the GHC defined in this file.
installPhase = '' installPhase = ''
makeWrapper ${pkgs.stack}/bin/stack $out/bin/stack \ makeWrapper ${pkgs.stack}/bin/stack $out/bin/stack \
--prefix PATH : ${ghc}/bin \ --prefix PATH : ${ghc}/bin \
--add-flags '--no-nix --system-ghc --no-install-ghc' --add-flags '--no-nix --system-ghc --no-install-ghc'
''; '';
}; };
utoy' =
{ mkDerivation }: utoy = pkgs.haskell.lib.justStaticExecutables (haskellPackages.callPackage
({ mkDerivation }:
mkDerivation { mkDerivation {
version = "0.5"; # Keep this in sync with package.yaml
version = "0.6";
pname = "utoy"; pname = "utoy";
license = pkgs.lib.licenses.mit; license = pkgs.lib.licenses.mit;
src = src =
# We only need these files for building:
let let
buildFiles = [ whitelist = [
./LICENSE ./LICENSE
./utoy.cabal ./utoy.cabal
./Setup.hs ./Setup.hs
@@ -45,17 +55,21 @@
in in
pkgs.lib.sources.cleanSourceWith { pkgs.lib.sources.cleanSourceWith {
src = ./.; src = ./.;
filter = path: _type: pkgs.lib.any (prefix: pkgs.lib.hasPrefix (toString prefix) path) buildFiles; filter = path: _type: pkgs.lib.any (prefix: pkgs.lib.hasPrefix (toString prefix) path) whitelist;
}; };
libraryHaskellDepends = haskellDeps haskellPackages; libraryHaskellDepends = haskellDeps haskellPackages;
}; }) {});
utoy = pkgs.haskell.lib.justStaticExecutables (haskellPackages.callPackage utoy' {});
in { in {
packages.x86_64-linux = { packages.x86_64-linux = {
inherit ghc; inherit ghc;
inherit stack; inherit stack;
inherit utoy;
docker =
pkgs.dockerTools.buildImage {
name = "git.pbrinkmeier.de/paul/utoy";
tag = utoy.version;
config.Cmd = [ "${utoy}/bin/utoy" ];
};
default = utoy; default = utoy;
}; };
-9
View File
@@ -1,9 +0,0 @@
let
pkgs = import ./pkgs.nix {};
utoy = import ../utoy.nix;
in
pkgs.dockerTools.buildImage {
name = "git.pbrinkmeier.de/paul/utoy";
tag = utoy.version;
config.Cmd = [ "${utoy}/bin/utoy" ];
}
+2 -1
View File
@@ -1,7 +1,8 @@
# Adapted from new-template.hsfiles # Adapted from new-template.hsfiles
name: utoy name: utoy
version: 0.5 # Keep this in sync with the version in flake.nix.
version: 0.6
git: "https://git.pbrinkmeier.de/paul/utoy" git: "https://git.pbrinkmeier.de/paul/utoy"
license: MIT license: MIT
author: "Paul Brinkmeier" author: "Paul Brinkmeier"
+1
View File
@@ -1,4 +1,5 @@
# You can get a working environment using nix develop. # You can get a working environment using nix develop.
# Keep this in sync with nix/settings.nix
resolver: ghc-9.4.4 resolver: ghc-9.4.4
packages: packages:
- . - .
+1 -1
View File
@@ -5,7 +5,7 @@ cabal-version: 1.12
-- see: https://github.com/sol/hpack -- see: https://github.com/sol/hpack
name: utoy name: utoy
version: 0.5 version: 0.6
author: Paul Brinkmeier author: Paul Brinkmeier
maintainer: hallo@pbrinkmeier.de maintainer: hallo@pbrinkmeier.de
copyright: 2023 Paul Brinkmeier copyright: 2023 Paul Brinkmeier