diff --git a/src/System/Serverman/Actions/Nginx.hs b/src/System/Serverman/Actions/Nginx.hs index 66163bd..305ce64 100644 --- a/src/System/Serverman/Actions/Nginx.hs +++ b/src/System/Serverman/Actions/Nginx.hs @@ -20,13 +20,16 @@ module System.Serverman.Actions.Nginx (nginx) where do -- Turn SSL off at first, because we have not yet received a certificate let content = show (params { ssl = False, port = "80" }) - parent = configDirectory serverService "configs" + mainConfig = configDirectory serverService "nginx.conf" + parent = configDirectory serverService "serverman-configs" path = parent domain targetDir = directory createDirectoryIfMissing True targetDir createDirectoryIfMissing True parent + writeIncludeStatementIfMissing mainConfig parent + when ssl $ do let sslPath = configDirectory serverService "ssl.conf" writeFileIfMissing sslPath nginxSSL @@ -71,4 +74,14 @@ module System.Serverman.Actions.Nginx (nginx) where writeFile path (show params) wait =<< restart + writeIncludeStatementIfMissing path target = do + content <- readFile path + + let statement = "include " ++ target ++ "/*" + + when (not (statement `isInfixOf` content)) $ do + let newContent = appendAfter content "http {" (" " ++ statement) + + writeFile path newContent + diff --git a/src/System/Serverman/Actions/WebServer.hs b/src/System/Serverman/Actions/WebServer.hs index 6eab681..3b550ee 100644 --- a/src/System/Serverman/Actions/WebServer.hs +++ b/src/System/Serverman/Actions/WebServer.hs @@ -29,15 +29,15 @@ module System.Serverman.Actions.WebServer (ServerParams(..), ServerType(..)) whe in case serverType conf of Static -> - nginxBlock "server" $ keyvalue (base ++ [("root", directory conf)]) + block "server" $ keyvalue (base ++ [("root", directory conf)]) PortForwarding -> - let proxyBlock = nginxBlock "location /" $ + let proxyBlock = block "location /" $ keyvalue ([ ("proxy_pass", "http://127.0.0.1:" ++ forward conf) , ("proxy_set_header", "X-Forwarded-Host $host") , ("proxy_set_header", "X-Forwarded-Server $host") , ("proxy_set_header", "X-Forwarded-For $proxy_add_x_forwarded_for") ]) - in nginxBlock "server" $ keyvalue base ++ proxyBlock + in block "server" $ keyvalue base ++ proxyBlock | otherwise = "Unknown service provider" diff --git a/src/System/Serverman/Utils.hs b/src/System/Serverman/Utils.hs index e77f7e0..24fad08 100644 --- a/src/System/Serverman/Utils.hs +++ b/src/System/Serverman/Utils.hs @@ -1,8 +1,10 @@ module System.Serverman.Utils ( keyvalue - , nginxBlock + , block , nginxSSL + , indent , writeFileIfMissing , commandError + , appendAfter , execute) where import System.IO @@ -18,8 +20,8 @@ module System.Serverman.Utils ( keyvalue keyvalue ((a, b):xs) = a ++ " " ++ b ++ ";\n" ++ keyvalue xs keyvalue [] = "" - nginxBlock :: String -> String -> String - nginxBlock blockName content = blockName ++ " {\n" ++ indent content ++ "}" + block :: String -> String -> String + block blockName content = blockName ++ " {\n" ++ indent content ++ "}" writeFileIfMissing :: FilePath -> String -> IO () writeFileIfMissing path content = do @@ -28,6 +30,13 @@ module System.Serverman.Utils ( keyvalue when (not exists) $ do writeFile path content + appendAfter :: String -> String -> String -> String + appendAfter content after line = + let ls = lines content + appended = concat $ map (\x -> if x == after then [x, line] else [x]) ls + + in unlines appended + indent :: String -> String indent s = unlines $ map ("\t" ++) (lines s)