diff src/luan/host/init.luan @ 1392:002152af497a

hosted postgres
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 06 Sep 2019 00:19:47 -0600
parents 6617763dfd76
children cc0dbca576dc
line wrap: on
line diff
--- a/src/luan/host/init.luan	Thu Sep 05 01:29:57 2019 -0600
+++ b/src/luan/host/init.luan	Fri Sep 06 00:19:47 2019 -0600
@@ -5,12 +5,12 @@
 local gsub = String.gsub or error()
 
 
-local dir, domain, logging = ...
+local dir, domain = ...
 
 
 -- logging
 
-if logging then
+do
 	require "java"
 	local Log4j = require "java:luan.modules.logging.Log4j"
 	local Level = require "java:org.apache.log4j.Level"
@@ -37,7 +37,6 @@
 end
 
 
-
 -- set vars
 
 local Io = require "luan:Io.luan"
@@ -66,6 +65,44 @@
 }.send
 
 
+
+
+-- postgres
+
+local Sql = require "luan:sql/Sql.luan"
+local database = Sql.database or error()
+local Logging = require "luan:logging/Logging.luan"
+local logger = Logging.logger "init"
+
+local fn = Luan.load_file("file:postgres.luan") or error()
+local pg = fn()
+
+function Hosting.postgres_spec()
+	logger.info("pg="..pg.." domain="..domain)
+	if pg == nil then
+		return nil
+	end
+	local spec = {
+		class = "org.postgresql.Driver"
+		url = "jdbc:postgresql://localhost:5432/"..domain
+		user = domain
+		password = Io.password
+	}
+	local db = database(pg)
+	local exists = db.query("select datname from pg_database where datname=?",domain)() ~= nil;
+	logger.info("exists "..exists)
+	if not exists then
+		db.update( [[create user "]]..spec.user..[[" with encrypted password ']]..spec.password..[[']] )
+		db.update( [[create database "]]..domain..[[" owner "]]..spec.user..[["]] )
+	end
+	db.close()
+	return spec
+end
+
+
+
+
+
 -- callback to luanhost code
 do_file "file:init.luan"