1
1
import os
2
2
import sys
3
- from typing import Any , Callable , Sequence
3
+ from typing import Sequence
4
4
5
5
from absl import app , flags
6
- from flask import Flask
7
6
8
7
import mesop .protos .ui_pb2 as pb
9
8
from mesop .cli .execute_module import execute_module , get_module_name_from_path
10
9
from mesop .exceptions import format_traceback
11
- from mesop .runtime import enable_debug_mode , runtime
12
- from mesop .server .constants import EDITOR_PACKAGE_PATH , PROD_PACKAGE_PATH
13
- from mesop .server .flags import port
14
- from mesop .server .logging import log_startup
15
- from mesop .server .server import configure_flask_app
16
- from mesop .server .static_file_serving import configure_static_file_serving
10
+ from mesop .runtime import runtime
11
+ from mesop .server .wsgi_app import create_app
17
12
18
13
FLAGS = flags .FLAGS
19
14
20
15
flags .DEFINE_bool (
21
16
"prod" , False , "set to true for prod mode; otherwise editor mode."
22
17
)
23
- flags .DEFINE_bool ("verbose" , False , "set to true for verbose logging." )
24
18
25
19
26
- class App :
27
- _flask_app : Flask
28
-
29
- def __init__ (self , flask_app : Flask ):
30
- self ._flask_app = flask_app
31
-
32
- def run (self ):
33
- log_startup (port = port ())
34
- self ._flask_app .run (host = "0.0.0.0" , port = port (), use_reloader = False )
20
+ def main (argv : Sequence [str ]):
21
+ if len (argv ) < 2 :
22
+ print (
23
+ """\u001b [31mERROR: missing command-line argument to Mesop application.\u001b [0m
35
24
36
- def __call__ (
37
- self , environ : dict [Any , Any ], start_response : Callable [..., Any ]
38
- ) -> Any :
39
- """Wrapper around Flask API so that a WGSI server can call this application."""
40
- return self ._flask_app .wsgi_app (environ , start_response ) # type: ignore
25
+ Example command:
26
+ $\u001b [35m mesop file.py\u001b [0m"""
27
+ )
28
+ sys .exit (1 )
41
29
30
+ if len (argv ) > 2 :
31
+ print (
32
+ f"""\u001b [31mERROR: Too many command-line arguments.\u001b [0m
42
33
43
- def make_path_absolute (file_path : str ):
44
- if os .path .isabs (file_path ):
45
- return file_path
46
- # Otherwise, make the relative path absolute by joining
47
- # with current working dir.
48
- absolute_path = os .path .join (os .getcwd (), file_path )
49
- return absolute_path
34
+ Actual:
35
+ $\u001b [35m mesop { " " .join (argv [1 :])} \u001b [0m
50
36
37
+ Re-run with:
38
+ $\u001b [35m mesop { argv [1 ]} \u001b [0m"""
39
+ )
40
+ sys .exit (1 )
51
41
52
- def create_app (path_arg : str ) -> App :
53
- flask_app = configure_flask_app ()
42
+ create_app (
43
+ prod_mode = FLAGS .prod , run_block = lambda : execute_main_module (argv [1 ])
44
+ ).run ()
54
45
55
- if FLAGS .prod :
56
- enable_debug_mode ()
57
46
47
+ def execute_main_module (path_arg : str ):
58
48
try :
59
49
absolute_path = make_path_absolute (path_arg )
60
50
execute_module (
@@ -69,38 +59,14 @@ def create_app(path_arg: str) -> App:
69
59
# Always raise an error to make it easy to debug
70
60
raise e
71
61
72
- configure_static_file_serving (
73
- flask_app ,
74
- static_file_runfiles_base = PROD_PACKAGE_PATH
75
- if FLAGS .prod
76
- else EDITOR_PACKAGE_PATH ,
77
- )
78
-
79
- return App (flask_app = flask_app )
80
-
81
62
82
- def main (argv : Sequence [str ]):
83
- if len (argv ) < 2 :
84
- print (
85
- """\u001b [31mERROR: missing command-line argument to Mesop application.\u001b [0m
86
-
87
- Example command:
88
- $\u001b [35m mesop file.py\u001b [0m"""
89
- )
90
- sys .exit (1 )
91
-
92
- if len (argv ) > 2 :
93
- print (
94
- f"""\u001b [31mERROR: Too many command-line arguments.\u001b [0m
95
-
96
- Actual:
97
- $\u001b [35m mesop { " " .join (argv [1 :])} \u001b [0m
98
-
99
- Re-run with:
100
- $\u001b [35m mesop { argv [1 ]} \u001b [0m"""
101
- )
102
- sys .exit (1 )
103
- create_app (argv [1 ]).run ()
63
+ def make_path_absolute (file_path : str ):
64
+ if os .path .isabs (file_path ):
65
+ return file_path
66
+ # Otherwise, make the relative path absolute by joining
67
+ # with current working dir.
68
+ absolute_path = os .path .join (os .getcwd (), file_path )
69
+ return absolute_path
104
70
105
71
106
72
def run_main ():
0 commit comments