@@ -603,20 +603,40 @@ def read_file(path):
603603def read_conf ():
604604 return read_file ("/etc/apache2/sites-available/000-default.conf" )
605605
606+ import re
607+ def clean_conf (apache_conf , root_path ):
608+ apache_conf = re .sub (r'ProxyPass\s+' + re .escape (root_path ) + r'\s+.*\n' , '' , apache_conf )
609+ apache_conf = re .sub (r'ProxyPassReverse\s+' + re .escape (root_path ) + r'\s+.*\n' , '' , apache_conf )
610+ # return apache_conf
611+ return re .sub (r'[ \t]+</VirtualHost>' , '</VirtualHost>' , apache_conf )
612+
613+ def sub_at_start (apache_conf , root_path , api_target ):
614+ return re .sub (
615+ r'(<VirtualHost\b.*?>)' ,
616+ f'\\ 1\n ProxyPass { root_path } { api_target } \n ProxyPassReverse { root_path } { api_target } ' ,
617+ apache_conf ,
618+ count = 1
619+ )
606620
607- def make_apache_content ():
608- return read_file ("/etc/apache2/sites-available/000-default.conf" )
621+
622+ def sub_at_end (apache_conf , root_path , api_target ):
623+ return re .sub (r'</VirtualHost>' , f' ProxyPass { root_path } { api_target } \n ProxyPassReverse { root_path } { api_target } \n </VirtualHost>' , apache_conf )
624+
625+ def make_apache_content (apache_conf , root_path , api_target ):
626+ # Remove existing ProxyPass and ProxyPassReverse directives for the root_path
627+ apache_conf = clean_conf (apache_conf , root_path )
628+
629+ # Add new ProxyPass and ProxyPassReverse directives based on the root_path
630+ if root_path == "/" :
631+ return remove_empty_lines (sub_at_end (apache_conf , root_path , api_target ))
632+ else :
633+ # Add new directives right after the VirtualHost opening tag
634+ return remove_empty_lines (sub_at_start (apache_conf , root_path , api_target ))
609635
610636def setup_apache_load_balancer_desktop_app (port , api_base_path ):
611637 root_path = api_base_path or '/'
612- apache_conf = f"""<VirtualHost *:80>
613- ServerAdmin webmaster@localhost
614- DocumentRoot /var/www/html
615- ErrorLog ${{APACHE_LOG_DIR}}/error.log
616- CustomLog ${{APACHE_LOG_DIR}}/access.log combined
617- ProxyPass { root_path } http://127.0.0.1:{ port } { root_path }
618- ProxyPassReverse { root_path } http://127.0.0.1:{ port } { root_path }
619- </VirtualHost>"""
638+ api_target = f"http://127.0.0.1:{ port } { root_path } "
639+ apache_conf = make_apache_content (read_conf (), root_path , api_target )
620640 write_file_sudo (apache_conf , "/etc/apache2/sites-available/000-default.conf" )
621641
622642def validate_url (url ):
0 commit comments