This commit is contained in:
		
							parent
							
								
									60b07ed446
								
							
						
					
					
						commit
						86841f6190
					
				
					 1 changed files with 24 additions and 0 deletions
				
			
		|  | @ -1,3 +1,4 @@ | ||||||
|  | import gleam/bit_array | ||||||
| import gleam/bytes_builder | import gleam/bytes_builder | ||||||
| import gleam/erlang/process | import gleam/erlang/process | ||||||
| import gleam/http | import gleam/http | ||||||
|  | @ -29,6 +30,11 @@ pub fn main() { | ||||||
|             on_close: fn(_state) { io.println("Goodbye!") }, |             on_close: fn(_state) { io.println("Goodbye!") }, | ||||||
|             handler: handle_we_message, |             handler: handle_we_message, | ||||||
|           ) |           ) | ||||||
|  |         ["echo"] -> echo_body(req) | ||||||
|  |         ["chunk"] -> serve_chunk(req) | ||||||
|  |         ["file", ..rest] -> serve_file(req, rest) | ||||||
|  |         ["form"] -> handle_form(req) | ||||||
|  | 
 | ||||||
|         _ -> not_found |         _ -> not_found | ||||||
|       } |       } | ||||||
|     } |     } | ||||||
|  | @ -60,3 +66,21 @@ fn handle_we_message(state, conn, message) { | ||||||
|     mist.Closed | mist.Shutdown -> actor.Stop(process.Normal) |     mist.Closed | mist.Shutdown -> actor.Stop(process.Normal) | ||||||
|   } |   } | ||||||
| } | } | ||||||
|  | 
 | ||||||
|  | fn echo_body(request: Request(Connection)) -> Response(ResponseData) { | ||||||
|  |   let content_type = | ||||||
|  |     request | ||||||
|  |     |> request.get_header("content-type") | ||||||
|  |     |> result.unwrap("test/plain") | ||||||
|  | 
 | ||||||
|  |   mist.read_body(request, 1024 * 1024 * 10) | ||||||
|  |   |> result.map(fn(req) { | ||||||
|  |     response.new(200) | ||||||
|  |     |> response.set_body(mist.Bytes(bytes_builder.from_bit_array(req.body))) | ||||||
|  |     |> response.set_header("content-type", content_type) | ||||||
|  |   }) | ||||||
|  |   |> result.lazy_unwrap(fn() { | ||||||
|  |     response.new(400) | ||||||
|  |     |> response.set_body(mist.Bytes(bytes_builder.new())) | ||||||
|  |   }) | ||||||
|  | } | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		
		Reference in a new issue