summaryrefslogtreecommitdiffstats
path: root/aports.lua
blob: 7a3a7d9193b4c0cf22fb7d4b48721534459c7165 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/usr/bin/env luajit

-- lua turbo application

local turbo = require "turbo"
local inspect = require "inspect"

local tpl = turbo.web.Mustache.TemplateHelper("./tpl")

local ContentsRenderer = class("ContentsRenderer", turbo.web.RequestHandler)

function ContentsRenderer:get()
	local table = {}
    local args = {
        filename = self:get_argument("filename","", true),
        arch = self:get_argument("arch", "x86", true),
    }
	if args.filename ~= "" then
        local result = QueryContents(args)
        if next(result) ~= nil then
            table.rows = result
        end
    end
    table.contents = true
	table.filename = args.filename
	table.header = tpl:render("header.tpl", table)
    table.footer = tpl:render("footer.tpl", table)
	local page = tpl:render(self.options, table)
	self:write(page)
end

local PackagesRenderer = class("PackagesRenderer", turbo.web.RequestHandler)

function PackagesRenderer:get()
	local table = {}
    local args = {
        package = self:get_argument("package","", true)
    }
	if args.package == "" then
		args.package = "%"
	end
	local result = QueryPackages(args)
	if next(result) ~= nil then
		table.rows = result
	end
    table.packages = true
	table.header = tpl:render("header.tpl", table)
	table.footer = tpl:render("footer.tpl", table)
	local page = tpl:render(self.options, table)
	self:write(page)
    print(inspect(table))
end

local PackageRenderer = class("PackageRenderer", turbo.web.RequestHandler)

function PackageRenderer:get(arch, pkgname)
	local fields = {}
	local table = {}
	fields.arch = arch
	fields.pkgname = pkgname
	result = QueryPackage(fields)
	
	if result ~= nil then
		table = result
		for k in pairs (table) do
            if table[k] == "" then
                table[k] = nil
            end
        end
	end
	table.header = tpl:render("header.tpl")
	table.footer = tpl:render("footer.tpl")
	local page = tpl:render(self.options, table)
	self:write(page)	
end

function QueryContents(terms)
	require('DBI')
	local dbh = assert(DBI.Connect('SQLite3', 'db/filelist.db'))
	local sth = assert(dbh:prepare('select * from filelist where file like ? and arch like ? limit 100'))
	sth:execute(terms.filename, terms.arch)
	local r = {}
	for row in sth:rows(true) do
		r[#r + 1] = {
			file = "/" .. row.path .. "/" .. row.file, 
			pkgname = row.pkgname, 
			repo = row.repo,
			arch = row.arch,
		}
	end
	return r

end
function QueryPackages(terms)
    require('DBI')
    local dbh = assert(DBI.Connect('SQLite3', 'db/apkindex.db'))
    local sth = assert(dbh:prepare('select name, version, url, lic, desc, arch, maintainer, datetime(build_time, \'unixepoch\') as build_time from apkindex where name like ? ORDER BY build_time DESC limit 100'))
    sth:execute(terms.package)
    local r = {}
    for row in sth:rows(true) do
        r[#r + 1] = {
            package = row.name,
            version = row.version,
            project = row.url,
            license = row.lic,
            desc = row.desc,
            arch = row.arch,
            repo = "unk",
            maintainer = string.gsub(row.maintainer, '<.*>', ''),
            bdate = row.build_time
        }
    end
    return r
end

function QueryPackage(fields)
	require('DBI')
	local dbh = assert(DBI.Connect('SQLite3', 'db/apkindex.db'))
    local sth = assert(dbh:prepare('select *, datetime(build_time, \'unixepoch\') as build_time from apkindex where name like ? and arch like ? limit 1'))
	sth:execute(fields.pkgname, fields.arch)
	local r = {}
	r = sth:fetch(true)
    print(inspect(r))
	return r
end

turbo.web.Application({
	{"^/$", turbo.web.RedirectHandler, "/packages"},
	{"^/contents$", ContentsRenderer, "contents.tpl"},
    {"^/packages$", PackagesRenderer, "packages.tpl"},
	{"^/package/(.*)/(.*)$", PackageRenderer, "package.tpl"},
	{"/assets/(.*)$", turbo.web.StaticFileHandler, "assets/"},
}):listen(8888)
turbo.ioloop.instance():start()