Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
C
core
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to JiHu GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Chenhao Ma
core
Commits
bd55c559
Commit
bd55c559
authored
8 years ago
by
Nathaniel Kofalt
Browse files
Options
Downloads
Patches
Plain Diff
Separate out application start from routing table
parent
4c59e389
No related branches found
No related tags found
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
api/api.py
+2
-78
2 additions, 78 deletions
api/api.py
api/base.py
+0
-1
0 additions, 1 deletion
api/base.py
api/start.py
+79
-0
79 additions, 0 deletions
api/start.py
bin/api.wsgi
+2
-2
2 additions, 2 deletions
bin/api.wsgi
with
83 additions
and
81 deletions
api/api.py
+
2
−
78
View file @
bd55c559
import
atexit
import
json
import
os
import
sys
import
traceback
import
webapp2
import
webapp2_extras.routes
from
.
import
encoder
from
.
import
root
from
.
import
util
from
.
import
config
from
.request
import
SciTranRequest
from
.
import
root
from
.centralclient
import
CentralClient
from
.download
import
Download
from
.handlers.collectionshandler
import
CollectionsHandler
...
...
@@ -29,13 +18,9 @@ from .handlers.userhandler import UserHandler
from
.jobs.handlers
import
JobsHandler
,
JobHandler
,
GearsHandler
,
GearHandler
,
RulesHandler
from
.upload
import
Upload
from
.
import
config
log
=
config
.
log
try
:
import
uwsgi
except
ImportError
:
uwsgi
=
None
routing_regexes
=
{
# Group ID: 2-32 characters of form [0-9a-z.@_-]. Start and ends with alphanum.
...
...
@@ -268,64 +253,3 @@ endpoints = [
]),
]
def
dispatcher
(
router
,
request
,
response
):
try
:
if
uwsgi
is
not
None
:
uwsgi
.
set_logvar
(
'
request_id
'
,
request
.
id
)
except
:
# pylint: disable=bare-except
request
.
logger
.
error
(
"
Error setting request_id log var
"
,
exc_info
=
True
)
try
:
rv
=
router
.
default_dispatcher
(
request
,
response
)
if
rv
is
not
None
:
response
.
write
(
json
.
dumps
(
rv
,
default
=
encoder
.
custom_json_serializer
))
response
.
headers
[
'
Content-Type
'
]
=
'
application/json; charset=utf-8
'
except
webapp2
.
HTTPException
as
e
:
util
.
send_json_http_exception
(
response
,
str
(
e
),
e
.
code
)
except
Exception
as
e
:
# pylint: disable=broad-except
request
.
logger
.
error
(
"
Error dispatching request
"
,
exc_info
=
True
)
if
config
.
get_item
(
'
core
'
,
'
debug
'
):
message
=
traceback
.
format_exc
()
else
:
message
=
'
Internal Server Error
'
util
.
send_json_http_exception
(
response
,
message
,
500
)
def
app_factory
(
*
_
,
**
__
):
# pylint: disable=protected-access,unused-argument
# don't use config.get_item() as we don't want to require the database at startup
application
=
webapp2
.
WSGIApplication
(
endpoints
,
debug
=
config
.
__config
[
'
core
'
][
'
debug
'
])
application
.
router
.
set_dispatcher
(
dispatcher
)
application
.
request_class
=
SciTranRequest
if
os
.
environ
.
get
(
"
SCITRAN_RUNTIME_COVERAGE
"
)
==
"
true
"
:
start_coverage
()
# configure new relic
if
config
.
__config
[
'
core
'
][
'
newrelic
'
]:
try
:
import
newrelic.agent
,
newrelic
.
api
.
exceptions
newrelic
.
agent
.
initialize
(
config
.
__config
[
'
core
'
][
'
newrelic
'
])
application
=
newrelic
.
agent
.
WSGIApplicationWrapper
(
application
)
log
.
info
(
'
New Relic detected and loaded. Monitoring enabled.
'
)
except
ImportError
:
log
.
critical
(
'
New Relic libraries not found.
'
)
sys
.
exit
(
1
)
except
newrelic
.
api
.
exceptions
.
ConfigurationError
:
log
.
critical
(
'
New Relic detected, but configuration invalid.
'
)
sys
.
exit
(
1
)
return
application
# Functions to enable code coverage when API is started for testing
def
start_coverage
():
import
coverage
config
.
log
.
info
(
"
Enabling code coverage
"
)
cov
=
coverage
.
coverage
(
source
=
[
"
api
"
],
data_suffix
=
"
integration-tests
"
)
cov
.
start
()
atexit
.
register
(
save_coverage
,
cov
)
def
save_coverage
(
cov
):
config
.
log
.
info
(
"
Saving coverage
"
)
cov
.
stop
()
cov
.
save
()
This diff is collapsed.
Click to expand it.
api/base.py
+
0
−
1
View file @
bd55c559
...
...
@@ -15,7 +15,6 @@ from .types import Origin
from
.
import
validators
from
.dao
import
APIConsistencyException
,
APIConflictException
,
APINotFoundException
,
APIPermissionException
class
RequestHandler
(
webapp2
.
RequestHandler
):
json_schema
=
None
...
...
This diff is collapsed.
Click to expand it.
api/start.py
0 → 100644
+
79
−
0
View file @
bd55c559
import
atexit
import
json
import
os
import
sys
import
traceback
import
webapp2
from
.api
import
endpoints
from
.
import
config
from
.
import
encoder
from
.
import
util
from
.request
import
SciTranRequest
try
:
import
uwsgi
except
ImportError
:
uwsgi
=
None
log
=
config
.
log
def
dispatcher
(
router
,
request
,
response
):
try
:
if
uwsgi
is
not
None
:
uwsgi
.
set_logvar
(
'
request_id
'
,
request
.
id
)
except
:
# pylint: disable=bare-except
request
.
logger
.
error
(
"
Error setting request_id log var
"
,
exc_info
=
True
)
try
:
rv
=
router
.
default_dispatcher
(
request
,
response
)
if
rv
is
not
None
:
response
.
write
(
json
.
dumps
(
rv
,
default
=
encoder
.
custom_json_serializer
))
response
.
headers
[
'
Content-Type
'
]
=
'
application/json; charset=utf-8
'
except
webapp2
.
HTTPException
as
e
:
util
.
send_json_http_exception
(
response
,
str
(
e
),
e
.
code
)
except
Exception
as
e
:
# pylint: disable=broad-except
request
.
logger
.
error
(
"
Error dispatching request
"
,
exc_info
=
True
)
if
config
.
get_item
(
'
core
'
,
'
debug
'
):
message
=
traceback
.
format_exc
()
else
:
message
=
'
Internal Server Error
'
util
.
send_json_http_exception
(
response
,
message
,
500
)
def
app_factory
(
*
_
,
**
__
):
# pylint: disable=protected-access,unused-argument
# don't use config.get_item() as we don't want to require the database at startup
application
=
webapp2
.
WSGIApplication
(
endpoints
,
debug
=
config
.
__config
[
'
core
'
][
'
debug
'
])
application
.
router
.
set_dispatcher
(
dispatcher
)
application
.
request_class
=
SciTranRequest
if
os
.
environ
.
get
(
"
SCITRAN_RUNTIME_COVERAGE
"
)
==
"
true
"
:
start_coverage
()
# configure new relic
if
config
.
__config
[
'
core
'
][
'
newrelic
'
]:
try
:
import
newrelic.agent
,
newrelic
.
api
.
exceptions
newrelic
.
agent
.
initialize
(
config
.
__config
[
'
core
'
][
'
newrelic
'
])
application
=
newrelic
.
agent
.
WSGIApplicationWrapper
(
application
)
log
.
info
(
'
New Relic detected and loaded. Monitoring enabled.
'
)
except
ImportError
:
log
.
critical
(
'
New Relic libraries not found.
'
)
sys
.
exit
(
1
)
except
newrelic
.
api
.
exceptions
.
ConfigurationError
:
log
.
critical
(
'
New Relic detected, but configuration invalid.
'
)
sys
.
exit
(
1
)
return
application
# Functions to enable code coverage when API is started for testing
def
start_coverage
():
import
coverage
config
.
log
.
info
(
"
Enabling code coverage
"
)
cov
=
coverage
.
coverage
(
source
=
[
"
api
"
],
data_suffix
=
"
integration-tests
"
)
cov
.
start
()
atexit
.
register
(
save_coverage
,
cov
)
def
save_coverage
(
cov
):
config
.
log
.
info
(
"
Saving coverage
"
)
cov
.
stop
()
cov
.
save
()
This diff is collapsed.
Click to expand it.
bin/api.wsgi
+
2
−
2
View file @
bd55c559
# vim: filetype=python
from
api
import
api
from
api
import
start
application
=
api
.
app_factory
()
application
=
start
.
app_factory
()
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment