@@ -74,62 +74,63 @@ async def http_not_found(request: web.Request):
74
74
return web .HTTPNotFound ()
75
75
76
76
77
- app = web .Application (middlewares = [server_version_middleware ])
78
- cors = setup (
79
- app ,
80
- defaults = {
81
- "*" : ResourceOptions (
82
- allow_credentials = True ,
83
- expose_headers = "*" ,
84
- allow_headers = "*" ,
85
- )
86
- },
87
- )
77
+ def setup_webapp ():
78
+ app = web .Application (middlewares = [server_version_middleware ])
79
+ cors = setup (
80
+ app ,
81
+ defaults = {
82
+ "*" : ResourceOptions (
83
+ allow_credentials = True ,
84
+ expose_headers = "*" ,
85
+ allow_headers = "*" ,
86
+ )
87
+ },
88
+ )
88
89
89
- # Routes that need CORS enabled
90
- cors_routes = [
91
- # /about APIs return information about the VM Orchestrator
92
- web .get ("/about/login" , about_login ),
93
- web .get ("/about/executions/list" , list_executions ),
94
- web .get ("/about/executions/details" , about_executions ),
95
- web .get ("/about/executions/records" , about_execution_records ),
96
- web .get ("/about/usage/system" , about_system_usage ),
97
- web .get ("/about/config" , about_config ),
98
- # /control APIs are used to control the VMs and access their logs
99
- web .post ("/control/allocation/notify" , notify_allocation ),
100
- web .get ("/control/machine/{ref}/logs" , stream_logs ),
101
- web .post ("/control/machine/{ref}/expire" , operate_expire ),
102
- web .post ("/control/machine/{ref}/stop" , operate_stop ),
103
- web .post ("/control/machine/{ref}/erase" , operate_erase ),
104
- web .post ("/control/machine/{ref}/reboot" , operate_reboot ),
105
- # /status APIs are used to check that the VM Orchestrator is running properly
106
- web .get ("/status/check/fastapi" , status_check_fastapi ),
107
- web .get ("/status/check/fastapi/legacy" , status_check_fastapi_legacy ),
108
- web .get ("/status/check/host" , status_check_host ),
109
- web .get ("/status/check/version" , status_check_version ),
110
- web .get ("/status/check/ipv6" , status_check_ipv6 ),
111
- web .get ("/status/config" , status_public_config ),
112
- ]
113
- routes = app .add_routes (cors_routes )
114
- for route in routes :
115
- cors .add (route )
116
-
117
-
118
- # Routes that don't need CORS enabled
119
- other_routes = [
120
- # /control APIs are used to control the VMs and access their logs
121
- web . post ( "/control/allocations" , update_allocations ),
122
- # Raise an HTTP Error 404 if attempting to access an unknown URL within these paths.
123
- web .get ("/about /{suffix:.*}" , http_not_found ),
124
- web .get ("/control /{suffix:.*}" , http_not_found ),
125
- web . get ( "/status/{suffix:.*}" , http_not_found ),
126
- # /static is used to serve static files
127
- web . static ( "/static" , Path ( __file__ ). parent / "views/static" ),
128
- # /vm is used to launch VMs on-demand
129
- web .route ("*" , "/vm/{ref}{ suffix:.*}" , run_code_from_path ),
130
- web . route ( "*" , "/{suffix:.*}" , run_code_from_hostname ),
131
- ]
132
- app . add_routes ( other_routes )
90
+ # Routes that need CORS enabled
91
+ cors_routes = [
92
+ # /about APIs return information about the VM Orchestrator
93
+ web .get ("/about/login" , about_login ),
94
+ web .get ("/about/executions/list" , list_executions ),
95
+ web .get ("/about/executions/details" , about_executions ),
96
+ web .get ("/about/executions/records" , about_execution_records ),
97
+ web .get ("/about/usage/system" , about_system_usage ),
98
+ web .get ("/about/config" , about_config ),
99
+ # /control APIs are used to control the VMs and access their logs
100
+ web .post ("/control/allocation/notify" , notify_allocation ),
101
+ web .get ("/control/machine/{ref}/logs" , stream_logs ),
102
+ web .post ("/control/machine/{ref}/expire" , operate_expire ),
103
+ web .post ("/control/machine/{ref}/stop" , operate_stop ),
104
+ web .post ("/control/machine/{ref}/erase" , operate_erase ),
105
+ web .post ("/control/machine/{ref}/reboot" , operate_reboot ),
106
+ # /status APIs are used to check that the VM Orchestrator is running properly
107
+ web .get ("/status/check/fastapi" , status_check_fastapi ),
108
+ web .get ("/status/check/fastapi/legacy" , status_check_fastapi_legacy ),
109
+ web .get ("/status/check/host" , status_check_host ),
110
+ web .get ("/status/check/version" , status_check_version ),
111
+ web .get ("/status/check/ipv6" , status_check_ipv6 ),
112
+ web .get ("/status/config" , status_public_config ),
113
+ ]
114
+ routes = app .add_routes (cors_routes )
115
+ for route in routes :
116
+ cors .add (route )
117
+
118
+ # Routes that don't need CORS enabled
119
+ other_routes = [
120
+ # /control APIs are used to control the VMs and access their logs
121
+ web . post ( "/ control/allocations" , update_allocations ),
122
+ # Raise an HTTP Error 404 if attempting to access an unknown URL within these paths.
123
+ web . get ( "/about/{suffix:.*}" , http_not_found ),
124
+ web .get ("/control /{suffix:.*}" , http_not_found ),
125
+ web .get ("/status /{suffix:.*}" , http_not_found ),
126
+ # /static is used to serve static files
127
+ web . static ( "/static" , Path ( __file__ ). parent / "views/static" ),
128
+ # /vm is used to launch VMs on-demand
129
+ web . route ( "*" , "/vm/{ref}{suffix:.*}" , run_code_from_path ),
130
+ web .route ("*" , "/{ suffix:.*}" , run_code_from_hostname ),
131
+ ]
132
+ app . add_routes ( other_routes )
133
+ return app
133
134
134
135
135
136
async def stop_all_vms (app : web .Application ):
@@ -153,6 +154,7 @@ def run():
153
154
154
155
# Require a random token to access /about APIs
155
156
secret_token = token_urlsafe (nbytes = 32 )
157
+ app = setup_webapp ()
156
158
# Store app singletons. Note that app["pubsub"] will also be created.
157
159
app ["secret_token" ] = secret_token
158
160
app ["vm_pool" ] = pool
0 commit comments