Initial part of repos API (SEGFAULTS) [CI SKIP]
This commit is contained in:
parent
92ad0c51eb
commit
e13252d368
10 changed files with 150 additions and 39 deletions
100
src/server/git.v
Normal file
100
src/server/git.v
Normal file
|
|
@ -0,0 +1,100 @@
|
|||
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()
|
||||
}
|
||||
|
||||
f.write_string('{}') ?
|
||||
|
||||
return []
|
||||
}
|
||||
|
||||
content := os.read_file(path) ?
|
||||
return json.decode([]GitRepo, content)
|
||||
}
|
||||
|
||||
fn write_repos(path string, repos []GitRepo) ? {
|
||||
mut f := os.create(path) ?
|
||||
|
||||
defer {
|
||||
f.close()
|
||||
}
|
||||
|
||||
dump(repos)
|
||||
value := json.encode(repos)
|
||||
f.write_string(value) ?
|
||||
}
|
||||
|
||||
['/api/repos'; get]
|
||||
pub fn (mut app App) get_repos() web.Result {
|
||||
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]
|
||||
pub fn (mut app App) post_repo() web.Result {
|
||||
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 {
|
||||
write_repos(app.conf.repos_file, repos) or {
|
||||
return app.server_error(500)
|
||||
}
|
||||
}
|
||||
|
||||
return app.ok('Repo added successfully.')
|
||||
}
|
||||
|
|
@ -7,25 +7,6 @@ import time
|
|||
import rand
|
||||
import util
|
||||
|
||||
const prefixes = ['B', 'KB', 'MB', 'GB']
|
||||
|
||||
// pretty_bytes converts a byte count to human-readable version
|
||||
fn pretty_bytes(bytes int) string {
|
||||
mut i := 0
|
||||
mut n := f32(bytes)
|
||||
|
||||
for n >= 1024 {
|
||||
i++
|
||||
n /= 1024
|
||||
}
|
||||
|
||||
return '${n:.2}${server.prefixes[i]}'
|
||||
}
|
||||
|
||||
fn is_pkg_name(s string) bool {
|
||||
return s.contains('.pkg')
|
||||
}
|
||||
|
||||
// healthcheck just returns a string, but can be used to quickly check if the
|
||||
// server is still responsive.
|
||||
['/health'; get]
|
||||
|
|
@ -65,7 +46,7 @@ fn (mut app App) put_package() web.Result {
|
|||
pkg_path = os.join_path_single(app.conf.download_dir, rand.uuid_v4())
|
||||
}
|
||||
|
||||
app.ldebug("Uploading $length bytes (${pretty_bytes(length.int())}) to '$pkg_path'.")
|
||||
app.ldebug("Uploading $length bytes (${util.pretty_bytes(length.int())}) to '$pkg_path'.")
|
||||
|
||||
// This is used to time how long it takes to upload a file
|
||||
mut sw := time.new_stopwatch(time.StopWatchOptions{ auto_start: true })
|
||||
|
|
|
|||
|
|
@ -12,9 +12,11 @@ const port = 8000
|
|||
struct App {
|
||||
web.Context
|
||||
pub:
|
||||
conf env.ServerConfig [required: web_global]
|
||||
conf env.ServerConfig [required; web_global]
|
||||
pub mut:
|
||||
repo repo.Repo [required; web_global]
|
||||
// This is used to claim the file lock on the repos file
|
||||
git_mutex shared util.Dummy
|
||||
}
|
||||
|
||||
pub fn server() ? {
|
||||
|
|
|
|||
Reference in a new issue