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
-- 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

View File

@ -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"

View File

@ -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)