diff mercurial/server.py @ 30518:add7bcad1d9c

server: move service factory from hgweb
author Yuya Nishihara <yuya@tcha.org>
date Sat, 15 Oct 2016 14:09:36 +0900
parents dd539e2d89aa
children a0878bc87379
line wrap: on
line diff
--- a/mercurial/server.py	Sat Oct 15 14:06:46 2016 +0900
+++ b/mercurial/server.py	Sat Oct 15 14:09:36 2016 +0900
@@ -17,6 +17,7 @@
 from . import (
     commandserver,
     error,
+    hgweb,
     util,
 )
 
@@ -118,3 +119,36 @@
         return _cmdservicemap[mode](ui, repo, opts)
     except KeyError:
         raise error.Abort(_('unknown mode %s') % mode)
+
+def createhgwebservice(ui, repo, opts):
+    # this way we can check if something was given in the command-line
+    if opts.get('port'):
+        opts['port'] = util.getport(opts.get('port'))
+
+    alluis = set([ui])
+    if repo:
+        baseui = repo.baseui
+        alluis.update([repo.baseui, repo.ui])
+    else:
+        baseui = ui
+    webconf = opts.get('web_conf') or opts.get('webdir_conf')
+    if webconf:
+        # load server settings (e.g. web.port) to "copied" ui, which allows
+        # hgwebdir to reload webconf cleanly
+        servui = ui.copy()
+        servui.readconfig(webconf, sections=['web'])
+        alluis.add(servui)
+    else:
+        servui = ui
+
+    optlist = ("name templates style address port prefix ipv6"
+               " accesslog errorlog certificate encoding")
+    for o in optlist.split():
+        val = opts.get(o, '')
+        if val in (None, ''): # should check against default options instead
+            continue
+        for u in alluis:
+            u.setconfig("web", o, val, 'serve')
+
+    app = hgweb.createapp(baseui, repo, webconf)
+    return hgweb.httpservice(servui, app, opts)