forked from vieter-v/vieter
Compare commits
2 Commits
5edbaf9b4b
...
93c0dc9c4f
| Author | SHA1 | Date |
|---|---|---|
|
|
93c0dc9c4f | |
|
|
17038dc33c |
|
|
@ -4,10 +4,14 @@ import web
|
||||||
import os
|
import os
|
||||||
import log
|
import log
|
||||||
import io
|
import io
|
||||||
|
import repo
|
||||||
|
|
||||||
const port = 8000
|
const port = 8000
|
||||||
|
|
||||||
const buf_size = 1_000_000
|
const buf_size = 1_000_000
|
||||||
|
|
||||||
|
const db_name = 'pieter.db.tar.gz'
|
||||||
|
|
||||||
struct App {
|
struct App {
|
||||||
web.Context
|
web.Context
|
||||||
api_key string [required; web_global]
|
api_key string [required; web_global]
|
||||||
|
|
@ -21,7 +25,7 @@ fn exit_with_message(code int, msg string) {
|
||||||
exit(code)
|
exit(code)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn reader_to_file(mut reader io.BufferedReader, path string) ? {
|
fn reader_to_file(mut reader io.BufferedReader, length int, path string) ? {
|
||||||
// Open up a file for writing to
|
// Open up a file for writing to
|
||||||
mut file := os.create(path) ?
|
mut file := os.create(path) ?
|
||||||
defer {
|
defer {
|
||||||
|
|
@ -29,36 +33,26 @@ fn reader_to_file(mut reader io.BufferedReader, path string) ? {
|
||||||
}
|
}
|
||||||
|
|
||||||
mut buf := []byte{len: buf_size}
|
mut buf := []byte{len: buf_size}
|
||||||
|
mut bytes_left := length
|
||||||
|
|
||||||
// Repeat as long as the stream still has data
|
// Repeat as long as the stream still has data
|
||||||
for {
|
for bytes_left > 0 {
|
||||||
// TODO don't just endlessly loop if reading keeps failing
|
|
||||||
println('heey')
|
|
||||||
// TODO check if just breaking here is safe
|
// TODO check if just breaking here is safe
|
||||||
bytes_read := reader.read(mut &buf) or {
|
bytes_read := reader.read(mut buf) or { break }
|
||||||
println('youre here')
|
bytes_left -= bytes_read
|
||||||
break
|
|
||||||
}
|
|
||||||
println(bytes_read)
|
|
||||||
|
|
||||||
mut to_write := bytes_read
|
mut to_write := bytes_read
|
||||||
|
|
||||||
for to_write > 0 {
|
for to_write > 0 {
|
||||||
// TODO don't just loop infinitely here
|
// TODO don't just loop infinitely here
|
||||||
bytes_written := file.write(buf[bytes_read - to_write..bytes_read]) or {
|
bytes_written := file.write(buf[bytes_read - to_write..bytes_read]) or { continue }
|
||||||
println("$err.msg")
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
println(bytes_written)
|
|
||||||
|
|
||||||
to_write = to_write - bytes_written
|
to_write = to_write - bytes_written
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
println('File complete!')
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[put; '/pkgs/:pkg']
|
['/pkgs/:pkg'; put]
|
||||||
fn (mut app App) put_package(pkg string) web.Result {
|
fn (mut app App) put_package(pkg string) web.Result {
|
||||||
full_path := os.join_path_single(app.repo_dir, pkg)
|
full_path := os.join_path_single(app.repo_dir, pkg)
|
||||||
|
|
||||||
|
|
@ -66,11 +60,21 @@ fn (mut app App) put_package(pkg string) web.Result {
|
||||||
return app.text('File already exists.')
|
return app.text('File already exists.')
|
||||||
}
|
}
|
||||||
|
|
||||||
reader_to_file(mut app.reader, full_path) or {
|
if length := app.req.header.get(.content_length) {
|
||||||
return app.text('Failed to upload file.')
|
reader_to_file(mut app.reader, length.int(), full_path) or {
|
||||||
|
return app.text('Failed to upload file.')
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return app.text("Content-Type header isn't set.")
|
||||||
}
|
}
|
||||||
|
|
||||||
return app.text('just stop')
|
repo.add_package(os.join_path_single(app.repo_dir, db_name), full_path) or {
|
||||||
|
os.rm(full_path) or { println('Failed to remove $full_path') }
|
||||||
|
|
||||||
|
return app.text('Failed to add package to repo.')
|
||||||
|
}
|
||||||
|
|
||||||
|
return app.text('Package added successfully.')
|
||||||
}
|
}
|
||||||
|
|
||||||
// ['/publish'; post]
|
// ['/publish'; post]
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,12 @@
|
||||||
|
module repo
|
||||||
|
|
||||||
|
import os
|
||||||
|
|
||||||
|
pub fn add_package(db_path string, pkg_path string) ? {
|
||||||
|
res := os.execute("repo-add '$db_path' '$pkg_path'")
|
||||||
|
|
||||||
|
if res.exit_code != 0 {
|
||||||
|
println(res.output)
|
||||||
|
return error('repo-add failed.')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -447,13 +447,13 @@ fn handle_conn<T>(mut conn net.TcpConn, mut app T, routes map[string]Route) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// req := http.parse_request(mut reader) or {
|
// req := http.parse_request(mut reader) or {
|
||||||
// // Prevents errors from being thrown when BufferedReader is empty
|
// // Prevents errors from being thrown when BufferedReader is empty
|
||||||
// if '$err' != 'none' {
|
// if '$err' != 'none' {
|
||||||
// eprintln('error parsing request: $err')
|
// eprintln('error parsing request: $err')
|
||||||
// }
|
// }
|
||||||
// return
|
// return
|
||||||
// }
|
// }
|
||||||
|
|
||||||
// URL Parse
|
// URL Parse
|
||||||
url := urllib.parse(head.url) or {
|
url := urllib.parse(head.url) or {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue