2022-02-21 22:22:36 +01:00
|
|
|
module server
|
|
|
|
|
|
|
|
import web
|
|
|
|
import os
|
|
|
|
import json
|
|
|
|
|
|
|
|
const repos_file = 'repos.json'
|
|
|
|
|
|
|
|
pub struct GitRepo {
|
|
|
|
pub:
|
|
|
|
url string [required]
|
|
|
|
branch string [required]
|
|
|
|
}
|
|
|
|
|
|
|
|
fn read_repos(path string) ?[]GitRepo {
|
|
|
|
if !os.exists(path) {
|
|
|
|
mut f := os.create(path) ?
|
|
|
|
|
|
|
|
defer {
|
|
|
|
f.close()
|
|
|
|
}
|
|
|
|
|
2022-02-21 22:32:54 +01:00
|
|
|
f.write_string('[]') ?
|
2022-02-21 22:22:36 +01:00
|
|
|
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
|
|
|
|
content := os.read_file(path) ?
|
2022-02-21 22:32:54 +01:00
|
|
|
res := json.decode([]GitRepo, content) ?
|
|
|
|
return res
|
2022-02-21 22:22:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn write_repos(path string, repos []GitRepo) ? {
|
|
|
|
mut f := os.create(path) ?
|
|
|
|
|
|
|
|
defer {
|
|
|
|
f.close()
|
|
|
|
}
|
|
|
|
|
|
|
|
value := json.encode(repos)
|
|
|
|
f.write_string(value) ?
|
|
|
|
}
|
|
|
|
|
|
|
|
['/api/repos'; get]
|
2022-02-22 08:14:20 +01:00
|
|
|
fn (mut app App) get_repos() web.Result {
|
2022-02-21 22:22:36 +01:00
|
|
|
if !app.is_authorized() {
|
|
|
|
return app.text('Unauthorized.')
|
|
|
|
}
|
|
|
|
|
|
|
|
repos := rlock app.git_mutex {
|
|
|
|
read_repos(app.conf.repos_file) or {
|
|
|
|
app.lerror('Failed to read repos file.')
|
|
|
|
|
|
|
|
return app.server_error(500)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return app.json(repos)
|
|
|
|
}
|
|
|
|
|
|
|
|
['/api/repos'; post]
|
2022-02-22 08:14:20 +01:00
|
|
|
fn (mut app App) post_repo() web.Result {
|
2022-02-21 22:22:36 +01:00
|
|
|
if !app.is_authorized() {
|
|
|
|
return app.text('Unauthorized.')
|
|
|
|
}
|
|
|
|
|
|
|
|
if !('url' in app.query && 'branch' in app.query) {
|
|
|
|
return app.server_error(400)
|
|
|
|
}
|
|
|
|
|
|
|
|
new_repo := GitRepo{
|
|
|
|
url: app.query['url']
|
|
|
|
branch: app.query['branch']
|
|
|
|
}
|
|
|
|
|
|
|
|
mut repos := rlock app.git_mutex {
|
|
|
|
read_repos(app.conf.repos_file) or {
|
|
|
|
app.lerror('Failed to read repos file.')
|
|
|
|
|
|
|
|
return app.server_error(500)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We need to check for duplicates
|
|
|
|
for r in repos {
|
|
|
|
if r == new_repo {
|
|
|
|
return app.text('Duplicate repository.')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
repos << new_repo
|
|
|
|
|
|
|
|
lock app.git_mutex {
|
2022-02-21 22:32:54 +01:00
|
|
|
write_repos(app.conf.repos_file, repos) or { return app.server_error(500) }
|
2022-02-21 22:22:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return app.ok('Repo added successfully.')
|
|
|
|
}
|
2022-02-21 22:40:59 +01:00
|
|
|
|
|
|
|
['/api/repos'; delete]
|
2022-02-22 08:14:20 +01:00
|
|
|
fn (mut app App) delete_repo() web.Result {
|
2022-02-21 22:40:59 +01:00
|
|
|
if !app.is_authorized() {
|
|
|
|
return app.text('Unauthorized.')
|
|
|
|
}
|
|
|
|
|
|
|
|
if !('url' in app.query && 'branch' in app.query) {
|
|
|
|
return app.server_error(400)
|
|
|
|
}
|
|
|
|
|
|
|
|
repo_to_remove := GitRepo{
|
|
|
|
url: app.query['url']
|
|
|
|
branch: app.query['branch']
|
|
|
|
}
|
|
|
|
|
|
|
|
mut repos := rlock app.git_mutex {
|
|
|
|
read_repos(app.conf.repos_file) or {
|
|
|
|
app.lerror('Failed to read repos file.')
|
|
|
|
|
|
|
|
return app.server_error(500)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
filtered := repos.filter(it != repo_to_remove)
|
|
|
|
|
|
|
|
lock app.git_mutex {
|
|
|
|
write_repos(app.conf.repos_file, filtered) or { return app.server_error(500) }
|
|
|
|
}
|
|
|
|
|
|
|
|
return app.ok('Repo removed successfully.')
|
|
|
|
}
|