Mercurial Hosting > luan
annotate src/luan/host/init.luan @ 1416:db57d562c4bd
add Http.is_hosted
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Fri, 18 Oct 2019 15:06:32 -0600 |
parents | 4abf88ec66de |
children | 732b5de211fc |
rev | line source |
---|---|
1135 | 1 local Luan = require "luan:Luan.luan" |
2 local error = Luan.error | |
1238 | 3 local do_file = Luan.do_file or error() |
1135 | 4 local String = require "luan:String.luan" |
5 local gsub = String.gsub or error() | |
6 | |
7 | |
1392 | 8 local dir, domain = ... |
1135 | 9 |
10 | |
11 -- logging | |
12 | |
1392 | 13 do |
1353
8d95711f6615
replace java() with require "java"
Franklin Schmidt <fschmidt@gmail.com>
parents:
1351
diff
changeset
|
14 require "java" |
1332 | 15 local Log4j = require "java:luan.modules.logging.Log4j" |
1321
307e76ccd0d6
generalize separate logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
1315
diff
changeset
|
16 local Level = require "java:org.apache.log4j.Level" |
307e76ccd0d6
generalize separate logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
1315
diff
changeset
|
17 local EnhancedPatternLayout = require "java:org.apache.log4j.EnhancedPatternLayout" |
307e76ccd0d6
generalize separate logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
1315
diff
changeset
|
18 local RollingFileAppender = require "java:org.apache.log4j.RollingFileAppender" |
1365
6617763dfd76
fix logging and lucene backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
1353
diff
changeset
|
19 |
1332 | 20 local logger = Log4j.getRootLogger() |
1365
6617763dfd76
fix logging and lucene backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
1353
diff
changeset
|
21 Log4j.removeAllAppenders(logger) |
1321
307e76ccd0d6
generalize separate logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
1315
diff
changeset
|
22 local layout = EnhancedPatternLayout.new("%d %-5p %c - %m%n") |
1199 | 23 local log_dir = dir.."/site/private/local/logs/" |
1135 | 24 |
1199 | 25 local function add_appender(file,level) |
26 local appender = RollingFileAppender.new(layout, log_dir..file) | |
27 appender.setMaxFileSize("1MB") | |
28 logger.addAppender(appender) | |
29 if level ~= logger.getEffectiveLevel() then | |
30 appender.setThreshold(level) | |
31 end | |
32 end | |
1135 | 33 |
1199 | 34 add_appender("error.log",Level.ERROR) |
35 add_appender("warn.log",Level.WARN) | |
36 add_appender("info.log",Level.INFO) | |
1135 | 37 end |
38 | |
39 | |
1235
22228c12b6dd
fix luanhost thread logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
1199
diff
changeset
|
40 -- set vars |
22228c12b6dd
fix luanhost thread logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
1199
diff
changeset
|
41 |
22228c12b6dd
fix luanhost thread logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
1199
diff
changeset
|
42 local Io = require "luan:Io.luan" |
22228c12b6dd
fix luanhost thread logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
1199
diff
changeset
|
43 local Http = require "luan:http/Http.luan" |
22228c12b6dd
fix luanhost thread logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
1199
diff
changeset
|
44 local Hosting = require "luan:host/Hosting.luan" |
22228c12b6dd
fix luanhost thread logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
1199
diff
changeset
|
45 local Mail = require "luan:mail/Mail.luan" |
22228c12b6dd
fix luanhost thread logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
1199
diff
changeset
|
46 |
1330 | 47 Io.password = do_file(dir.."/info.luan").password or error() |
1235
22228c12b6dd
fix luanhost thread logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
1199
diff
changeset
|
48 |
22228c12b6dd
fix luanhost thread logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
1199
diff
changeset
|
49 Http.dir = "file:"..dir.."/site" |
1414 | 50 Http.is_serving = true |
1235
22228c12b6dd
fix luanhost thread logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
1199
diff
changeset
|
51 |
22228c12b6dd
fix luanhost thread logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
1199
diff
changeset
|
52 function Io.schemes.site(path,loading) |
22228c12b6dd
fix luanhost thread logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
1199
diff
changeset
|
53 return Io.uri( Http.dir..path, loading ) |
22228c12b6dd
fix luanhost thread logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
1199
diff
changeset
|
54 end |
22228c12b6dd
fix luanhost thread logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
1199
diff
changeset
|
55 |
1315 | 56 Http.domain = domain |
1416 | 57 Http.is_hosted = true |
1235
22228c12b6dd
fix luanhost thread logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
1199
diff
changeset
|
58 |
22228c12b6dd
fix luanhost thread logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
1199
diff
changeset
|
59 |
1135 | 60 -- mail - fix later |
61 | |
62 Hosting.send_mail = Mail.Sender{ | |
1312 | 63 host = "smtpcorp.com" |
64 username = "smtp@luan.ws" -- ? | |
65 password = "luanhost" | |
66 port = 2525 | |
1135 | 67 }.send |
68 | |
69 | |
1392 | 70 |
71 | |
72 -- postgres | |
73 | |
74 local Sql = require "luan:sql/Sql.luan" | |
75 local database = Sql.database or error() | |
76 local Logging = require "luan:logging/Logging.luan" | |
77 local logger = Logging.logger "init" | |
78 | |
79 local fn = Luan.load_file("file:postgres.luan") or error() | |
80 local pg = fn() | |
81 | |
82 function Hosting.postgres_spec() | |
83 if pg == nil then | |
84 return nil | |
85 end | |
86 local spec = { | |
87 class = "org.postgresql.Driver" | |
88 url = "jdbc:postgresql://localhost:5432/"..domain | |
89 user = domain | |
90 password = Io.password | |
91 } | |
92 local db = database(pg) | |
1407 | 93 local exists = db.query("select datname from pg_database where datname=?",domain).results() ~= nil; |
1394
8fe777ba5045
change postgres password
Franklin Schmidt <fschmidt@gmail.com>
parents:
1393
diff
changeset
|
94 --logger.info("exists "..exists) |
1392 | 95 if not exists then |
96 db.update( [[create user "]]..spec.user..[[" with encrypted password ']]..spec.password..[[']] ) | |
97 db.update( [[create database "]]..domain..[[" owner "]]..spec.user..[["]] ) | |
98 end | |
99 db.close() | |
100 return spec | |
101 end | |
102 | |
103 | |
104 | |
105 | |
106 | |
1238 | 107 -- callback to luanhost code |
108 do_file "file:init.luan" | |
109 | |
110 | |
1330 | 111 |
1353
8d95711f6615
replace java() with require "java"
Franklin Schmidt <fschmidt@gmail.com>
parents:
1351
diff
changeset
|
112 require "java" |
1330 | 113 local WebHandler = require "java:luan.host.WebHandler" |
114 local LuanJava = require "java:luan.Luan" | |
115 | |
116 function Hosting.no_security(password) | |
117 WebHandler.securityPassword == password or error "wrong password" | |
118 LuanJava.setSecurity(nil) | |
119 end | |
1351
21b153b4bcc4
better reset_luan for luanhost
Franklin Schmidt <fschmidt@gmail.com>
parents:
1332
diff
changeset
|
120 |
21b153b4bcc4
better reset_luan for luanhost
Franklin Schmidt <fschmidt@gmail.com>
parents:
1332
diff
changeset
|
121 function Http.reset_luan() |
21b153b4bcc4
better reset_luan for luanhost
Franklin Schmidt <fschmidt@gmail.com>
parents:
1332
diff
changeset
|
122 WebHandler.removeHandler(domain) |
21b153b4bcc4
better reset_luan for luanhost
Franklin Schmidt <fschmidt@gmail.com>
parents:
1332
diff
changeset
|
123 end |