Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 171 lines (142 sloc) 5.759 kb
5de695a Moved option parsing code from Config module to main program.
John MacFarlane authored
1 {-# LANGUAGE CPP #-}
89725fd @jgm Initial commit.
authored
2 {-
3 Copyright (C) 2008 John MacFarlane <jgm@berkeley.edu>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 -}
19
20 module Main where
21
24695bf @jgm Moved wikiHandler function from gitit.hs to Network.Gitit.
authored
22 import Network.Gitit
2e4d87e @jgm Moved Gitit under Network namespace.
authored
23 import Network.Gitit.Server
fc0b5eb @jgm Use custom readFileUTF8 instead of broken System.IO.UTF8.
authored
24 import Network.Gitit.Util (readFileUTF8)
89725fd @jgm Initial commit.
authored
25 import System.Directory
523f92b @jgm Moved wiki handlers into a separate module, Gitit.Handlers.
authored
26 import Data.Maybe (isNothing)
b602533 @jgm Relaxed package bounds, fixed compiler warnings.
authored
27 import Network.Gitit.Compat.Except()
89725fd @jgm Initial commit.
authored
28 import Control.Monad.Reader
18ff134 @jgm Added math config option, native mathml support.
authored
29 import System.Log.Logger (Priority(..), setLevel, setHandlers,
a2e3f0f Code cleanup/refactoring.
John MacFarlane authored
30 getLogger, saveGlobalLogger)
d932596 @jgm Added logging, fixed debug mode.
authored
31 import System.Log.Handler.Simple (fileHandler)
5de695a Moved option parsing code from Config module to main program.
John MacFarlane authored
32 import System.Environment
33 import System.Exit
fc0b5eb @jgm Use custom readFileUTF8 instead of broken System.IO.UTF8.
authored
34 import System.IO (stderr)
5de695a Moved option parsing code from Config module to main program.
John MacFarlane authored
35 import System.Console.GetOpt
fa1b0c9 @tbk303 Added command line parameter to specify listen device
tbk303 authored
36 import Network.Socket hiding (Debug)
5de695a Moved option parsing code from Config module to main program.
John MacFarlane authored
37 import Data.Version (showVersion)
c82f68c @jgm Fixed warnings/updated for newer happstack.
authored
38 import qualified Data.ByteString.Char8 as B
fc0b5eb @jgm Use custom readFileUTF8 instead of broken System.IO.UTF8.
authored
39 import Data.ByteString.UTF8 (fromString)
40
5de695a Moved option parsing code from Config module to main program.
John MacFarlane authored
41 import Paths_gitit (version, getDataFileName)
89725fd @jgm Initial commit.
authored
42
43 main :: IO ()
44 main = do
2ff8918 @jgm Code cleanup for readability.
authored
45
46 -- parse options to get config file
c52180f @bmillwood Don't read config for --help or --version
bmillwood authored
47 args <- getArgs >>= parseArgs
48
49 -- sequence in Either monad gets first Left or all Rights
50 opts <- case sequence args of
fbd5af1 @bmillwood Include the default.conf location in --help
bmillwood authored
51 Left Help -> putErr ExitSuccess =<< usageMessage
52 Left Version -> do
53 progname <- getProgName
54 putErr ExitSuccess (progname ++ " version " ++
55 showVersion version ++ compileInfo ++ copyrightMessage)
c52180f @bmillwood Don't read config for --help or --version
bmillwood authored
56 Left PrintDefaultConfig -> getDataFileName "data/default.conf" >>=
a48453f @freiric hlint cosmetics
freiric authored
57 readFileUTF8 >>= B.putStrLn . fromString >> exitSuccess
c52180f @bmillwood Don't read config for --help or --version
bmillwood authored
58 Right xs -> return xs
59
9d3d109 @jgm Fixed regression in option parsing.
authored
60 conf' <- case [f | ConfigFile f <- opts] of
5612c37 @freiric configuration in several files (to separate usual configuration from oau...
freiric authored
61 fs -> getConfigFromFiles fs
9d3d109 @jgm Fixed regression in option parsing.
authored
62
63 let conf = foldl handleFlag conf' opts
64
6a79fb7 @jgm Check for external programs depending on whether Git or Darcs is used.
authored
65 -- check for external programs that are needed
13b66f4 @jgm Added support for the new mercurial filestore backend.
authored
66 let repoProg = case repositoryType conf of
67 Mercurial -> "hg"
68 Darcs -> "darcs"
69 Git -> "git"
d7d984f @jgm Refactoring:
authored
70 let prereqs = ["grep", repoProg]
6a79fb7 @jgm Check for external programs depending on whether Git or Darcs is used.
authored
71 forM_ prereqs $ \prog ->
72 findExecutable prog >>= \mbFind ->
d789539 @jgm Code cleanup/refactoring: initializeWiki is now two functions.
authored
73 when (isNothing mbFind) $ error $
74 "Required program '" ++ prog ++ "' not found in system path."
2ff8918 @jgm Code cleanup for readability.
authored
75
d932596 @jgm Added logging, fixed debug mode.
authored
76 -- set up logging
8bc81b5 @jgm Moved jsMath from AppState -> Config.
authored
77 let level = if debugMode conf then DEBUG else logLevel conf
d932596 @jgm Added logging, fixed debug mode.
authored
78 logFileHandler <- fileHandler (logFile conf) level
f73ea92 @jgm Changed name of server log to Happstack.Server.AccessLog.Combined.
authored
79 serverLogger <- getLogger "Happstack.Server.AccessLog.Combined"
d932596 @jgm Added logging, fixed debug mode.
authored
80 gititLogger <- getLogger "gitit"
81 saveGlobalLogger $ setLevel level $ setHandlers [logFileHandler] serverLogger
82 saveGlobalLogger $ setLevel level $ setHandlers [logFileHandler] gititLogger
83
2e7f4e9 @jgm New createTemplateIfMissing function in Initialize.
authored
84 -- setup the page repository, template, and static files, if they don't exist
9d3d109 @jgm Fixed regression in option parsing.
authored
85 createRepoIfMissing conf
86 createStaticIfMissing conf
87 createTemplateIfMissing conf
d7d984f @jgm Refactoring:
authored
88
25ffe1e @jgm Added custom.css. Initialize only copies logo, footer, custom.css.
authored
89 -- initialize state
9d3d109 @jgm Fixed regression in option parsing.
authored
90 initializeGititState conf
25ffe1e @jgm Added custom.css. Initialize only copies logo, footer, custom.css.
authored
91
9d3d109 @jgm Fixed regression in option parsing.
authored
92 let serverConf = nullConf { validator = Nothing, port = portNumber conf,
c82f68c @jgm Fixed warnings/updated for newer happstack.
authored
93 timeout = 20, logAccess = Nothing }
5de695a Moved option parsing code from Config module to main program.
John MacFarlane authored
94
fa1b0c9 @tbk303 Added command line parameter to specify listen device
tbk303 authored
95 -- open the requested interface
96 sock <- socket AF_INET Stream defaultProtocol
97 setSocketOption sock ReuseAddr 1
6e3998e @larose Added address config option.
larose authored
98 device <- inet_addr (address conf)
9d3d109 @jgm Fixed regression in option parsing.
authored
99 bindSocket sock (SockAddrInet (toEnum (portNumber conf)) device)
fa1b0c9 @tbk303 Added command line parameter to specify listen device
tbk303 authored
100 listen sock 10
101
7bc9711 @jgm Be sure to create the template before you read it!
authored
102 -- start the server
9d3d109 @jgm Fixed regression in option parsing.
authored
103 simpleHTTPWithSocket sock serverConf $ msum [ wiki conf
5de695a Moved option parsing code from Config module to main program.
John MacFarlane authored
104 , dir "_reloadTemplates" reloadTemplates
105 ]
106
c52180f @bmillwood Don't read config for --help or --version
bmillwood authored
107 data ExitOpt
5de695a Moved option parsing code from Config module to main program.
John MacFarlane authored
108 = Help
109 | Version
110 | PrintDefaultConfig
c52180f @bmillwood Don't read config for --help or --version
bmillwood authored
111
112 data ConfigOpt
113 = ConfigFile FilePath
114 | Port Int
115 | Listen String
116 | Debug
5de695a Moved option parsing code from Config module to main program.
John MacFarlane authored
117 deriving (Eq)
118
c52180f @bmillwood Don't read config for --help or --version
bmillwood authored
119 type Opt = Either ExitOpt ConfigOpt
120
5de695a Moved option parsing code from Config module to main program.
John MacFarlane authored
121 flags :: [OptDescr Opt]
122 flags =
c52180f @bmillwood Don't read config for --help or --version
bmillwood authored
123 [ Option ['h'] ["help"] (NoArg (Left Help))
5de695a Moved option parsing code from Config module to main program.
John MacFarlane authored
124 "Print this help message"
c52180f @bmillwood Don't read config for --help or --version
bmillwood authored
125 , Option ['v'] ["version"] (NoArg (Left Version))
5de695a Moved option parsing code from Config module to main program.
John MacFarlane authored
126 "Print version information"
c52180f @bmillwood Don't read config for --help or --version
bmillwood authored
127 , Option ['p'] ["port"] (ReqArg (Right . Port . read) "PORT")
5de695a Moved option parsing code from Config module to main program.
John MacFarlane authored
128 "Specify port"
6e3998e @larose Added address config option.
larose authored
129 , Option ['l'] ["listen"] (ReqArg (Right . Listen) "INTERFACE")
b2a1300 @andyring Edited gitit.hs via GitHub
andyring authored
130 "Specify IP address to listen on"
c52180f @bmillwood Don't read config for --help or --version
bmillwood authored
131 , Option [] ["print-default-config"] (NoArg (Left PrintDefaultConfig))
5de695a Moved option parsing code from Config module to main program.
John MacFarlane authored
132 "Print default configuration"
c52180f @bmillwood Don't read config for --help or --version
bmillwood authored
133 , Option [] ["debug"] (NoArg (Right Debug))
5de695a Moved option parsing code from Config module to main program.
John MacFarlane authored
134 "Print debugging information on each request"
c52180f @bmillwood Don't read config for --help or --version
bmillwood authored
135 , Option ['f'] ["config-file"] (ReqArg (Right . ConfigFile) "FILE")
5de695a Moved option parsing code from Config module to main program.
John MacFarlane authored
136 "Specify configuration file"
137 ]
138
139 parseArgs :: [String] -> IO [Opt]
a48453f @freiric hlint cosmetics
freiric authored
140 parseArgs argv =
5de695a Moved option parsing code from Config module to main program.
John MacFarlane authored
141 case getOpt Permute flags argv of
142 (opts,_,[]) -> return opts
fbd5af1 @bmillwood Include the default.conf location in --help
bmillwood authored
143 (_,_,errs) -> putErr (ExitFailure 1) . (concat errs ++) =<< usageMessage
5de695a Moved option parsing code from Config module to main program.
John MacFarlane authored
144
fbd5af1 @bmillwood Include the default.conf location in --help
bmillwood authored
145 usageMessage :: IO String
146 usageMessage = do
147 progname <- getProgName
148 return $ usageInfo ("Usage: " ++ progname ++ " [opts...]") flags
5de695a Moved option parsing code from Config module to main program.
John MacFarlane authored
149
150 copyrightMessage :: String
151 copyrightMessage = "\nCopyright (C) 2008 John MacFarlane\n" ++
152 "This is free software; see the source for copying conditions. There is no\n" ++
153 "warranty, not even for merchantability or fitness for a particular purpose."
154
155 compileInfo :: String
156 compileInfo =
157 #ifdef _PLUGINS
158 " +plugins"
159 #else
160 " -plugins"
161 #endif
162
9d3d109 @jgm Fixed regression in option parsing.
authored
163 handleFlag :: Config -> ConfigOpt -> Config
164 handleFlag conf Debug = conf{ debugMode = True, logLevel = DEBUG }
165 handleFlag conf (Port p) = conf { portNumber = p }
6e3998e @larose Added address config option.
larose authored
166 handleFlag conf (Listen l) = conf { address = l }
9d3d109 @jgm Fixed regression in option parsing.
authored
167 handleFlag conf _ = conf
5de695a Moved option parsing code from Config module to main program.
John MacFarlane authored
168
fc0b5eb @jgm Use custom readFileUTF8 instead of broken System.IO.UTF8.
authored
169 putErr :: ExitCode -> String -> IO a
170 putErr c s = B.hPutStrLn stderr (fromString s) >> exitWith c
Something went wrong with that request. Please try again.