Skip to content

Commit

Permalink
rewriting
Browse files Browse the repository at this point in the history
  • Loading branch information
hexisdylan committed Jul 23, 2022
1 parent 9167a1e commit 39956fd
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 21 deletions.
85 changes: 71 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,75 @@
**TODO: Add description**
## PragTechnologies - Coding Bootcamp

## Installation
Each Code Progress can be viewed on my [Commit History](https://github.com/skedaddl3/elixir-pragstudio/commits/master)

If [available in Hex](https://hex.pm/docs/publish), the package can be installed
by adding `servy` to your list of dependencies in `mix.exs`:
1. pragstudio-elixir-01-intro - [Introduction](https://github.com/skedaddl3/elixir-pragstudio/commit/2d7cedd6e95697d686a85a9da1c3b8ddf929c073#diff-ba7efc6b78ec91b2d2aed8b82c1a8eb5ec11ed843c6ab6ff2db4f04e0ccb05b0)

```elixir
def deps do
[
{:servy, "~> 0.1.0"}
]
end
```
2. pragstudio-elixir-02-mix-project - [Mix Project](https://github.com/skedaddl3/elixir-pragstudio/blob/321ddf15370ba01234986400f80de1f1c2dfef11/lib/servy.ex)

Documentation can be generated with [ExDoc](https://github.com/elixir-lang/ex_doc)
and published on [HexDocs](https://hexdocs.pm). Once published, the docs can
be found at <https://hexdocs.pm/servy>.
3. pragstudio-elixir-03-transforms - [Transforms](https://github.com/skedaddl3/elixir-pragstudio/blob/0e49501f2c43fd64deb6f886efa5d2b9adcf45ec/lib/handler.ex)

4. pragstudio-elixir-04-parse - [Parse]()

5. pragstudio-elixir-05-route-response - [Route Response]()

6. pragstudio-elixir-06-function-clauses - [Function Clauses]()

7. pragstudio-elixir-07-params-status - [Params Status]()

8. pragstudio-elixir-08-rewriting - [Rewriting]()

9. pragstudio-elixir-09-serve-files - [Serve Files]()

10. pragstudio-elixir-10-module-attributes - [Module Attributes]()

11. pragstudio-elixir-11-organizing-code - [Organizing Code]()

12. pragstudio-elixir-12-structs - [Structs]()

13. pragstudio-elixir-13-post-requests - [Post Requests]()

14. pragstudio-elixir-14-recursion - [Recursion]()

15. pragstudio-elixir-15-enum-1 - [Enum 1]()

16. pragstudio-elixir-15-enum-2 - [Enum 2]()

17. pragstudio-elixir-16-comprehensions - [Comprehensions]()

18. pragstudio-elixir-17-phoenix - [Phoenix]()

19. pragstudio-elixir-18-testing - [Testing]()

20. pragstudio-elixir-19-external-library - [External Library]()

21. pragstudio-elixir-20-web-sockets - [Web Sockets]()

22. pragstudio-elixir-21-processes-1 - [Processes 1]()

23. pragstudio-elixir-21-processes-2 - [Processes 2]()

24. pragstudio-elixir-22-messages - [Messages]()

25. pragstudio-elixir-23-tasks - [Tasks]()

26. pragstudio-elixir-24-stateful-server-1 - [Stateful Server 1]()

27. pragstudio-elixir-24-stateful-server-2 - [Stateful Server 2]()

28. pragstudio-elixir-24-stateful-server-3 - [Stateful Server 3]()

29. pragstudio-elixir-24-stateful-server-4 - [Stateful Server 4]()

30. pragstudio-elixir-25-refactor-server - [Refactor Server]()

31. pragstudio-elixir-26-gen-server - [Gen Server]()

32. pragstudio-elixir-27-another-genserver - [Another Gen Server]()

33. pragstudio-elixir-28-linking-processes - [Linking Processes]()

34. pragstudio-elixir-29-supervisors-1 - [Supervisors 1]()

35. pragstudio-elixir-29-supervisors-2 - [Supervisors 2]()

36. pragstudio-elixir-30-application - [Application]()
40 changes: 33 additions & 7 deletions lib/handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,26 @@ defmodule Servy.Handler do
def handle(request) do
request
|> parse
|> rewrite_path
|> log
|> route
|> track
|> format_response
end

def track(%{status: 404, path: path} = conv) do
IO.puts("Warning: #{path} is on the loose!")
conv
end

def track(conv), do: conv

def rewrite_path(%{path: "/wildlife"} = conv) do
%{conv | path: "/wildthings"}
end

def rewrite_path(conv), do: conv

def parse(request) do
[method, path, _] =
request
Expand All @@ -19,23 +34,23 @@ defmodule Servy.Handler do

def log(conv), do: IO.inspect(conv)

def route(conv) do
route(conv, conv.method, conv.path)
end
# def route(conv) do
# route(conv, conv.method, conv.path)
# end

def route(conv, "GET", "/wildthings") do
def route(%{method: "GET", path: "/wildthings"} = conv) do
%{conv | status: 200, resp_body: "Bears, Lions, Tigers"}
end

def route(conv, "GET", "/bears") do
def route(%{method: "GET", path: "/bears"} = conv) do
%{conv | status: 200, resp_body: "Teddy, Smokey, Paddington"}
end

def route(conv, "GET", "/bears/" <> id) do
def route(%{method: "GET", path: "/bears" <> id} = conv) do
%{conv | status: 200, resp_body: "Bear #{id}"}
end

def route(conv, method, path) do
def route(%{path: path} = conv) do
%{conv | status: 404, resp_body: "No #{path} here!"}
end

Expand All @@ -61,6 +76,17 @@ defmodule Servy.Handler do
end
end

request = """
GET /bigfoot HTTP/1.1
Host: example.com
User-Agent: ExampleBrowser/1.0
Accept: */*
"""

response = Servy.Handler.handle(request)
IO.puts(response)

request = """
GET /wildthings HTTP/1.1
Host: example.com
Expand Down

0 comments on commit 39956fd

Please sign in to comment.