diff options
author | sternenseemann <git@lukasepple.de> | 2020-07-22 02:43:48 +0200 |
---|---|---|
committer | sternenseemann <git@lukasepple.de> | 2020-07-22 02:47:58 +0200 |
commit | ea4bd2ee5bad2d2d24cd77d901911c249df5ab3f (patch) | |
tree | 571cb31f880c8da9f2d7f61950d1eb81cfba8fc4 | |
parent | d95388562201ab387ae0cff8239fec880d4dd6eb (diff) |
Get paths to dependencies via env vars in backend
* LIKELY_MUSIC_FRONTEND: path to frontend dist folder, for example ./web/dist * LIKELY_MUSIC_SYNTH: a executable which accepts two parameters: 1. INPUT: input midi file 2. OUTPUT: path to output wav file likely-music will call LIKELY_MUSIC_SYNTH with such two parameters and the read the OUTPUT wav file and send it to the user. If an env var is not set, likely-music will behave as it used to, i. e. assume a static location of the dependency.
-rw-r--r-- | backend/Main.hs | 46 | ||||
-rw-r--r-- | likely-music-backend.nix | 5 | ||||
-rw-r--r-- | likely-music.cabal | 1 |
3 files changed, 35 insertions, 17 deletions
diff --git a/backend/Main.hs b/backend/Main.hs index 6ba476b..404d632 100644 --- a/backend/Main.hs +++ b/backend/Main.hs @@ -23,12 +23,14 @@ import Api import Codec.Midi (buildMidi) import Codec.ByteString.Builder import Control.Monad.IO.Class +import Data.Maybe (fromMaybe, fromJust) import Data.ByteString.Lazy (ByteString ()) import qualified Data.ByteString.Lazy as B import Euterpea hiding (app) import GHC.IO.Handle import Network.Wai import Network.Wai.Handler.Warp +import Network.Wai.Application.Static import Servant import Sound.Likely import System.Directory @@ -38,6 +40,7 @@ import System.FilePath.Posix import System.IO import System.Process import System.Random +import WaiAppStatic.Types (toPiece) api :: Proxy LikelyApi api = Proxy @@ -45,8 +48,12 @@ api = Proxy midiString :: ToMusic1 a => Music a -> ByteString midiString = toLazyByteString . buildMidi . toMidi . perform -server :: Server LikelyApi -server = genInterpretation :<|> randomSeed :<|> serveDirectoryWebApp "web/dist" +server :: String -> Server LikelyApi +server distDir = genInterpretation :<|> randomSeed + :<|> serveDirectoryWith serverSettings + where serverSettings = (defaultWebAppSettings distDir) + { ssIndices = [ fromJust (toPiece "index.html") ] + , ssRedirectToIndex = True } randomSeed :: Handler Int randomSeed = liftIO newStdGen >>= return . fst . random @@ -62,18 +69,24 @@ genInterpretation Wav g = genInterpretation Midi g >>= synthWav synthWav :: ByteString -> Handler ByteString synthWav midi = do - inName <- tempFile "mid" - liftIO $ B.writeFile inName midi + inName <- tempFile "mid" outName <- tempFile "wav" + liftIO $ B.writeFile inName midi + + synth <- liftIO $ lookupEnv "LIKELY_MUSIC_SYNTH" + let synthProc = + case synth of + Just synth -> proc synth [ inName, outName ] + -- if env var is missing use old behavior + Nothing -> proc "fluidsynth" + [ "-a", "file" + , "-F", outName + , "-i" + , "/usr/share/soundfonts/FluidR3_GM.sf2" + , inName ] + (_, _, _, ph) <- liftIO $ - createProcess_ "fluidsynth" - (proc "fluidsynth" - [ "-a", "file" - , "-F", outName - , "-i" - , "/usr/share/soundfonts/FluidR3_GM.sf2" - , inName ]) - { std_in = CreatePipe } + createProcess_ "synthWav synthesizer" synthProc { std_in = CreatePipe } code <- liftIO $ waitForProcess ph case code of ExitFailure _ -> throwError err500 { errBody = "fluidsynth failed" } @@ -95,8 +108,11 @@ tempFile ext = try 0 then try (n + 1) else pure path | otherwise = throwError err500 { errBody = "no temp files" } -app :: Application -app = serve api server +app :: String -> Application +app distDir = serve api (server distDir) main :: IO () -main = newStdGen >> run 8081 app +main = do + newStdGen + distDir <- lookupEnv "LIKELY_MUSIC_FRONTEND" + run 8081 $ app (fromMaybe "web/dist" distDir) diff --git a/likely-music-backend.nix b/likely-music-backend.nix index 85aaba1..0333b76 100644 --- a/likely-music-backend.nix +++ b/likely-music-backend.nix @@ -1,6 +1,6 @@ { mkDerivation, aeson, base, bytestring, containers, directory , Euterpea, filepath, HCodecs, PortMidi, process, random, servant -, servant-server, stdenv, text, wai, warp +, servant-server, stdenv, text, wai, wai-app-static, warp }: mkDerivation { pname = "likely-music"; @@ -13,7 +13,8 @@ mkDerivation { ]; executableHaskellDepends = [ aeson base bytestring containers directory Euterpea filepath - HCodecs process random servant servant-server text wai warp + HCodecs process random servant servant-server text wai + wai-app-static warp ]; license = stdenv.lib.licenses.agpl3; } diff --git a/likely-music.cabal b/likely-music.cabal index 68f0cd9..4ab02bd 100644 --- a/likely-music.cabal +++ b/likely-music.cabal @@ -42,6 +42,7 @@ executable likely-music-backend , servant , servant-server , wai + , wai-app-static , warp , HCodecs , random |