annotate src/lib/Shared.luan @ 4:028e74c8889d

add all_users
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 12 May 2022 16:54:11 -0600
parents dfc36e7ed22c
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
1 local Luan = require "luan:Luan.luan"
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
2 local error = Luan.error
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
3 local ipairs = Luan.ipairs or error()
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
4 local pairs = Luan.pairs or error()
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
5 local type = Luan.type or error()
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
6 local set_metatable = Luan.set_metatable or error()
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
7 local stringify = Luan.stringify or error()
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
8 local parse = Luan.parse or error()
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
9 local range = Luan.range or error()
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
10 local String = require "luan:String.luan"
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
11 local split = String.split or error()
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
12 local trim = String.trim or error()
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
13 local sub_string = String.sub or error()
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
14 local Table = require "luan:Table.luan"
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
15 local concat = Table.concat or error()
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
16 local Math = require "luan:Math.luan"
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
17 local random = Math.random or error()
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
18 local Binary = require "luan:Binary.luan"
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
19 local binary_base64_decode = Binary.base64_decode or error()
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
20 local binary_to_string = Binary.to_string or error()
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
21 local Io = require "luan:Io.luan"
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
22 local uri = Io.uri or error()
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
23 local Http = require "luan:http/Http.luan"
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
24 local Logging = require "luan:logging/Logging.luan"
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
25 local logger = Logging.logger "Shared"
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
26
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
27
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
28 local Shared = {}
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
29
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
30 function Shared.head()
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
31 %>
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
32 <meta name="viewport" content="width=device-width, initial-scale=1">
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
33 <style>
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
34 @import "/site.css";
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
35 </style>
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
36 <%
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
37 end
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
38
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
39 local function header(crumbs)
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
40 %>
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
41 <div header>
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
42 <a href="/">Mercurial Hosting</a>
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
43 <% for _, crumb in ipairs(crumbs or {}) do %>
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
44 / <%=crumb%>
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
45 <% end %>
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
46 </div>
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
47 <%
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
48 end
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
49 Shared.header = header
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
50
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
51 function Shared.admin_header()
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
52 header{
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
53 [[<a href="/admin/">Your Repositories</a>]]
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
54 }
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
55 end
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
56
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
57 function Shared.private_header()
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
58 header{
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
59 [[<a href="/private/">private</a>]]
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
60 [[<a href="/private/tools/">tools</a>]]
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
61 }
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
62 end
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
63
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
64 Shared.admin_return = [[<p>Return to <a href="/admin/">Your Repositories</a></p>]]
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
65
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
66 local function base64_decode(s)
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
67 return binary_to_string(binary_base64_decode(s))
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
68 end
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
69
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
70 function Shared.get_user()
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
71 local s = Http.request.headers["Authorization"] or error "not authorized"
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
72 local tp, auth = split(s," ")
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
73 tp=="Basic" or error("invalid auth type: "..tp)
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
74 s = base64_decode(auth)
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
75 s = split(s,":")
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
76 return s
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
77 end
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
78
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
79 local function deep_copy(tbl)
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
80 local t = {}
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
81 for k,v in pairs(tbl) do
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
82 if type(v)=="table" then
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
83 v = deep_copy(v)
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
84 end
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
85 t[k] = v
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
86 end
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
87 return t
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
88 end
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
89
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
90 local set_mt = {}
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
91 function set_mt.__index(table,key)
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
92 return false
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
93 end
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
94
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
95 local function list_to_set(list)
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
96 local set = {}
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
97 for _, v in ipairs(list) do
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
98 set[v] = true
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
99 end
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
100 set_metatable(set,set_mt)
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
101 return set
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
102 end
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
103 Shared.list_to_set = list_to_set
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
104
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
105 function Shared.text_to_list(text)
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
106 local list = {}
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
107 for line in Io.schemes.string(text).read_lines() do
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
108 line = trim(line)
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
109 if line ~= "" then
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
110 list[#list+1] = line
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
111 end
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
112 end
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
113 return list
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
114 end
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
115
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
116 local password_chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
117 do
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
118 local t = {}
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
119 for i in range(1,#password_chars) do
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
120 t[#t+1] = sub_string(password_chars,i,i)
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
121 end
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
122 password_chars = t
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
123 end
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
124
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
125 function Shared.new_password()
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
126 local n = #password_chars
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
127 local t = {}
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
128 for _ in range(1,10) do
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
129 t[#t+1] = password_chars[random(n)]
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
130 end
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
131 return concat(t)
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
132 end
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
133
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
134 local raw_config = parse( uri("file:config/config.luano").read_text() )
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
135 local config = deep_copy(raw_config)
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
136 for name, repo in pairs(config.repos) do
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
137 repo.name = name
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
138 repo.users = list_to_set(repo.users)
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
139 repo.admins = list_to_set(repo.admins)
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
140 end
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
141 config.private = config.private and list_to_set(config.private)
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
142 Shared.config = config
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
143
4
028e74c8889d add all_users
Franklin Schmidt <fschmidt@gmail.com>
parents: 0
diff changeset
144 local user_repos = {}
028e74c8889d add all_users
Franklin Schmidt <fschmidt@gmail.com>
parents: 0
diff changeset
145 for _, repo in pairs(config.repos) do
028e74c8889d add all_users
Franklin Schmidt <fschmidt@gmail.com>
parents: 0
diff changeset
146 for user in pairs(repo.users) do
028e74c8889d add all_users
Franklin Schmidt <fschmidt@gmail.com>
parents: 0
diff changeset
147 local repos = user_repos[user]
028e74c8889d add all_users
Franklin Schmidt <fschmidt@gmail.com>
parents: 0
diff changeset
148 if repos == nil then
028e74c8889d add all_users
Franklin Schmidt <fschmidt@gmail.com>
parents: 0
diff changeset
149 repos = {}
028e74c8889d add all_users
Franklin Schmidt <fschmidt@gmail.com>
parents: 0
diff changeset
150 user_repos[user] = repos
028e74c8889d add all_users
Franklin Schmidt <fschmidt@gmail.com>
parents: 0
diff changeset
151 end
028e74c8889d add all_users
Franklin Schmidt <fschmidt@gmail.com>
parents: 0
diff changeset
152 repos[#repos+1] = repo
028e74c8889d add all_users
Franklin Schmidt <fschmidt@gmail.com>
parents: 0
diff changeset
153 end
028e74c8889d add all_users
Franklin Schmidt <fschmidt@gmail.com>
parents: 0
diff changeset
154 end
028e74c8889d add all_users
Franklin Schmidt <fschmidt@gmail.com>
parents: 0
diff changeset
155 Shared.user_repos = user_repos
028e74c8889d add all_users
Franklin Schmidt <fschmidt@gmail.com>
parents: 0
diff changeset
156
0
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
157 function Shared.get_raw_config()
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
158 return deep_copy(raw_config)
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
159 end
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
160
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
161 local function reload_nginx()
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
162 local cmd = [[sudo $(which nginx) -s reload]]
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
163 local s = uri("bash:"..cmd).read_text()
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
164 logger.info("reload_nginx "..s)
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
165 end
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
166
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
167 function Shared.save_raw_config(raw_config)
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
168 local config_file = uri("file:config/config.luano")
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
169 local config_old = uri("file:config/config.luano.old")
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
170 config_old.delete()
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
171 config_file.move_to(config_old)
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
172 config_file.write_text(stringify(raw_config).."\n")
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
173 Http.reset_luan()
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
174 Luan.do_file "update_repositories.luan"
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
175 reload_nginx()
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
176 end
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
177
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
178 return Shared