Commit | Line | Data |
---|---|---|
a51d37c1 EW |
1 | #!/bin/sh |
2 | # | |
3 | # Copyright (c) 2006 Eric Wong | |
4 | # | |
c2db2e0e | 5 | |
c5699693 | 6 | PERL='@@PERL@@' |
c2db2e0e PH |
7 | OPTIONS_KEEPDASHDASH= |
8 | OPTIONS_SPEC="\ | |
1b1dd23f | 9 | git instaweb [options] (--start | --stop | --restart) |
c2db2e0e PH |
10 | -- |
11 | l,local only bind on 127.0.0.1 | |
12 | p,port= the port to bind to | |
13 | d,httpd= the command to launch | |
14 | b,browser= the browser to launch | |
15 | m,module-path= the module path (only needed for apache2) | |
16 | Action | |
17 | stop stop the web server | |
18 | start start the web server | |
19 | restart restart the web server | |
20 | " | |
a51d37c1 EW |
21 | |
22 | . git-sh-setup | |
23 | ||
b2bc9a30 | 24 | fqgitdir="$GIT_DIR" |
43d60d2e FP |
25 | local="$(git config --bool --get instaweb.local)" |
26 | httpd="$(git config --get instaweb.httpd)" | |
c0cb4ed3 | 27 | root="$(git config --get instaweb.gitwebdir)" |
43d60d2e FP |
28 | port=$(git config --get instaweb.port) |
29 | module_path="$(git config --get instaweb.modulepath)" | |
c0175f92 | 30 | action="browse" |
a51d37c1 | 31 | |
9126425d | 32 | conf="$GIT_DIR/gitweb/httpd.conf" |
a51d37c1 EW |
33 | |
34 | # Defaults: | |
35 | ||
82e5a82f | 36 | # if installed, it doesn't need further configuration (module_path) |
a51d37c1 EW |
37 | test -z "$httpd" && httpd='lighttpd -f' |
38 | ||
c0cb4ed3 PKS |
39 | # Default is @@GITWEBDIR@@ |
40 | test -z "$root" && root='@@GITWEBDIR@@' | |
41 | ||
a51d37c1 EW |
42 | # any untaken local port will do... |
43 | test -z "$port" && port=1234 | |
44 | ||
43d60d2e FP |
45 | resolve_full_httpd () { |
46 | case "$httpd" in | |
4bdf8599 DM |
47 | *apache2*|*lighttpd*|*httpd*) |
48 | # yes, *httpd* covers *lighttpd* above, but it is there for clarity | |
43d60d2e | 49 | # ensure that the apache2/lighttpd command ends with "-f" |
e1622bfc | 50 | if ! echo "$httpd" | sane_grep -- '-f *$' >/dev/null 2>&1 |
43d60d2e FP |
51 | then |
52 | httpd="$httpd -f" | |
53 | fi | |
54 | ;; | |
78646987 JN |
55 | *plackup*) |
56 | # server is started by running via generated gitweb.psgi in $fqgitdir/gitweb | |
57 | full_httpd="$fqgitdir/gitweb/gitweb.psgi" | |
58 | httpd_only="${httpd%% *}" # cut on first space | |
59 | return | |
60 | ;; | |
422bff28 | 61 | *webrick*) |
f46e1304 | 62 | # server is started by running via generated webrick.rb in |
422bff28 | 63 | # $fqgitdir/gitweb |
f46e1304 | 64 | full_httpd="$fqgitdir/gitweb/webrick.rb" |
422bff28 EW |
65 | httpd_only="${httpd%% *}" # cut on first space |
66 | return | |
67 | ;; | |
43d60d2e FP |
68 | esac |
69 | ||
70 | httpd_only="$(echo $httpd | cut -f1 -d' ')" | |
e47eec8f | 71 | if case "$httpd_only" in /*) : ;; *) which $httpd_only >/dev/null 2>&1;; esac |
a51d37c1 | 72 | then |
43d60d2e | 73 | full_httpd=$httpd |
a51d37c1 EW |
74 | else |
75 | # many httpds are installed in /usr/sbin or /usr/local/sbin | |
76 | # these days and those are not in most users $PATHs | |
14b45b66 MD |
77 | # in addition, we may have generated a server script |
78 | # in $fqgitdir/gitweb. | |
c0cb4ed3 | 79 | for i in /usr/local/sbin /usr/sbin "$root" "$fqgitdir/gitweb" |
a51d37c1 EW |
80 | do |
81 | if test -x "$i/$httpd_only" | |
82 | then | |
43d60d2e | 83 | full_httpd=$i/$httpd |
a51d37c1 EW |
84 | return |
85 | fi | |
86 | done | |
43d60d2e FP |
87 | |
88 | echo >&2 "$httpd_only not found. Install $httpd_only or use" \ | |
89 | "--httpd to specify another httpd daemon." | |
f281e3a1 | 90 | exit 1 |
a51d37c1 | 91 | fi |
43d60d2e FP |
92 | } |
93 | ||
94 | start_httpd () { | |
0b624b4c SB |
95 | if test -f "$fqgitdir/pid"; then |
96 | say "Instance already running. Restarting..." | |
97 | stop_httpd | |
98 | fi | |
99 | ||
43d60d2e FP |
100 | # here $httpd should have a meaningful value |
101 | resolve_full_httpd | |
5ad6d387 JN |
102 | mkdir -p "$fqgitdir/gitweb/$httpd_only" |
103 | conf="$fqgitdir/gitweb/$httpd_only.conf" | |
104 | ||
105 | # generate correct config file if it doesn't exist | |
106 | test -f "$conf" || configure_httpd | |
107 | test -f "$fqgitdir/gitweb/gitweb_config.perl" || gitweb_conf | |
43d60d2e FP |
108 | |
109 | # don't quote $full_httpd, there can be arguments to it (-f) | |
0ded4758 | 110 | case "$httpd" in |
78646987 JN |
111 | *mongoose*|*plackup*) |
112 | #These servers don't have a daemon mode so we'll have to fork it | |
48bf76ca | 113 | $full_httpd "$conf" & |
0ded4758 WL |
114 | #Save the pid before doing anything else (we'll print it later) |
115 | pid=$! | |
116 | ||
117 | if test $? != 0; then | |
118 | echo "Could not execute http daemon $httpd." | |
119 | exit 1 | |
120 | fi | |
121 | ||
122 | cat > "$fqgitdir/pid" <<EOF | |
123 | $pid | |
124 | EOF | |
125 | ;; | |
126 | *) | |
48bf76ca | 127 | $full_httpd "$conf" |
0ded4758 WL |
128 | if test $? != 0; then |
129 | echo "Could not execute http daemon $httpd." | |
130 | exit 1 | |
131 | fi | |
132 | ;; | |
133 | esac | |
a51d37c1 EW |
134 | } |
135 | ||
136 | stop_httpd () { | |
43d60d2e | 137 | test -f "$fqgitdir/pid" && kill $(cat "$fqgitdir/pid") |
d1127622 | 138 | rm -f "$fqgitdir/pid" |
a51d37c1 EW |
139 | } |
140 | ||
d94775e1 JN |
141 | httpd_is_ready () { |
142 | "$PERL" -MIO::Socket::INET -e " | |
143 | local \$| = 1; # turn on autoflush | |
144 | exit if (IO::Socket::INET->new('127.0.0.1:$port')); | |
145 | print 'Waiting for \'$httpd\' to start ..'; | |
146 | do { | |
147 | print '.'; | |
148 | sleep(1); | |
149 | } until (IO::Socket::INET->new('127.0.0.1:$port')); | |
150 | print qq! (done)\n!; | |
151 | " | |
152 | } | |
153 | ||
822f7c73 | 154 | while test $# != 0 |
a51d37c1 EW |
155 | do |
156 | case "$1" in | |
157 | --stop|stop) | |
c0175f92 | 158 | action="stop" |
a51d37c1 EW |
159 | ;; |
160 | --start|start) | |
c0175f92 | 161 | action="start" |
a51d37c1 EW |
162 | ;; |
163 | --restart|restart) | |
c0175f92 | 164 | action="restart" |
a51d37c1 | 165 | ;; |
c2db2e0e | 166 | -l|--local) |
a51d37c1 EW |
167 | local=true |
168 | ;; | |
c2db2e0e PH |
169 | -d|--httpd) |
170 | shift | |
171 | httpd="$1" | |
172 | ;; | |
173 | -b|--browser) | |
174 | shift | |
175 | browser="$1" | |
a51d37c1 | 176 | ;; |
c2db2e0e PH |
177 | -p|--port) |
178 | shift | |
179 | port="$1" | |
a51d37c1 | 180 | ;; |
c2db2e0e PH |
181 | -m|--module-path) |
182 | shift | |
183 | module_path="$1" | |
a51d37c1 | 184 | ;; |
c2db2e0e | 185 | --) |
a51d37c1 EW |
186 | ;; |
187 | *) | |
188 | usage | |
189 | ;; | |
190 | esac | |
191 | shift | |
192 | done | |
193 | ||
194 | mkdir -p "$GIT_DIR/gitweb/tmp" | |
43d60d2e | 195 | GIT_EXEC_PATH="$(git --exec-path)" |
a51d37c1 | 196 | GIT_DIR="$fqgitdir" |
c0cb4ed3 PKS |
197 | GITWEB_CONFIG="$fqgitdir/gitweb/gitweb_config.perl" |
198 | export GIT_EXEC_PATH GIT_DIR GITWEB_CONFIG | |
a51d37c1 | 199 | |
425b78e8 | 200 | webrick_conf () { |
422bff28 EW |
201 | # webrick seems to have no way of passing arbitrary environment |
202 | # variables to the underlying CGI executable, so we wrap the | |
203 | # actual gitweb.cgi using a shell script to force it | |
204 | wrapper="$fqgitdir/gitweb/$httpd/wrapper.sh" | |
205 | cat > "$wrapper" <<EOF | |
206 | #!/bin/sh | |
207 | # we use this shell script wrapper around the real gitweb.cgi since | |
208 | # there appears to be no other way to pass arbitrary environment variables | |
209 | # into the CGI process | |
210 | GIT_EXEC_PATH=$GIT_EXEC_PATH GIT_DIR=$GIT_DIR GITWEB_CONFIG=$GITWEB_CONFIG | |
211 | export GIT_EXEC_PATH GIT_DIR GITWEB_CONFIG | |
212 | exec $root/gitweb.cgi | |
213 | EOF | |
214 | chmod +x "$wrapper" | |
215 | ||
216 | # This assumes _ruby_ is in the user's $PATH. that's _one_ | |
217 | # portable way to run ruby, which could be installed anywhere, really. | |
425b78e8 MD |
218 | # generate a standalone server script in $fqgitdir/gitweb. |
219 | cat >"$fqgitdir/gitweb/$httpd.rb" <<EOF | |
f46e1304 | 220 | #!/usr/bin/env ruby |
425b78e8 | 221 | require 'webrick' |
e9323e7f | 222 | require 'logger' |
f46e1304 EW |
223 | options = { |
224 | :Port => $port, | |
225 | :DocumentRoot => "$root", | |
e9323e7f EW |
226 | :Logger => Logger.new('$fqgitdir/gitweb/error.log'), |
227 | :AccessLog => [ | |
228 | [ Logger.new('$fqgitdir/gitweb/access.log'), | |
229 | WEBrick::AccessLog::COMBINED_LOG_FORMAT ] | |
230 | ], | |
f46e1304 EW |
231 | :DirectoryIndex => ["gitweb.cgi"], |
232 | :CGIInterpreter => "$wrapper", | |
233 | :StartCallback => lambda do | |
234 | File.open("$fqgitdir/pid", "w") { |f| f.puts Process.pid } | |
235 | end, | |
236 | :ServerType => WEBrick::Daemon, | |
237 | } | |
238 | options[:BindAddress] = '127.0.0.1' if "$local" == "true" | |
425b78e8 MD |
239 | server = WEBrick::HTTPServer.new(options) |
240 | ['INT', 'TERM'].each do |signal| | |
241 | trap(signal) {server.shutdown} | |
242 | end | |
243 | server.start | |
244 | EOF | |
f46e1304 EW |
245 | chmod +x "$fqgitdir/gitweb/$httpd.rb" |
246 | # configuration is embedded in server script file, webrick.rb | |
247 | rm -f "$conf" | |
425b78e8 MD |
248 | } |
249 | ||
a51d37c1 EW |
250 | lighttpd_conf () { |
251 | cat > "$conf" <<EOF | |
c0cb4ed3 | 252 | server.document-root = "$root" |
a51d37c1 | 253 | server.port = $port |
e47eec8f | 254 | server.modules = ( "mod_setenv", "mod_cgi" ) |
a51d37c1 EW |
255 | server.indexfiles = ( "gitweb.cgi" ) |
256 | server.pid-file = "$fqgitdir/pid" | |
be5347b3 | 257 | server.errorlog = "$fqgitdir/gitweb/$httpd_only/error.log" |
e47eec8f RJ |
258 | |
259 | # to enable, add "mod_access", "mod_accesslog" to server.modules | |
260 | # variable above and uncomment this | |
be5347b3 | 261 | #accesslog.filename = "$fqgitdir/gitweb/$httpd_only/access.log" |
e47eec8f | 262 | |
c0cb4ed3 | 263 | setenv.add-environment = ( "PATH" => env.PATH, "GITWEB_CONFIG" => env.GITWEB_CONFIG ) |
e47eec8f | 264 | |
a51d37c1 | 265 | cgi.assign = ( ".cgi" => "" ) |
e47eec8f RJ |
266 | |
267 | # mimetype mapping | |
268 | mimetype.assign = ( | |
269 | ".pdf" => "application/pdf", | |
270 | ".sig" => "application/pgp-signature", | |
271 | ".spl" => "application/futuresplash", | |
272 | ".class" => "application/octet-stream", | |
273 | ".ps" => "application/postscript", | |
274 | ".torrent" => "application/x-bittorrent", | |
275 | ".dvi" => "application/x-dvi", | |
276 | ".gz" => "application/x-gzip", | |
277 | ".pac" => "application/x-ns-proxy-autoconfig", | |
278 | ".swf" => "application/x-shockwave-flash", | |
279 | ".tar.gz" => "application/x-tgz", | |
280 | ".tgz" => "application/x-tgz", | |
281 | ".tar" => "application/x-tar", | |
282 | ".zip" => "application/zip", | |
283 | ".mp3" => "audio/mpeg", | |
284 | ".m3u" => "audio/x-mpegurl", | |
285 | ".wma" => "audio/x-ms-wma", | |
286 | ".wax" => "audio/x-ms-wax", | |
287 | ".ogg" => "application/ogg", | |
288 | ".wav" => "audio/x-wav", | |
289 | ".gif" => "image/gif", | |
290 | ".jpg" => "image/jpeg", | |
291 | ".jpeg" => "image/jpeg", | |
292 | ".png" => "image/png", | |
293 | ".xbm" => "image/x-xbitmap", | |
294 | ".xpm" => "image/x-xpixmap", | |
295 | ".xwd" => "image/x-xwindowdump", | |
296 | ".css" => "text/css", | |
297 | ".html" => "text/html", | |
298 | ".htm" => "text/html", | |
299 | ".js" => "text/javascript", | |
300 | ".asc" => "text/plain", | |
301 | ".c" => "text/plain", | |
302 | ".cpp" => "text/plain", | |
303 | ".log" => "text/plain", | |
304 | ".conf" => "text/plain", | |
305 | ".text" => "text/plain", | |
306 | ".txt" => "text/plain", | |
307 | ".dtd" => "text/xml", | |
308 | ".xml" => "text/xml", | |
309 | ".mpeg" => "video/mpeg", | |
310 | ".mpg" => "video/mpeg", | |
311 | ".mov" => "video/quicktime", | |
312 | ".qt" => "video/quicktime", | |
313 | ".avi" => "video/x-msvideo", | |
314 | ".asf" => "video/x-ms-asf", | |
315 | ".asx" => "video/x-ms-asf", | |
316 | ".wmv" => "video/x-ms-wmv", | |
317 | ".bz2" => "application/x-bzip", | |
318 | ".tbz" => "application/x-bzip-compressed-tar", | |
319 | ".tar.bz2" => "application/x-bzip-compressed-tar", | |
320 | "" => "text/plain" | |
321 | ) | |
a51d37c1 | 322 | EOF |
9126425d | 323 | test x"$local" = xtrue && echo 'server.bind = "127.0.0.1"' >> "$conf" |
a51d37c1 EW |
324 | } |
325 | ||
326 | apache2_conf () { | |
4bdf8599 DM |
327 | if test -z "$module_path" |
328 | then | |
329 | test -d "/usr/lib/httpd/modules" && | |
330 | module_path="/usr/lib/httpd/modules" | |
331 | test -d "/usr/lib/apache2/modules" && | |
332 | module_path="/usr/lib/apache2/modules" | |
333 | fi | |
a51d37c1 | 334 | bind= |
9126425d | 335 | test x"$local" = xtrue && bind='127.0.0.1:' |
e24c76bf | 336 | echo 'text/css css' > "$fqgitdir/mime.types" |
a51d37c1 | 337 | cat > "$conf" <<EOF |
44a167b0 | 338 | ServerName "git-instaweb" |
c0cb4ed3 PKS |
339 | ServerRoot "$root" |
340 | DocumentRoot "$root" | |
be5347b3 PKS |
341 | ErrorLog "$fqgitdir/gitweb/$httpd_only/error.log" |
342 | CustomLog "$fqgitdir/gitweb/$httpd_only/access.log" combined | |
a51d37c1 EW |
343 | PidFile "$fqgitdir/pid" |
344 | Listen $bind$port | |
44a167b0 EW |
345 | EOF |
346 | ||
4bdf8599 DM |
347 | for mod in mime dir env log_config |
348 | do | |
349 | if test -e $module_path/mod_${mod}.so | |
350 | then | |
44a167b0 EW |
351 | echo "LoadModule ${mod}_module " \ |
352 | "$module_path/mod_${mod}.so" >> "$conf" | |
353 | fi | |
354 | done | |
355 | cat >> "$conf" <<EOF | |
e24c76bf | 356 | TypesConfig "$fqgitdir/mime.types" |
a51d37c1 EW |
357 | DirectoryIndex gitweb.cgi |
358 | EOF | |
359 | ||
360 | # check to see if Dennis Stosberg's mod_perl compatibility patch | |
361 | # (<20060621130708.Gcbc6e5c@leonov.stosberg.net>) has been applied | |
e1622bfc | 362 | if test -f "$module_path/mod_perl.so" && |
c0cb4ed3 | 363 | sane_grep 'MOD_PERL' "$root/gitweb.cgi" >/dev/null |
a51d37c1 EW |
364 | then |
365 | # favor mod_perl if available | |
366 | cat >> "$conf" <<EOF | |
367 | LoadModule perl_module $module_path/mod_perl.so | |
368 | PerlPassEnv GIT_DIR | |
2989f516 | 369 | PerlPassEnv GIT_EXEC_PATH |
c0cb4ed3 | 370 | PerlPassEnv GITWEB_CONFIG |
a51d37c1 EW |
371 | <Location /gitweb.cgi> |
372 | SetHandler perl-script | |
373 | PerlResponseHandler ModPerl::Registry | |
374 | PerlOptions +ParseHeaders | |
375 | Options +ExecCGI | |
376 | </Location> | |
377 | EOF | |
378 | else | |
379 | # plain-old CGI | |
43d60d2e | 380 | resolve_full_httpd |
9524cf29 | 381 | list_mods=$(echo "$full_httpd" | sed 's/-f$/-l/') |
e1622bfc | 382 | $list_mods | sane_grep 'mod_cgi\.c' >/dev/null 2>&1 || \ |
10d1432a MR |
383 | if test -f "$module_path/mod_cgi.so" |
384 | then | |
385 | echo "LoadModule cgi_module $module_path/mod_cgi.so" >> "$conf" | |
386 | else | |
387 | $list_mods | grep 'mod_cgid\.c' >/dev/null 2>&1 || \ | |
388 | if test -f "$module_path/mod_cgid.so" | |
389 | then | |
390 | echo "LoadModule cgid_module $module_path/mod_cgid.so" \ | |
391 | >> "$conf" | |
392 | else | |
393 | echo "You have no CGI support!" | |
394 | exit 2 | |
395 | fi | |
396 | echo "ScriptSock logs/gitweb.sock" >> "$conf" | |
397 | fi | |
a51d37c1 | 398 | cat >> "$conf" <<EOF |
2989f516 DM |
399 | PassEnv GIT_DIR |
400 | PassEnv GIT_EXEC_PATH | |
401 | PassEnv GITWEB_CONFIG | |
a51d37c1 EW |
402 | AddHandler cgi-script .cgi |
403 | <Location /gitweb.cgi> | |
404 | Options +ExecCGI | |
405 | </Location> | |
406 | EOF | |
407 | fi | |
408 | } | |
409 | ||
0ded4758 WL |
410 | mongoose_conf() { |
411 | cat > "$conf" <<EOF | |
412 | # Mongoose web server configuration file. | |
413 | # Lines starting with '#' and empty lines are ignored. | |
414 | # For detailed description of every option, visit | |
415 | # http://code.google.com/p/mongoose/wiki/MongooseManual | |
416 | ||
c0cb4ed3 | 417 | root $root |
0ded4758 WL |
418 | ports $port |
419 | index_files gitweb.cgi | |
420 | #ssl_cert $fqgitdir/gitweb/ssl_cert.pem | |
be5347b3 PKS |
421 | error_log $fqgitdir/gitweb/$httpd_only/error.log |
422 | access_log $fqgitdir/gitweb/$httpd_only/access.log | |
0ded4758 WL |
423 | |
424 | #cgi setup | |
c0cb4ed3 | 425 | cgi_env PATH=$PATH,GIT_DIR=$GIT_DIR,GIT_EXEC_PATH=$GIT_EXEC_PATH,GITWEB_CONFIG=$GITWEB_CONFIG |
0ded4758 WL |
426 | cgi_interp $PERL |
427 | cgi_ext cgi,pl | |
428 | ||
429 | # mimetype mapping | |
430 | mime_types .gz=application/x-gzip,.tar.gz=application/x-tgz,.tgz=application/x-tgz,.tar=application/x-tar,.zip=application/zip,.gif=image/gif,.jpg=image/jpeg,.jpeg=image/jpeg,.png=image/png,.css=text/css,.html=text/html,.htm=text/html,.js=text/javascript,.c=text/plain,.cpp=text/plain,.log=text/plain,.conf=text/plain,.text=text/plain,.txt=text/plain,.dtd=text/xml,.bz2=application/x-bzip,.tbz=application/x-bzip-compressed-tar,.tar.bz2=application/x-bzip-compressed-tar | |
431 | EOF | |
432 | } | |
433 | ||
78646987 JN |
434 | plackup_conf () { |
435 | # generate a standalone 'plackup' server script in $fqgitdir/gitweb | |
436 | # with embedded configuration; it does not use "$conf" file | |
437 | cat > "$fqgitdir/gitweb/gitweb.psgi" <<EOF | |
438 | #!$PERL | |
439 | ||
440 | # gitweb - simple web interface to track changes in git repositories | |
441 | # PSGI wrapper and server starter (see http://plackperl.org) | |
442 | ||
443 | use strict; | |
444 | ||
445 | use IO::Handle; | |
446 | use Plack::MIME; | |
447 | use Plack::Builder; | |
448 | use Plack::App::WrapCGI; | |
449 | use CGI::Emulate::PSGI 0.07; # minimum version required to work with gitweb | |
450 | ||
451 | # mimetype mapping (from lighttpd_conf) | |
452 | Plack::MIME->add_type( | |
453 | ".pdf" => "application/pdf", | |
454 | ".sig" => "application/pgp-signature", | |
455 | ".spl" => "application/futuresplash", | |
456 | ".class" => "application/octet-stream", | |
457 | ".ps" => "application/postscript", | |
458 | ".torrent" => "application/x-bittorrent", | |
459 | ".dvi" => "application/x-dvi", | |
460 | ".gz" => "application/x-gzip", | |
461 | ".pac" => "application/x-ns-proxy-autoconfig", | |
462 | ".swf" => "application/x-shockwave-flash", | |
463 | ".tar.gz" => "application/x-tgz", | |
464 | ".tgz" => "application/x-tgz", | |
465 | ".tar" => "application/x-tar", | |
466 | ".zip" => "application/zip", | |
467 | ".mp3" => "audio/mpeg", | |
468 | ".m3u" => "audio/x-mpegurl", | |
469 | ".wma" => "audio/x-ms-wma", | |
470 | ".wax" => "audio/x-ms-wax", | |
471 | ".ogg" => "application/ogg", | |
472 | ".wav" => "audio/x-wav", | |
473 | ".gif" => "image/gif", | |
474 | ".jpg" => "image/jpeg", | |
475 | ".jpeg" => "image/jpeg", | |
476 | ".png" => "image/png", | |
477 | ".xbm" => "image/x-xbitmap", | |
478 | ".xpm" => "image/x-xpixmap", | |
479 | ".xwd" => "image/x-xwindowdump", | |
480 | ".css" => "text/css", | |
481 | ".html" => "text/html", | |
482 | ".htm" => "text/html", | |
483 | ".js" => "text/javascript", | |
484 | ".asc" => "text/plain", | |
485 | ".c" => "text/plain", | |
486 | ".cpp" => "text/plain", | |
487 | ".log" => "text/plain", | |
488 | ".conf" => "text/plain", | |
489 | ".text" => "text/plain", | |
490 | ".txt" => "text/plain", | |
491 | ".dtd" => "text/xml", | |
492 | ".xml" => "text/xml", | |
493 | ".mpeg" => "video/mpeg", | |
494 | ".mpg" => "video/mpeg", | |
495 | ".mov" => "video/quicktime", | |
496 | ".qt" => "video/quicktime", | |
497 | ".avi" => "video/x-msvideo", | |
498 | ".asf" => "video/x-ms-asf", | |
499 | ".asx" => "video/x-ms-asf", | |
500 | ".wmv" => "video/x-ms-wmv", | |
501 | ".bz2" => "application/x-bzip", | |
502 | ".tbz" => "application/x-bzip-compressed-tar", | |
503 | ".tar.bz2" => "application/x-bzip-compressed-tar", | |
504 | "" => "text/plain" | |
505 | ); | |
506 | ||
507 | my \$app = builder { | |
508 | # to be able to override \$SIG{__WARN__} to log build time warnings | |
509 | use CGI::Carp; # it sets \$SIG{__WARN__} itself | |
510 | ||
511 | my \$logdir = "$fqgitdir/gitweb/$httpd_only"; | |
512 | open my \$access_log_fh, '>>', "\$logdir/access.log" | |
513 | or die "Couldn't open access log '\$logdir/access.log': \$!"; | |
514 | open my \$error_log_fh, '>>', "\$logdir/error.log" | |
515 | or die "Couldn't open error log '\$logdir/error.log': \$!"; | |
516 | ||
517 | \$access_log_fh->autoflush(1); | |
518 | \$error_log_fh->autoflush(1); | |
519 | ||
520 | # redirect build time warnings to error.log | |
521 | \$SIG{'__WARN__'} = sub { | |
522 | my \$msg = shift; | |
523 | # timestamp warning like in CGI::Carp::warn | |
524 | my \$stamp = CGI::Carp::stamp(); | |
525 | \$msg =~ s/^/\$stamp/gm; | |
526 | print \$error_log_fh \$msg; | |
527 | }; | |
528 | ||
529 | # write errors to error.log, access to access.log | |
530 | enable 'AccessLog', | |
531 | format => "combined", | |
532 | logger => sub { print \$access_log_fh @_; }; | |
533 | enable sub { | |
534 | my \$app = shift; | |
535 | sub { | |
536 | my \$env = shift; | |
537 | \$env->{'psgi.errors'} = \$error_log_fh; | |
538 | \$app->(\$env); | |
539 | } | |
540 | }; | |
541 | # gitweb currently doesn't work with $SIG{CHLD} set to 'IGNORE', | |
542 | # because it uses 'close $fd or die...' on piped filehandle $fh | |
543 | # (which causes the parent process to wait for child to finish). | |
544 | enable_if { \$SIG{'CHLD'} eq 'IGNORE' } sub { | |
545 | my \$app = shift; | |
546 | sub { | |
547 | my \$env = shift; | |
548 | local \$SIG{'CHLD'} = 'DEFAULT'; | |
549 | local \$SIG{'CLD'} = 'DEFAULT'; | |
550 | \$app->(\$env); | |
551 | } | |
552 | }; | |
553 | # serve static files, i.e. stylesheet, images, script | |
554 | enable 'Static', | |
555 | path => sub { m!\.(js|css|png)\$! && s!^/gitweb/!! }, | |
556 | root => "$root/", | |
557 | encoding => 'utf-8'; # encoding for 'text/plain' files | |
558 | # convert CGI application to PSGI app | |
559 | Plack::App::WrapCGI->new(script => "$root/gitweb.cgi")->to_app; | |
560 | }; | |
561 | ||
562 | # make it runnable as standalone app, | |
563 | # like it would be run via 'plackup' utility | |
20e7ab8a JN |
564 | if (caller) { |
565 | return \$app; | |
566 | } else { | |
78646987 JN |
567 | require Plack::Runner; |
568 | ||
569 | my \$runner = Plack::Runner->new(); | |
570 | \$runner->parse_options(qw(--env deployment --port $port), | |
20e7ab8a | 571 | "$local" ? qw(--host 127.0.0.1) : ()); |
78646987 JN |
572 | \$runner->run(\$app); |
573 | } | |
574 | __END__ | |
575 | EOF | |
576 | ||
577 | chmod a+x "$fqgitdir/gitweb/gitweb.psgi" | |
578 | # configuration is embedded in server script file, gitweb.psgi | |
579 | rm -f "$conf" | |
580 | } | |
581 | ||
c0cb4ed3 PKS |
582 | gitweb_conf() { |
583 | cat > "$fqgitdir/gitweb/gitweb_config.perl" <<EOF | |
584 | #!/usr/bin/perl | |
585 | our \$projectroot = "$(dirname "$fqgitdir")"; | |
586 | our \$git_temp = "$fqgitdir/gitweb/tmp"; | |
587 | our \$projects_list = \$projectroot; | |
fc5b8e01 GB |
588 | |
589 | \$feature{'remote_heads'}{'default'} = [1]; | |
c0cb4ed3 | 590 | EOF |
4af819d4 JN |
591 | } |
592 | ||
db61f060 JN |
593 | configure_httpd() { |
594 | case "$httpd" in | |
595 | *lighttpd*) | |
596 | lighttpd_conf | |
597 | ;; | |
598 | *apache2*|*httpd*) | |
599 | apache2_conf | |
600 | ;; | |
601 | webrick) | |
602 | webrick_conf | |
603 | ;; | |
604 | *mongoose*) | |
605 | mongoose_conf | |
606 | ;; | |
607 | *plackup*) | |
608 | plackup_conf | |
609 | ;; | |
610 | *) | |
611 | echo "Unknown httpd specified: $httpd" | |
612 | exit 1 | |
613 | ;; | |
614 | esac | |
615 | } | |
616 | ||
c0175f92 JN |
617 | case "$action" in |
618 | stop) | |
619 | stop_httpd | |
620 | exit 0 | |
621 | ;; | |
622 | start) | |
623 | start_httpd | |
624 | exit 0 | |
625 | ;; | |
626 | restart) | |
627 | stop_httpd | |
628 | start_httpd | |
629 | exit 0 | |
630 | ;; | |
631 | esac | |
632 | ||
c0cb4ed3 | 633 | gitweb_conf |
a51d37c1 | 634 | |
be5347b3 PKS |
635 | resolve_full_httpd |
636 | mkdir -p "$fqgitdir/gitweb/$httpd_only" | |
5ad6d387 | 637 | conf="$fqgitdir/gitweb/$httpd_only.conf" |
be5347b3 | 638 | |
db61f060 | 639 | configure_httpd |
a51d37c1 EW |
640 | |
641 | start_httpd | |
5209eda8 | 642 | url=http://127.0.0.1:$port |
2e0c2902 CC |
643 | |
644 | if test -n "$browser"; then | |
d94775e1 | 645 | httpd_is_ready && git web--browse -b "$browser" $url || echo $url |
2e0c2902 | 646 | else |
d94775e1 | 647 | httpd_is_ready && git web--browse -c "instaweb.browser" $url || echo $url |
2e0c2902 | 648 | fi |