forked from vieter-v/vieter
				
			Split off server into own module
							parent
							
								
									1d434db166
								
							
						
					
					
						commit
						92ad0c51eb
					
				
							
								
								
									
										19
									
								
								src/env.v
								
								
								
								
							
							
						
						
									
										19
									
								
								src/env.v
								
								
								
								
							|  | @ -11,24 +11,24 @@ const file_suffix = '_FILE' | |||
| 
 | ||||
| pub struct ServerConfig { | ||||
| pub: | ||||
| 	log_level string [default: WARN] | ||||
| 	log_file string [default: 'vieter.log'] | ||||
| 	pkg_dir string | ||||
| 	log_level    string [default: WARN] | ||||
| 	log_file     string [default: 'vieter.log'] | ||||
| 	pkg_dir      string | ||||
| 	download_dir string | ||||
| 	api_key string | ||||
| 	repo_dir string | ||||
| 	api_key      string | ||||
| 	repo_dir     string | ||||
| } | ||||
| 
 | ||||
| pub struct BuildConfig { | ||||
| pub: | ||||
| 	api_key string | ||||
| 	api_key  string | ||||
| 	repo_dir string | ||||
| 	address string | ||||
| 	address  string | ||||
| } | ||||
| 
 | ||||
| fn get_env_var(field_name string) ?string { | ||||
| 	env_var_name := '${prefix}${field_name.to_upper()}' | ||||
| 	env_file_name := '${prefix}${field_name.to_upper()}${file_suffix}' | ||||
| 	env_var_name := '$env.prefix$field_name.to_upper()' | ||||
| 	env_file_name := '$env.prefix$field_name.to_upper()$env.file_suffix' | ||||
| 	env_var := os.getenv(env_var_name) | ||||
| 	env_file := os.getenv(env_file_name) | ||||
| 
 | ||||
|  | @ -76,6 +76,5 @@ pub fn load<T>() ?T { | |||
| 			default | ||||
| 		} | ||||
| 	} | ||||
| 
 | ||||
| 	return res | ||||
| } | ||||
|  |  | |||
							
								
								
									
										41
									
								
								src/main.v
								
								
								
								
							
							
						
						
									
										41
									
								
								src/main.v
								
								
								
								
							|  | @ -1,48 +1,17 @@ | |||
| module main | ||||
| 
 | ||||
| import os | ||||
| import io | ||||
| 
 | ||||
| [noreturn] | ||||
| fn exit_with_message(code int, msg string) { | ||||
| 	eprintln(msg) | ||||
| 	exit(code) | ||||
| } | ||||
| 
 | ||||
| fn reader_to_file(mut reader io.BufferedReader, length int, path string) ? { | ||||
| 	mut file := os.create(path) ? | ||||
| 	defer { | ||||
| 		file.close() | ||||
| 	} | ||||
| 
 | ||||
| 	mut buf := []byte{len: buf_size} | ||||
| 	mut bytes_left := length | ||||
| 
 | ||||
| 	// Repeat as long as the stream still has data | ||||
| 	for bytes_left > 0 { | ||||
| 		// TODO check if just breaking here is safe | ||||
| 		bytes_read := reader.read(mut buf) or { break } | ||||
| 		bytes_left -= bytes_read | ||||
| 
 | ||||
| 		mut to_write := bytes_read | ||||
| 
 | ||||
| 		for to_write > 0 { | ||||
| 			// TODO don't just loop infinitely here | ||||
| 			bytes_written := file.write(buf[bytes_read - to_write..bytes_read]) or { continue } | ||||
| 
 | ||||
| 			to_write = to_write - bytes_written | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
| import server | ||||
| import util | ||||
| 
 | ||||
| fn main() { | ||||
| 	if os.args.len == 1 { | ||||
| 		exit_with_message(1, 'No action provided.') | ||||
| 		util.exit_with_message(1, 'No action provided.') | ||||
| 	} | ||||
| 
 | ||||
| 	match os.args[1] { | ||||
| 		'server' { server() ? } | ||||
| 		'server' { server.server() ? } | ||||
| 		'build' { build() ? } | ||||
| 		else { exit_with_message(1, 'Unknown action: ${os.args[1]}') } | ||||
| 		else { util.exit_with_message(1, 'Unknown action: ${os.args[1]}') } | ||||
| 	} | ||||
| } | ||||
|  |  | |||
|  | @ -1,4 +1,4 @@ | |||
| module main | ||||
| module server | ||||
| 
 | ||||
| import net.http | ||||
| 
 | ||||
|  | @ -1,10 +1,11 @@ | |||
| module main | ||||
| module server | ||||
| 
 | ||||
| import web | ||||
| import os | ||||
| import repo | ||||
| import time | ||||
| import rand | ||||
| import util | ||||
| 
 | ||||
| const prefixes = ['B', 'KB', 'MB', 'GB'] | ||||
| 
 | ||||
|  | @ -18,7 +19,7 @@ fn pretty_bytes(bytes int) string { | |||
| 		n /= 1024 | ||||
| 	} | ||||
| 
 | ||||
| 	return '${n:.2}${prefixes[i]}' | ||||
| 	return '${n:.2}${server.prefixes[i]}' | ||||
| } | ||||
| 
 | ||||
| fn is_pkg_name(s string) bool { | ||||
|  | @ -69,7 +70,7 @@ fn (mut app App) put_package() web.Result { | |||
| 		// This is used to time how long it takes to upload a file | ||||
| 		mut sw := time.new_stopwatch(time.StopWatchOptions{ auto_start: true }) | ||||
| 
 | ||||
| 		reader_to_file(mut app.reader, length.int(), pkg_path) or { | ||||
| 		util.reader_to_file(mut app.reader, length.int(), pkg_path) or { | ||||
| 			app.lwarn("Failed to upload '$pkg_path'") | ||||
| 
 | ||||
| 			return app.text('Failed to upload file.') | ||||
|  | @ -1,15 +1,14 @@ | |||
| module main | ||||
| module server | ||||
| 
 | ||||
| import web | ||||
| import os | ||||
| import log | ||||
| import repo | ||||
| import env | ||||
| import util | ||||
| 
 | ||||
| const port = 8000 | ||||
| 
 | ||||
| const buf_size = 1_000_000 | ||||
| 
 | ||||
| struct App { | ||||
| 	web.Context | ||||
| pub: | ||||
|  | @ -18,12 +17,12 @@ pub mut: | |||
| 	repo repo.Repo [required; web_global] | ||||
| } | ||||
| 
 | ||||
| fn server() ? { | ||||
| pub fn server() ? { | ||||
| 	conf := env.load<env.ServerConfig>() ? | ||||
| 
 | ||||
| 	// Configure logger | ||||
| 	log_level := log.level_from_tag(conf.log_level) or { | ||||
| 		exit_with_message(1, 'Invalid log level. The allowed values are FATAL, ERROR, WARN, INFO & DEBUG.') | ||||
| 		util.exit_with_message(1, 'Invalid log level. The allowed values are FATAL, ERROR, WARN, INFO & DEBUG.') | ||||
| 	} | ||||
| 
 | ||||
| 	mut logger := log.Log{ | ||||
|  | @ -45,11 +44,13 @@ fn server() ? { | |||
| 		exit(1) | ||||
| 	} | ||||
| 
 | ||||
| 	os.mkdir_all(conf.download_dir) or { exit_with_message(1, 'Failed to create download directory.') } | ||||
| 	os.mkdir_all(conf.download_dir) or { | ||||
| 		util.exit_with_message(1, 'Failed to create download directory.') | ||||
| 	} | ||||
| 
 | ||||
| 	web.run(&App{ | ||||
| 		logger: logger | ||||
| 		conf: conf | ||||
| 		repo: repo | ||||
| 	}, port) | ||||
| 	}, server.port) | ||||
| } | ||||
							
								
								
									
										35
									
								
								src/util.v
								
								
								
								
							
							
						
						
									
										35
									
								
								src/util.v
								
								
								
								
							|  | @ -1,9 +1,44 @@ | |||
| module util | ||||
| 
 | ||||
| import os | ||||
| import io | ||||
| import crypto.md5 | ||||
| import crypto.sha256 | ||||
| 
 | ||||
| const reader_buf_size = 1_000_000 | ||||
| 
 | ||||
| [noreturn] | ||||
| pub fn exit_with_message(code int, msg string) { | ||||
| 	eprintln(msg) | ||||
| 	exit(code) | ||||
| } | ||||
| 
 | ||||
| pub fn reader_to_file(mut reader io.BufferedReader, length int, path string) ? { | ||||
| 	mut file := os.create(path) ? | ||||
| 	defer { | ||||
| 		file.close() | ||||
| 	} | ||||
| 
 | ||||
| 	mut buf := []byte{len: util.reader_buf_size} | ||||
| 	mut bytes_left := length | ||||
| 
 | ||||
| 	// Repeat as long as the stream still has data | ||||
| 	for bytes_left > 0 { | ||||
| 		// TODO check if just breaking here is safe | ||||
| 		bytes_read := reader.read(mut buf) or { break } | ||||
| 		bytes_left -= bytes_read | ||||
| 
 | ||||
| 		mut to_write := bytes_read | ||||
| 
 | ||||
| 		for to_write > 0 { | ||||
| 			// TODO don't just loop infinitely here | ||||
| 			bytes_written := file.write(buf[bytes_read - to_write..bytes_read]) or { continue } | ||||
| 
 | ||||
| 			to_write = to_write - bytes_written | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| // hash_file returns the md5 & sha256 hash of a given file | ||||
| // TODO actually implement sha256 | ||||
| pub fn hash_file(path &string) ?(string, string) { | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue