forked from vieter-v/vieter
				
			Switched to net.http.Status for status codes
							parent
							
								
									0a6be87970
								
							
						
					
					
						commit
						e5a630e990
					
				|  | @ -4,6 +4,7 @@ import web | ||||||
| import os | import os | ||||||
| import json | import json | ||||||
| import rand | import rand | ||||||
|  | import net.http | ||||||
| 
 | 
 | ||||||
| pub struct GitRepo { | pub struct GitRepo { | ||||||
| pub mut: | pub mut: | ||||||
|  | @ -78,31 +79,31 @@ fn write_repos(path string, repos &map[string]GitRepo) ? { | ||||||
| ['/api/repos'; get] | ['/api/repos'; get] | ||||||
| fn (mut app App) get_repos() web.Result { | fn (mut app App) get_repos() web.Result { | ||||||
| 	if !app.is_authorized() { | 	if !app.is_authorized() { | ||||||
| 		return app.json(401, new_response('Unauthorized.')) | 		return app.json(http.Status.unauthorized, new_response('Unauthorized.')) | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	repos := rlock app.git_mutex { | 	repos := rlock app.git_mutex { | ||||||
| 		read_repos(app.conf.repos_file) or { | 		read_repos(app.conf.repos_file) or { | ||||||
| 			app.lerror('Failed to read repos file.') | 			app.lerror('Failed to read repos file.') | ||||||
| 
 | 
 | ||||||
| 			return app.status(500) | 			return app.status(http.Status.internal_server_error) | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	return app.json(200, new_data_response(repos)) | 	return app.json(http.Status.ok, new_data_response(repos)) | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| ['/api/repos/:id'; get] | ['/api/repos/:id'; get] | ||||||
| fn (mut app App) get_single_repo(id string) web.Result { | fn (mut app App) get_single_repo(id string) web.Result { | ||||||
| 	if !app.is_authorized() { | 	if !app.is_authorized() { | ||||||
| 		return app.json(401, new_response('Unauthorized.')) | 		return app.json(http.Status.unauthorized, new_response('Unauthorized.')) | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	repos := rlock app.git_mutex { | 	repos := rlock app.git_mutex { | ||||||
| 		read_repos(app.conf.repos_file) or { | 		read_repos(app.conf.repos_file) or { | ||||||
| 			app.lerror('Failed to read repos file.') | 			app.lerror('Failed to read repos file.') | ||||||
| 
 | 
 | ||||||
| 			return app.status(500) | 			return app.status(http.Status.internal_server_error) | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  | @ -112,17 +113,17 @@ fn (mut app App) get_single_repo(id string) web.Result { | ||||||
| 
 | 
 | ||||||
| 	repo := repos[id] | 	repo := repos[id] | ||||||
| 
 | 
 | ||||||
| 	return app.json(200, new_data_response(repo)) | 	return app.json(http.Status.ok, new_data_response(repo)) | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| ['/api/repos'; post] | ['/api/repos'; post] | ||||||
| fn (mut app App) post_repo() web.Result { | fn (mut app App) post_repo() web.Result { | ||||||
| 	if !app.is_authorized() { | 	if !app.is_authorized() { | ||||||
| 		return app.json(401, new_response('Unauthorized.')) | 		return app.json(http.Status.unauthorized, new_response('Unauthorized.')) | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	new_repo := repo_from_params(app.query) or { | 	new_repo := repo_from_params(app.query) or { | ||||||
| 		return app.json(400, new_response(err.msg)) | 		return app.json(http.Status.bad_request, new_response(err.msg)) | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	id := rand.uuid_v4() | 	id := rand.uuid_v4() | ||||||
|  | @ -131,37 +132,37 @@ fn (mut app App) post_repo() web.Result { | ||||||
| 		read_repos(app.conf.repos_file) or { | 		read_repos(app.conf.repos_file) or { | ||||||
| 			app.lerror('Failed to read repos file.') | 			app.lerror('Failed to read repos file.') | ||||||
| 
 | 
 | ||||||
| 			return app.status(500) | 			return app.status(http.Status.internal_server_error) | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	// We need to check for duplicates | 	// We need to check for duplicates | ||||||
| 	for _, repo in repos { | 	for _, repo in repos { | ||||||
| 		if repo == new_repo { | 		if repo == new_repo { | ||||||
| 			return app.json(400, new_response('Duplicate repository.')) | 			return app.json(http.Status.bad_request, new_response('Duplicate repository.')) | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	repos[id] = new_repo | 	repos[id] = new_repo | ||||||
| 
 | 
 | ||||||
| 	lock app.git_mutex { | 	lock app.git_mutex { | ||||||
| 		write_repos(app.conf.repos_file, &repos) or { return app.status(500) } | 		write_repos(app.conf.repos_file, &repos) or { return app.status(http.Status.internal_server_error) } | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	return app.json(200, new_response('Repo added successfully.')) | 	return app.json(http.Status.ok, new_response('Repo added successfully.')) | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| ['/api/repos/:id'; delete] | ['/api/repos/:id'; delete] | ||||||
| fn (mut app App) delete_repo(id string) web.Result { | fn (mut app App) delete_repo(id string) web.Result { | ||||||
| 	if !app.is_authorized() { | 	if !app.is_authorized() { | ||||||
| 		return app.json(401, new_response('Unauthorized.')) | 		return app.json(http.Status.unauthorized, new_response('Unauthorized.')) | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	mut repos := rlock app.git_mutex { | 	mut repos := rlock app.git_mutex { | ||||||
| 		read_repos(app.conf.repos_file) or { | 		read_repos(app.conf.repos_file) or { | ||||||
| 			app.lerror('Failed to read repos file.') | 			app.lerror('Failed to read repos file.') | ||||||
| 
 | 
 | ||||||
| 			return app.status(500) | 			return app.status(http.Status.internal_server_error) | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  | @ -175,20 +176,20 @@ fn (mut app App) delete_repo(id string) web.Result { | ||||||
| 		write_repos(app.conf.repos_file, &repos) or { return app.server_error(500) } | 		write_repos(app.conf.repos_file, &repos) or { return app.server_error(500) } | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	return app.json(200, new_response('Repo removed successfully.')) | 	return app.json(http.Status.ok, new_response('Repo removed successfully.')) | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| ['/api/repos/:id'; patch] | ['/api/repos/:id'; patch] | ||||||
| fn (mut app App) patch_repo(id string) web.Result { | fn (mut app App) patch_repo(id string) web.Result { | ||||||
| 	if !app.is_authorized() { | 	if !app.is_authorized() { | ||||||
| 		return app.json(401, new_response('Unauthorized.')) | 		return app.json(http.Status.unauthorized, new_response('Unauthorized.')) | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	mut repos := rlock app.git_mutex { | 	mut repos := rlock app.git_mutex { | ||||||
| 		read_repos(app.conf.repos_file) or { | 		read_repos(app.conf.repos_file) or { | ||||||
| 			app.lerror('Failed to read repos file.') | 			app.lerror('Failed to read repos file.') | ||||||
| 
 | 
 | ||||||
| 			return app.status(500) | 			return app.status(http.Status.internal_server_error) | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  | @ -202,5 +203,5 @@ fn (mut app App) patch_repo(id string) web.Result { | ||||||
| 		write_repos(app.conf.repos_file, &repos) or { return app.server_error(500) } | 		write_repos(app.conf.repos_file, &repos) or { return app.server_error(500) } | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	return app.json(200, new_response('Repo updated successfully.')) | 	return app.json(http.Status.ok, new_response('Repo updated successfully.')) | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -12,7 +12,7 @@ import net.http | ||||||
| // server is still responsive. | // server is still responsive. | ||||||
| ['/health'; get] | ['/health'; get] | ||||||
| pub fn (mut app App) healthcheck() web.Result { | pub fn (mut app App) healthcheck() web.Result { | ||||||
| 	return app.json(200, new_response('Healthy.')) | 	return app.json(http.Status.ok, new_response('Healthy.')) | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // get_root handles a GET request for a file on the root | // get_root handles a GET request for a file on the root | ||||||
|  | @ -31,7 +31,7 @@ fn (mut app App) get_root(filename string) web.Result { | ||||||
| 	// Scuffed way to respond to HEAD requests | 	// Scuffed way to respond to HEAD requests | ||||||
| 	if app.req.method == http.Method.head { | 	if app.req.method == http.Method.head { | ||||||
| 		if os.exists(full_path) { | 		if os.exists(full_path) { | ||||||
| 			return app.status(200) | 			return app.status(http.Status.ok) | ||||||
| 		} | 		} | ||||||
| 
 | 
 | ||||||
| 		return app.not_found() | 		return app.not_found() | ||||||
|  | @ -43,7 +43,7 @@ fn (mut app App) get_root(filename string) web.Result { | ||||||
| ['/publish'; post] | ['/publish'; post] | ||||||
| fn (mut app App) put_package() web.Result { | fn (mut app App) put_package() web.Result { | ||||||
| 	if !app.is_authorized() { | 	if !app.is_authorized() { | ||||||
| 		return app.json(401, new_response('Unauthorized.')) | 		return app.json(http.Status.unauthorized, new_response('Unauthorized.')) | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	mut pkg_path := '' | 	mut pkg_path := '' | ||||||
|  | @ -64,7 +64,7 @@ fn (mut app App) put_package() web.Result { | ||||||
| 		util.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'") | 			app.lwarn("Failed to upload '$pkg_path'") | ||||||
| 
 | 
 | ||||||
| 			return app.json(500, new_response('Failed to upload file.')) | 			return app.json(http.Status.internal_server_error, new_response('Failed to upload file.')) | ||||||
| 		} | 		} | ||||||
| 
 | 
 | ||||||
| 		sw.stop() | 		sw.stop() | ||||||
|  | @ -73,7 +73,7 @@ fn (mut app App) put_package() web.Result { | ||||||
| 		app.lwarn('Tried to upload package without specifying a Content-Length.') | 		app.lwarn('Tried to upload package without specifying a Content-Length.') | ||||||
| 
 | 
 | ||||||
| 		// length required | 		// length required | ||||||
| 		return app.status(411) | 		return app.status(http.Status.length_required) | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	res := app.repo.add_from_path(pkg_path) or { | 	res := app.repo.add_from_path(pkg_path) or { | ||||||
|  | @ -81,17 +81,17 @@ fn (mut app App) put_package() web.Result { | ||||||
| 
 | 
 | ||||||
| 		os.rm(pkg_path) or { app.lerror("Failed to remove download '$pkg_path': $err.msg") } | 		os.rm(pkg_path) or { app.lerror("Failed to remove download '$pkg_path': $err.msg") } | ||||||
| 
 | 
 | ||||||
| 		return app.json(500, new_response('Failed to add package.')) | 		return app.json(http.Status.internal_server_error, new_response('Failed to add package.')) | ||||||
| 	} | 	} | ||||||
| 	if !res.added { | 	if !res.added { | ||||||
| 		os.rm(pkg_path) or { app.lerror("Failed to remove download '$pkg_path': $err.msg") } | 		os.rm(pkg_path) or { app.lerror("Failed to remove download '$pkg_path': $err.msg") } | ||||||
| 
 | 
 | ||||||
| 		app.lwarn("Duplicate package '$res.pkg.full_name()'.") | 		app.lwarn("Duplicate package '$res.pkg.full_name()'.") | ||||||
| 
 | 
 | ||||||
| 		return app.json(400, new_response('File already exists.')) | 		return app.json(http.Status.bad_request, new_response('File already exists.')) | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	app.linfo("Added '$res.pkg.full_name()' to repository.") | 	app.linfo("Added '$res.pkg.full_name()' to repository.") | ||||||
| 
 | 
 | ||||||
| 	return app.json(200, new_response('Package added successfully.')) | 	return app.json(http.Status.ok, new_response('Package added successfully.')) | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -139,7 +139,7 @@ pub const ( | ||||||
| pub struct Context { | pub struct Context { | ||||||
| mut: | mut: | ||||||
| 	content_type string = 'text/plain' | 	content_type string = 'text/plain' | ||||||
| 	status       int    = 200 | 	status       http.Status    = http.Status.ok | ||||||
| pub: | pub: | ||||||
| 	// HTTP Request | 	// HTTP Request | ||||||
| 	req http.Request | 	req http.Request | ||||||
|  | @ -212,12 +212,12 @@ pub fn (mut ctx Context) send_response_to_client(mimetype string, res string) bo | ||||||
| 		text: res | 		text: res | ||||||
| 	} | 	} | ||||||
| 	resp.set_version(.v1_1) | 	resp.set_version(.v1_1) | ||||||
| 	resp.set_status(http.status_from_int(ctx.status)) | 	resp.set_status(ctx.status) | ||||||
| 	send_string(mut ctx.conn, resp.bytestr()) or { return false } | 	send_string(mut ctx.conn, resp.bytestr()) or { return false } | ||||||
| 	return true | 	return true | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| pub fn (mut ctx Context) text(status int, s string) Result { | pub fn (mut ctx Context) text(status http.Status, s string) Result { | ||||||
| 	ctx.status = status | 	ctx.status = status | ||||||
| 
 | 
 | ||||||
| 	ctx.send_response_to_client('text/plain', s) | 	ctx.send_response_to_client('text/plain', s) | ||||||
|  | @ -226,7 +226,7 @@ pub fn (mut ctx Context) text(status int, s string) Result { | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // json<T> HTTP_OK with json_s as payload with content-type `application/json` | // json<T> HTTP_OK with json_s as payload with content-type `application/json` | ||||||
| pub fn (mut ctx Context) json<T>(status int, j T) Result { | pub fn (mut ctx Context) json<T>(status http.Status, j T) Result { | ||||||
| 	ctx.status = status | 	ctx.status = status | ||||||
| 
 | 
 | ||||||
| 	json_s := json.encode(j) | 	json_s := json.encode(j) | ||||||
|  | @ -280,7 +280,7 @@ pub fn (mut ctx Context) file(f_path string) Result { | ||||||
| 		header: header.join(web.headers_close) | 		header: header.join(web.headers_close) | ||||||
| 	} | 	} | ||||||
| 	resp.set_version(.v1_1) | 	resp.set_version(.v1_1) | ||||||
| 	resp.set_status(http.status_from_int(ctx.status)) | 	resp.set_status(ctx.status) | ||||||
| 	send_string(mut ctx.conn, resp.bytestr()) or { return Result{} } | 	send_string(mut ctx.conn, resp.bytestr()) or { return Result{} } | ||||||
| 
 | 
 | ||||||
| 	mut buf := []byte{len: 1_000_000} | 	mut buf := []byte{len: 1_000_000} | ||||||
|  | @ -306,7 +306,7 @@ pub fn (mut ctx Context) file(f_path string) Result { | ||||||
| 	return Result{} | 	return Result{} | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| pub fn (mut ctx Context) status(status int) Result { | pub fn (mut ctx Context) status(status http.Status) Result { | ||||||
| 	return ctx.text(status, '') | 	return ctx.text(status, '') | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | @ -337,7 +337,7 @@ pub fn (mut ctx Context) redirect(url string) Result { | ||||||
| 
 | 
 | ||||||
| // not_found Send an not_found response | // not_found Send an not_found response | ||||||
| pub fn (mut ctx Context) not_found() Result { | pub fn (mut ctx Context) not_found() Result { | ||||||
| 	return ctx.status(404) | 	return ctx.status(http.Status.not_found) | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // add_header Adds an header to the response with key and val | // add_header Adds an header to the response with key and val | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue