Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for binding to specific HOST allowing it to run in a Docker container #13

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion mindinsight/conf/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
####################################
# Web default settings.
####################################
HOST = '127.0.0.1'
# Removing HOST from constants allows us to specify which IP we should bind to via arguments
#HOST = '127.0.0.1'

# Allow to support cross origin resource sharing(CORS) enable. Default is disable.
# If enable CORS, `SUPPORT_REQUEST_METHODS` should enable 'OPTIONS' method.
Expand Down
2 changes: 2 additions & 0 deletions mindinsight/conf/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
# Web default settings.
####################################
PORT = 8080
# Specify HOST here instead adding the possibility of being overriden via argument
HOST = '127.0.0.1'
URL_PATH_PREFIX = ''

####################################
Expand Down
43 changes: 42 additions & 1 deletion mindinsight/scripts/start.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,40 @@ def __call__(self, parser, namespace, values, option_string=None):

setattr(namespace, self.dest, prefix)

class HostAction(argparse.Action):
"""Host action class definition."""

LOCALHOST = '127.0.0.1'
BIND_ALL = '0.0.0.0'

def __call__(self, parser, namespace, values, option_string=None):
"""
Inherited __call__ method from argparse.Action.

Args:
parser (ArgumentParser): Passed-in argument parser.
namespace (Namespace): Namespace object to hold arguments.
values (object): Argument values with type depending on argument definition.
option_string (str): Optional string for specific argument name. Default: None.
"""
def isIPv4(s):
try:
return str(int(s)) == s and 0 <= int(s) <= 255
except:
return False
def isIPv6(s):
if len(s) > 4:
return False
try :
return int(s, 16) >= 0 and s[0] != '-'
except:
return False
host = values
validIPv4 = host.count(".") == 3 and all(isIPv4(i) for i in host.split("."))
validIPv6 = host.count(":") == 7 and all(isIPv6(i) for i in host.split(":"))
if (host != self.LOCALHOST or host != self.BIND_ALL) and not validIPv6 and not validIPv4:
parser.error(f'{option_string} should be chosen between {self.LOCALHOST}, {self.BIND_ALL} and any other valid specific IPv6 or IPv4 address.')
setattr(namespace, self.dest, host)
class Command(BaseCommand):
"""
Start mindinsight service.
Expand Down Expand Up @@ -186,7 +219,15 @@ def add_arguments(self, parser):
help="""
Custom port ranging from %s to %s. Default value is %s.
""" % (PortAction.MIN_PORT, PortAction.MAX_PORT, settings.PORT))


parser.add_argument(
'--host',
type=str,
action=HostAction,
help="""
Custom host to bind process to e.g. %s - %s or specific IP. Default value is %s.
""" % (HostAction.LOCALHOST,HostAction.BIND_ALL, settings.HOST))

parser.add_argument(
'--url-path-prefix',
type=str,
Expand Down