fix: write include statement in nginx config

This commit is contained in:
Mahdi Dibaiee 2017-02-22 13:16:25 +03:30
parent bff0c6230b
commit b8fe5c304e
3 changed files with 29 additions and 7 deletions

View File

@ -20,13 +20,16 @@ module System.Serverman.Actions.Nginx (nginx) where
do do
-- Turn SSL off at first, because we have not yet received a certificate -- Turn SSL off at first, because we have not yet received a certificate
let content = show (params { ssl = False, port = "80" }) 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 path = parent </> domain
targetDir = directory targetDir = directory
createDirectoryIfMissing True targetDir createDirectoryIfMissing True targetDir
createDirectoryIfMissing True parent createDirectoryIfMissing True parent
writeIncludeStatementIfMissing mainConfig parent
when ssl $ do when ssl $ do
let sslPath = configDirectory serverService </> "ssl.conf" let sslPath = configDirectory serverService </> "ssl.conf"
writeFileIfMissing sslPath nginxSSL writeFileIfMissing sslPath nginxSSL
@ -71,4 +74,14 @@ module System.Serverman.Actions.Nginx (nginx) where
writeFile path (show params) writeFile path (show params)
wait =<< restart 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

View File

@ -29,15 +29,15 @@ module System.Serverman.Actions.WebServer (ServerParams(..), ServerType(..)) whe
in in
case serverType conf of case serverType conf of
Static -> Static ->
nginxBlock "server" $ keyvalue (base ++ [("root", directory conf)]) block "server" $ keyvalue (base ++ [("root", directory conf)])
PortForwarding -> PortForwarding ->
let proxyBlock = nginxBlock "location /" $ let proxyBlock = block "location /" $
keyvalue ([ ("proxy_pass", "http://127.0.0.1:" ++ forward conf) keyvalue ([ ("proxy_pass", "http://127.0.0.1:" ++ forward conf)
, ("proxy_set_header", "X-Forwarded-Host $host") , ("proxy_set_header", "X-Forwarded-Host $host")
, ("proxy_set_header", "X-Forwarded-Server $host") , ("proxy_set_header", "X-Forwarded-Server $host")
, ("proxy_set_header", "X-Forwarded-For $proxy_add_x_forwarded_for") , ("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" | otherwise = "Unknown service provider"

View File

@ -1,8 +1,10 @@
module System.Serverman.Utils ( keyvalue module System.Serverman.Utils ( keyvalue
, nginxBlock , block
, nginxSSL , nginxSSL
, indent
, writeFileIfMissing , writeFileIfMissing
, commandError , commandError
, appendAfter
, execute) where , execute) where
import System.IO import System.IO
@ -18,8 +20,8 @@ module System.Serverman.Utils ( keyvalue
keyvalue ((a, b):xs) = a ++ " " ++ b ++ ";\n" ++ keyvalue xs keyvalue ((a, b):xs) = a ++ " " ++ b ++ ";\n" ++ keyvalue xs
keyvalue [] = "" keyvalue [] = ""
nginxBlock :: String -> String -> String block :: String -> String -> String
nginxBlock blockName content = blockName ++ " {\n" ++ indent content ++ "}" block blockName content = blockName ++ " {\n" ++ indent content ++ "}"
writeFileIfMissing :: FilePath -> String -> IO () writeFileIfMissing :: FilePath -> String -> IO ()
writeFileIfMissing path content = do writeFileIfMissing path content = do
@ -28,6 +30,13 @@ module System.Serverman.Utils ( keyvalue
when (not exists) $ do when (not exists) $ do
writeFile path content 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 :: String -> String
indent s = unlines $ map ("\t" ++) (lines s) indent s = unlines $ map ("\t" ++) (lines s)