Skip to content
Newer
Older
100644 168 lines (142 sloc) 5.72 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)
89725fd @jgm Initial commit.
authored
27 import Control.Monad.Reader
18ff134 @jgm Added math config option, native mathml support.
authored
28 import System.Log.Logger (Priority(..), setLevel, setHandlers,
a2e3f0f Code cleanup/refactoring.
John MacFarlane authored
29 getLogger, saveGlobalLogger)
d932596 @jgm Added logging, fixed debug mode.
authored
30 import System.Log.Handler.Simple (fileHandler)
5de695a Moved option parsing code from Config module to main program.
John MacFarlane authored
31 import System.Environment
32 import System.Exit
fc0b5eb @jgm Use custom readFileUTF8 instead of broken System.IO.UTF8.
authored
33 import System.IO (stderr)
5de695a Moved option parsing code from Config module to main program.
John MacFarlane authored
34 import System.Console.GetOpt
fa1b0c9 Added command line parameter to specify listen device
Timo B. Hübel authored
35 import Network.Socket hiding (Debug)
36 import Network.URI
5de695a Moved option parsing code from Config module to main program.
John MacFarlane authored
37 import Data.Version (showVersion)
fc0b5eb @jgm Use custom readFileUTF8 instead of broken System.IO.UTF8.
authored
38 import qualified Data.ByteString as B
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
5de695a Moved option parsing code from Config module to main program.
John MacFarlane authored
47 opts <- getArgs >>= parseArgs
48 defaultConfig <- getDefaultConfig
49 conf <- foldM handleFlag defaultConfig opts
6a79fb7 @jgm Check for external programs depending on whether Git or Darcs is used.
authored
50 -- check for external programs that are needed
13b66f4 @jgm Added support for the new mercurial filestore backend.
authored
51 let repoProg = case repositoryType conf of
52 Mercurial -> "hg"
53 Darcs -> "darcs"
54 Git -> "git"
d7d984f @jgm Refactoring:
authored
55 let prereqs = ["grep", repoProg]
6a79fb7 @jgm Check for external programs depending on whether Git or Darcs is used.
authored
56 forM_ prereqs $ \prog ->
57 findExecutable prog >>= \mbFind ->
d789539 @jgm Code cleanup/refactoring: initializeWiki is now two functions.
authored
58 when (isNothing mbFind) $ error $
59 "Required program '" ++ prog ++ "' not found in system path."
2ff8918 @jgm Code cleanup for readability.
authored
60
d932596 @jgm Added logging, fixed debug mode.
authored
61 -- set up logging
8bc81b5 @jgm Moved jsMath from AppState -> Config.
authored
62 let level = if debugMode conf then DEBUG else logLevel conf
d932596 @jgm Added logging, fixed debug mode.
authored
63 logFileHandler <- fileHandler (logFile conf) level
f73ea92 @jgm Changed name of server log to Happstack.Server.AccessLog.Combined.
authored
64 serverLogger <- getLogger "Happstack.Server.AccessLog.Combined"
d932596 @jgm Added logging, fixed debug mode.
authored
65 gititLogger <- getLogger "gitit"
66 saveGlobalLogger $ setLevel level $ setHandlers [logFileHandler] serverLogger
67 saveGlobalLogger $ setLevel level $ setHandlers [logFileHandler] gititLogger
68
18ff134 @jgm Added math config option, native mathml support.
authored
69 let conf' = conf{logLevel = level}
24695bf @jgm Moved wikiHandler function from gitit.hs to Network.Gitit.
authored
70
2e7f4e9 @jgm New createTemplateIfMissing function in Initialize.
authored
71 -- setup the page repository, template, and static files, if they don't exist
24695bf @jgm Moved wikiHandler function from gitit.hs to Network.Gitit.
authored
72 createRepoIfMissing conf'
2e7f4e9 @jgm New createTemplateIfMissing function in Initialize.
authored
73 createStaticIfMissing conf'
74 createTemplateIfMissing conf'
d7d984f @jgm Refactoring:
authored
75
25ffe1e @jgm Added custom.css. Initialize only copies logo, footer, custom.css.
authored
76 -- initialize state
77 initializeGititState conf'
78
24695bf @jgm Moved wikiHandler function from gitit.hs to Network.Gitit.
authored
79 let serverConf = Conf { validator = Nothing, port = portNumber conf' }
5de695a Moved option parsing code from Config module to main program.
John MacFarlane authored
80
fa1b0c9 Added command line parameter to specify listen device
Timo B. Hübel authored
81 -- open the requested interface
82 sock <- socket AF_INET Stream defaultProtocol
83 setSocketOption sock ReuseAddr 1
84 device <- inet_addr (getListenOrDefault opts)
85 bindSocket sock (SockAddrInet (toEnum (portNumber conf')) device)
86 listen sock 10
87
7bc9711 @jgm Be sure to create the template before you read it!
authored
88 -- start the server
fa1b0c9 Added command line parameter to specify listen device
Timo B. Hübel authored
89 simpleHTTPWithSocket sock serverConf $ msum [ wiki conf'
5de695a Moved option parsing code from Config module to main program.
John MacFarlane authored
90 , dir "_reloadTemplates" reloadTemplates
91 ]
92
93 data Opt
94 = Help
95 | ConfigFile FilePath
96 | Port Int
fa1b0c9 Added command line parameter to specify listen device
Timo B. Hübel authored
97 | Listen String
5de695a Moved option parsing code from Config module to main program.
John MacFarlane authored
98 | Debug
99 | Version
100 | PrintDefaultConfig
101 deriving (Eq)
102
103 flags :: [OptDescr Opt]
104 flags =
105 [ Option ['h'] ["help"] (NoArg Help)
106 "Print this help message"
107 , Option ['v'] ["version"] (NoArg Version)
108 "Print version information"
109 , Option ['p'] ["port"] (ReqArg (Port . read) "PORT")
110 "Specify port"
fa1b0c9 Added command line parameter to specify listen device
Timo B. Hübel authored
111 , Option ['l'] ["listen"] (ReqArg (Listen . checkListen) "INTERFACE")
112 "Specify interface to listen on"
5de695a Moved option parsing code from Config module to main program.
John MacFarlane authored
113 , Option [] ["print-default-config"] (NoArg PrintDefaultConfig)
114 "Print default configuration"
115 , Option [] ["debug"] (NoArg Debug)
116 "Print debugging information on each request"
117 , Option ['f'] ["config-file"] (ReqArg ConfigFile "FILE")
118 "Specify configuration file"
119 ]
120
fa1b0c9 Added command line parameter to specify listen device
Timo B. Hübel authored
121 checkListen :: String -> String
122 checkListen l | isIPv6address l = l
123 | isIPv4address l = l
124 | otherwise = error "Gitit.checkListen: Not a valid interface name"
125
126 getListenOrDefault :: [Opt] -> String
127 getListenOrDefault [] = "127.0.0.1"
128 getListenOrDefault ((Listen l):_) = l
129 getListenOrDefault (_:os) = getListenOrDefault os
130
5de695a Moved option parsing code from Config module to main program.
John MacFarlane authored
131 parseArgs :: [String] -> IO [Opt]
132 parseArgs argv = do
133 progname <- getProgName
134 case getOpt Permute flags argv of
135 (opts,_,[]) -> return opts
fc0b5eb @jgm Use custom readFileUTF8 instead of broken System.IO.UTF8.
authored
136 (_,_,errs) -> putErr (ExitFailure 1) (concat errs ++ usageInfo (usageHeader progname) flags)
5de695a Moved option parsing code from Config module to main program.
John MacFarlane authored
137
138 usageHeader :: String -> String
139 usageHeader progname = "Usage: " ++ progname ++ " [opts...]"
140
141 copyrightMessage :: String
142 copyrightMessage = "\nCopyright (C) 2008 John MacFarlane\n" ++
143 "This is free software; see the source for copying conditions. There is no\n" ++
144 "warranty, not even for merchantability or fitness for a particular purpose."
145
146 compileInfo :: String
147 compileInfo =
148 #ifdef _PLUGINS
149 " +plugins"
150 #else
151 " -plugins"
152 #endif
153
154 handleFlag :: Config -> Opt -> IO Config
155 handleFlag conf opt = do
156 progname <- getProgName
157 case opt of
fc0b5eb @jgm Use custom readFileUTF8 instead of broken System.IO.UTF8.
authored
158 Help -> putErr ExitSuccess (usageInfo (usageHeader progname) flags)
159 Version -> putErr ExitSuccess (progname ++ " version " ++ showVersion version ++ compileInfo ++ copyrightMessage)
160 PrintDefaultConfig -> getDataFileName "data/default.conf" >>= readFileUTF8 >>= B.putStrLn . fromString >> exitWith ExitSuccess
5de695a Moved option parsing code from Config module to main program.
John MacFarlane authored
161 Debug -> return conf{ debugMode = True }
162 Port p -> return conf{ portNumber = p }
163 ConfigFile fname -> getConfigFromFile fname
fa1b0c9 Added command line parameter to specify listen device
Timo B. Hübel authored
164 Listen _ -> return conf
5de695a Moved option parsing code from Config module to main program.
John MacFarlane authored
165
fc0b5eb @jgm Use custom readFileUTF8 instead of broken System.IO.UTF8.
authored
166 putErr :: ExitCode -> String -> IO a
167 putErr c s = B.hPutStrLn stderr (fromString s) >> exitWith c
Something went wrong with that request. Please try again.