Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

multidatabase ready: master-slave setup

  • Loading branch information...
commit a20ef099d1148aa60961b6c3c258b2ed51ee3eae 1 parent 922ac8f
Andrei Savu authored
Showing with 41 additions and 20 deletions.
  1. +21 −0 db.py
  2. +9 −15 index.py
  3. +11 −5 settings.py
21 db.py
View
@@ -0,0 +1,21 @@
+
+import web, settings
+
+#
+# Master read/write database
+#
+master = web.database(dbn = settings.DB_MASTER_TYPE,
+ host = settings.DB_MASTER_HOST,
+ db = settings.DB_MASTER_NAME,
+ user = settings.DB_MASTER_USER,
+ pw = settings.DB_MASTER_PASSW)
+
+#
+# Slave databases - multiple servers behind HA proxy
+#
+slave = web.database(dbn = settings.DB_SLAVE_TYPE,
+ host = settings.DB_SLAVE_HOST,
+ db = settings.DB_SLAVE_NAME,
+ user = settings.DB_SLAVE_USER,
+ pw = settings.DB_SLAVE_PASSW)
+
24 index.py
View
@@ -1,6 +1,6 @@
import web, cgi, settings
-import storage, search
+import storage, search, db
import simplejson
from lib import is_mp3, get_mp3_info
@@ -19,17 +19,11 @@
app = web.application(urls, globals())
-db = web.database(dbn = settings.DB_TYPE,
- host = settings.DB_HOST,
- db = settings.DB_NAME,
- user = settings.DB_USER,
- pw = settings.DB_PASSW)
-
render = web.template.render(settings.TEMPLATE_FOLDER, base='base')
class do_index:
def GET(self):
- files = db.select('ms_files', order='date desc', limit=5)
+ files = db.slave.select('ms_files', order='date desc', limit=5)
return render.index(files)
class do_about:
@@ -39,7 +33,7 @@ def GET(self):
class do_search:
def GET(self):
input = web.input(q='')
- files = search.get(input.q, db)
+ files = search.get(input.q, db.slave)
return render.search(files, query=input.q, title='Search')
class do_upload_error:
@@ -62,7 +56,7 @@ def POST(self):
info['FILENAME'] = input.file.filename
except:
raise web.seeother('/upload/error')
- id = storage.save(info, input.file.file, db)
+ id = storage.save(info, input.file.file, db.master)
search.update(id, info)
raise web.seeother('/')
@@ -73,10 +67,10 @@ def GET(self, id):
class do_view:
def GET(self, id):
- f = search.get_by_id(id, db)
+ f = search.get_by_id(id, db.slave)
if f.title and f.artist:
title = "%s : %s" % (f.artist, f.title)
- related = search.get(f.artist, db)
+ related = search.get(f.artist, db.slave)
else:
title = f.filename
import re
@@ -86,7 +80,7 @@ def GET(self, id):
q = "%s %s" % (q[0], q[1])
else:
q = ' '.join(q)
- related = search.get(q, db)
+ related = search.get(q, db.slave)
related = [x for x in related if x.id != int(id)]
return render.view(f, related, title)
@@ -100,7 +94,7 @@ def GET(self, format):
if not format:
format = '.json'
input = web.input(q='')
- res = search.get(input.q, db)
+ res = search.get(input.q, db.slave)
files = []
for f in res:
del(f['date'])
@@ -122,7 +116,7 @@ def POST(self, format):
info['FILENAME'] = input.file.filename
except:
return simplejson.dumps({'code':2, 'error':'Error getting file information'})
- id = storage.save(info, input.file.file, db)
+ id = storage.save(info, input.file.file, db.master)
search.update(id, info)
return simplejson.dumps({'code':0})
16 settings.py
View
@@ -2,9 +2,15 @@
TEMPLATE_FOLDER = 'templates/'
MAX_UP_FILE_SIZE = 10 * 1024 * 1024 # 10 MB
-DB_TYPE = 'mysql'
-DB_HOST = 'localhost'
-DB_NAME = 'music'
-DB_USER = 'root'
-DB_PASSW = ''
+DB_MASTER_TYPE = 'mysql'
+DB_MASTER_HOST = 'localhost'
+DB_MASTER_NAME = 'music'
+DB_MASTER_USER = 'root'
+DB_MASTER_PASSW = ''
+
+DB_SLAVE_TYPE = 'mysql'
+DB_SLAVE_HOST = 'localhost'
+DB_SLAVE_NAME = 'music'
+DB_SLAVE_USER = 'root'
+DB_SLAVE_PASSW = ''
Please sign in to comment.
Something went wrong with that request. Please try again.