changeset 1396:a5f61890ad84

add check_postgres_password
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 10 Sep 2019 13:41:22 -0600
parents 9dfff82dfc59
children 0dc9837c16be
files examples/blog/src/lib/Db.luan src/luan/host/Util.luan src/luan/host/main.luan
diffstat 3 files changed, 28 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
diff -r 9dfff82dfc59 -r a5f61890ad84 examples/blog/src/lib/Db.luan
--- a/examples/blog/src/lib/Db.luan	Mon Sep 09 01:22:23 2019 -0600
+++ b/examples/blog/src/lib/Db.luan	Tue Sep 10 13:41:22 2019 -0600
@@ -1,3 +1,6 @@
+local Luan = require "luan:Luan.luan"
+local error = Luan.error
+local stringify = Luan.stringify or error()
 local Lucene = require "luan:lucene/Lucene.luan"
 local Io = require "luan:Io.luan"
 local Hosting = require "luan:host/Hosting.luan"
@@ -8,6 +11,7 @@
 local Db = {}
 
 local postgres_spec = Hosting.postgres_spec and Hosting.postgres_spec()
+--logger.info(stringify(postgres_spec))
 
 local function completer(doc)
 	return doc
diff -r 9dfff82dfc59 -r a5f61890ad84 src/luan/host/Util.luan
--- a/src/luan/host/Util.luan	Mon Sep 09 01:22:23 2019 -0600
+++ b/src/luan/host/Util.luan	Tue Sep 10 13:41:22 2019 -0600
@@ -54,14 +54,16 @@
 	file.delete()
 	file.write_text("return "..stringify{password=password}.."\n")
 	digest_authentication(dir,password)
+end
 
-	-- postgres
-	local fn = Luan.load_file("file:postgres.luan") or error()
-	local pg = fn()
-	if pg == nil then
+local fn = Luan.load_file("file:postgres.luan") or error()
+local pg_admin = fn()
+
+function Util.set_postgres_password(domain,password)
+	if pg_admin == nil then
 		return
 	end
-	local db = database(pg)
+	local db = database(pg_admin)
 	local exists = db.query("select rolname from pg_roles where rolname=?",domain)() ~= nil;
 	--logger.info("exists "..exists)
 	if exists then
@@ -70,4 +72,17 @@
 	db.close()
 end
 
+function Util.check_postgres_password(domain,password)
+	if pg_admin == nil then
+		return
+	end
+	local db = database{
+		class = "org.postgresql.Driver"
+		url = "jdbc:postgresql://localhost:5432/"..domain
+		user = domain
+		password = password
+	}
+	db.close()
+end
+
 return Util
diff -r 9dfff82dfc59 -r a5f61890ad84 src/luan/host/main.luan
--- a/src/luan/host/main.luan	Mon Sep 09 01:22:23 2019 -0600
+++ b/src/luan/host/main.luan	Tue Sep 10 13:41:22 2019 -0600
@@ -18,6 +18,8 @@
 local Util = require "classpath:luan/host/Util.luan"
 local read_password = Util.read_password or error()
 local set_password = Util.set_password or error()
+local set_postgres_password = Util.set_postgres_password or error()
+local check_postgres_password = Util.check_postgres_password or error()
 
 
 local sites_dir = Io.schemes.file(Hosting.sites_dir)
@@ -103,6 +105,7 @@
 	domain = lower(domain)
 	local dir = sites_dir.child(domain)
 	dir.exists() and error "already exists"
+	check_postgres_password(domain,password)
 	dir.mkdir()
 	set_password(domain,password)
 	dir = dir.child("site")
@@ -180,6 +183,7 @@
 	local site_dir = get_dir(domain,old_password)
 	site_dir or error "domain not found"
 	set_password(domain,new_password)
+	set_postgres_password(domain,new_password)
 	WebHandler.removeHandler(domain)
 	WebHandler.loadHandler(domain)
 end