Last active
July 29, 2020 06:28
-
-
Save ingoogni/abbe80044b655010139a40d7afbd3413 to your computer and use it in GitHub Desktop.
CherryPy config & structure
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
``` | |
from .Thing import Thing | |
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Goal: create a cherrypy app in such a way it can be "dropped" on the server as is, only change an __init__.py for importing. Simmilar with tools & plug ins. | |
Server | |
| | |
+-- app | |
| | | |
| +-- __init.py__ | |
| | | |
| +-- Thing | |
| | | | |
| | +--- static | |
| | | | | |
| | | +-- screen.css | |
| | | +-- thing.js | |
| | | | |
| | +-- __init__.py (empty) | |
| | +-- Thing.py | |
| | +-- index.html | |
| | | |
| +-- OtherApp | |
| | |
| | |
+--- server.py | |
+--- server.conf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta name="viewport" content="width=device-width,height=device-height,initial-scale=1"> | |
<link rel="stylesheet" type="text/css" href="/thing/static/screen.css" media="screen"> | |
<script src="/thing/static/order.js" type="text/javascript"></script> | |
<title>CherryPy</title> | |
</head> | |
<body> | |
<p>...World...</p> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
``` | |
import os | |
import cherrypy | |
server_root = os.path.dirname(os.path.abspath(__file__)) | |
global_config = os.path.join(server_root, 'server.conf') | |
cherrypy.config.update(global_config) | |
import app | |
cherrypy.engine.signals.subscribe() | |
cherrypy.engine.start() | |
cherrypy.engine.block() | |
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
``` | |
import os | |
import cherrypy | |
app_dir = os.path.dirname(__file__) | |
@cherrypy.expose | |
class Thing(object): | |
def __init__(self): | |
self.app_name = 'thing' # | |
self.app_dir = app_dir # instead of here these could be stuck in global or app config | |
@cherrypy.tools.accept(media='text/plain') | |
def GET(self, *args, **kwargs): | |
os.chdir(self.app_dir) | |
return open('index.html') | |
thing_conf = { | |
'/': { | |
'request.dispatch' : cherrypy.dispatch.MethodDispatcher(), | |
'tools.staticdir.root' : app_dir | |
}, | |
'/static': { | |
'tools.staticdir.dir' : "static", | |
'tools.staticdir.on' : True, | |
'tools.staticdir.debug' : True | |
} | |
} | |
cherrypy.tree.mount(Thing(), '/thing', thing_conf) | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment