annotate host/Config.luan @ 1628:520707a70379

add host
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 07 Dec 2021 23:29:58 -0700
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1628
520707a70379 add host
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1 local Luan = require "luan:Luan.luan"
520707a70379 add host
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
2 local error = Luan.error
520707a70379 add host
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
3 local ipairs = Luan.ipairs or error()
520707a70379 add host
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
4 local Io = require "luan:Io.luan"
520707a70379 add host
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
5 local uri = Io.uri or error()
520707a70379 add host
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
6 local Package = require "luan:Package.luan"
520707a70379 add host
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
7 local load = Package.load or error()
520707a70379 add host
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
8 local Logging = require "luan:logging/Logging.luan"
520707a70379 add host
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
9 local logger = Logging.logger "Config"
520707a70379 add host
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
10
520707a70379 add host
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
11
520707a70379 add host
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
12 uri("file:local").mkdir()
520707a70379 add host
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
13 local password_file = uri("file:local/password.txt")
520707a70379 add host
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
14 local password = password_file.exists() and password_file.read_text() or "password"
520707a70379 add host
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
15 local site_config = nil
520707a70379 add host
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
16 local site_name
520707a70379 add host
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
17 for _, site_dir in ipairs( uri("file:sites").children() or {} ) do
520707a70379 add host
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
18 local name = site_dir.name()
520707a70379 add host
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
19 local config = load("file:sites/"..name.."/site/private/host/Config.luan")
520707a70379 add host
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
20 if config == false then
520707a70379 add host
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
21 continue
520707a70379 add host
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22 end
520707a70379 add host
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23 if config.password ~= password and config.old_password ~= password then
520707a70379 add host
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
24 logger.warn("wrong password for "..name)
520707a70379 add host
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
25 continue
520707a70379 add host
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
26 end
520707a70379 add host
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
27 if site_config == nil then
520707a70379 add host
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
28 site_config = config
520707a70379 add host
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
29 site_name = name
520707a70379 add host
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
30 else
520707a70379 add host
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
31 logger.error("conflicting Config in "..name.." and "..site_name)
520707a70379 add host
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
32 end
520707a70379 add host
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
33 end
520707a70379 add host
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
34 if site_config ~= nil then
520707a70379 add host
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
35 logger.info("using Config from "..site_name)
520707a70379 add host
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
36 local site_password = site_config.password or error()
520707a70379 add host
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
37 if site_password ~= password then
520707a70379 add host
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
38 password_file.write_text(password)
520707a70379 add host
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
39 end
520707a70379 add host
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
40 return site_config
520707a70379 add host
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
41 end
520707a70379 add host
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
42 if password ~= "password" then
520707a70379 add host
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
43 password_file.write_text("password")
520707a70379 add host
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
44 end
520707a70379 add host
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
45
520707a70379 add host
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
46
520707a70379 add host
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
47 local Config = {}
520707a70379 add host
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
48
520707a70379 add host
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
49 Config.password = "password"
520707a70379 add host
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
50
520707a70379 add host
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
51 --[[ an example
520707a70379 add host
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
52 Config.postgres = {
520707a70379 add host
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
53 class = "org.postgresql.Driver"
520707a70379 add host
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
54 url = "jdbc:postgresql://localhost:5432/postgres"
520707a70379 add host
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
55 user = "someone"
520707a70379 add host
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
56 password = "password"
520707a70379 add host
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
57 }
520707a70379 add host
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
58 ]]
520707a70379 add host
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
59
520707a70379 add host
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
60 return Config