David hace 10 años
padre
commit
0d5fb818c6
Se han modificado 1 ficheros con 44 adiciones y 9 borrados
  1. 44 9
      web/app.py

+ 44 - 9
web/app.py

@@ -5,6 +5,22 @@ import json
 
 install(SQLitePlugin(dbfile='test.db'))
 
+def ip_from_name(db, machine):
+    cur = db.execute('SELECT ip FROM machines where name=?', (machine,))
+    row = cur.fetchone()
+    if row is None:
+        return None
+    return row[0]
+
+def next_port(db, ip):
+    BASE_PORT=8000
+    cur = db.execute('SELECT port FROM domains where ip=? order by port desc limit 1', (ip,))
+    row = cur.fetchone()
+    if row is None:
+        return BASE_PORT
+
+    return row[0]+1
+
 def turn_me_to_json(cursor,res):
     out = []
     for row in res:
@@ -27,22 +43,41 @@ def put_machines(db, group, name, ip):
     if c.rowcount == 0:
         c = db.execute('INSERT INTO machines (mgroup,name,ip) values(?,?,?)', (group,name,ip))
 
-@route('/domain/', method='GET')
+@route('/domains/', method='GET')
 def get_domains(db):
-    c = db.execute('SELECT ip,port,domain,subdomain FROM domains')
+    c = db.execute('SELECT ip,port,domain,subdomain,user FROM domains')
     out = turn_me_to_json(c,c.fetchall())
     response.content_type = "application/json"
     return json.dumps(out)
 
-@route('/domain/<ip>', method='PUT')
-def put_domain(db, ip="Mystery Domain" ):
-    if request.json is None or "domain" not in request.json or "subdomain" not in request.json or "port" not in request.json:
+@route('/domains/<machine>', method='PUT')
+def put_domain(db, machine ):
+    if request.json is None or \
+        "domain" not in request.json or \
+        "user" not in request.json or \
+        "subdomain" not in request.json:
         response.status=400
         return
-    c = db.execute('UPDATE domains set domain=? and subdomain=? and port=? where ip=?', 
-            (request.json["domain"],request.json["subdomain"],request.json["port"],ip))
+    ip = ip_from_name(db,machine)
+    if ip is None:
+        response.status=400
+        return
+    port = next_port(db,ip)
+    domain = request.json["domain"]
+    user = request.json["user"]
+    sub = request.json["subdomain"]
+
+    print(domain)
+    #d2om 8001 martita5 208.68.38.138 subuno 
+    c = db.execute('UPDATE domains set domain = ?, port = ?, user = ? where ip=? and subdomain=?', 
+            (domain,port,user,ip,sub))
+
     if c.rowcount == 0:
-        c = db.execute('INSERT INTO user_machines (ip,name) values(?,?)', (request.json["ip"],name))
-    return 
+        print("inserting!")
+        c = db.execute('INSERT INTO domains(domain,subdomain,user,port,ip) VALUES (?,?,?,?,?)', 
+            (domain,sub,user,port,ip))
+    else:
+        print("updating! %d" % c.rowcount)
+        print(domain,port,user,ip,sub)
 
 run(host='localhost', port=8080)