witten
/
luminotes
Archived
1
0
Fork 0

Began a local/desktop mode, and made the server exit upon startup if it's already running.

This commit is contained in:
Dan Helfman 2008-08-15 15:00:16 -07:00
parent c37e35b042
commit 3d1919287b
5 changed files with 67 additions and 5 deletions

22
config/Desktop.py Normal file
View File

@ -0,0 +1,22 @@
import os
import cherrypy
settings = {
"global": {
"server.thread_pool": 1,
"static_filter.root": os.getcwd(),
"server.log_to_screen": False,
"luminotes.launch_browser": True,
},
"/static": {
"static_filter.on": True,
"static_filter.dir": "static",
"session_filter.on": False,
},
"/favicon.ico": {
"static_filter.on": True,
"static_filter.file": "static/images/favicon.ico",
"session_filter.on": False,
},
}

View File

@ -343,6 +343,12 @@ class Root( object ):
next_id = next_id,
)
@expose( view = Json )
def ping( self ):
return dict(
response = u"pong",
)
def _cp_on_http_error( self, status, message ):
"""
CherryPy HTTP error handler, used to display page not found and generic error pages.

View File

@ -106,7 +106,7 @@ class Valid_string( object ):
# check for max length after cleaning html, as cleaning can reduce the html's size
if self.max is not None and len( value ) > self.max:
self.message = u"must be no longer than %s characters" % self.max
self.message = u"must be no longer than %s characters. Please try removing some of the text" % self.max
raise ValueError()
return value

View File

@ -419,6 +419,11 @@ class Test_root( Test_controller ):
assert result.get( "next_id" )
def test_ping( self ):
result = self.http_get( "/ping" )
assert result.get( "response" ) == u"pong"
def test_404( self ):
result = self.http_get( "/four_oh_four" )

View File

@ -1,29 +1,56 @@
#!/usr/bin/python2.4
import sys
import socket
import urllib2 as urllib
import cherrypy
import webbrowser
from controller.Database import Database
from controller.Root import Root
from config import Common
INITIAL_SOCKET_TIMEOUT_SECONDS = 1
SOCKET_TIMEOUT_SECONDS = 60
def main( args ):
socket.setdefaulttimeout( SOCKET_TIMEOUT_SECONDS )
cherrypy.config.update( Common.settings )
if len( args ) > 0 and args[ 0 ] == "-d":
from config import Development
settings = Development.settings
elif len( args ) > 0 and args[ 0 ] == "-l":
from config import Desktop
settings = Desktop.settings
else:
from config import Production
settings = Production.settings
cherrypy.config.update( settings )
launch_browser = cherrypy.config.configMap[ u"global" ].get( u"luminotes.launch_browser" )
# check to see if the server is already running
socket.setdefaulttimeout( INITIAL_SOCKET_TIMEOUT_SECONDS )
server_url = u"http://localhost:%d/" % cherrypy.config.configMap[ u"global" ].get( u"server.socket_port" )
server_present = True
try:
urllib.urlopen( "%sping" % server_url )
except urllib.URLError:
server_present = False
if server_present is True:
print "Luminotes server is already running. aborting"
if launch_browser:
webbrowser.open_new( server_url )
sys.exit( 1 )
socket.setdefaulttimeout( SOCKET_TIMEOUT_SECONDS )
database = Database(
host = cherrypy.config.configMap[ u"global" ].get( u"luminotes.db_host" ),
ssl_mode = cherrypy.config.configMap[ u"global" ].get( u"luminotes.db_ssl_mode" ),
@ -33,9 +60,11 @@ def main( args ):
root = Root( database, cherrypy.config.configMap )
cherrypy.root = root
cherrypy.server.start()
if launch_browser is True:
cherrypy.server.start_with_callback( webbrowser.open_new, ( server_url, ) )
else:
cherrypy.server.start()
if __name__ == "__main__":
import sys
main( sys.argv[ 1: ] )