Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

restful-go: add clear completion requirements for exercise 2 #54

Merged
merged 1 commit into from
Sep 19, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 21 additions & 14 deletions restful-go/README.md
Original file line number Diff line number Diff line change
@@ -1,29 +1,34 @@
# Day 1: Writing Restful Golang Servers and Client

These are the code-samples, exercises, quizzes, and links needed for this course. To complete these exercises follow the instructions in this doc. In the subdirectories of this section are example solutions. Your soluutions do not need to match the provided solutions. The goal of the exercises is to learn and understand the why things are implemented so that a participant apply principles to any framework or problem set.
These are the code-samples, exercises, quizzes, and links needed for this course. To complete these exercises follow the instructions in this doc. In the subdirectories of this section are example solutions. Your soluutions do not need to match the provided solutions. The goal of the exercises is to learn and understand the why things are implemented so that a participant apply principles to any framework or problem set.

## Exercises

### [Exercise 1](/ex-1-servers/server.go) Build a Standard Library Server
### [Exercise 1](/ex-1-servers/server.go) Build a Standard Library Server

In a main function, create a webserver using the `ListenAndServe()` function. This server should have at least one endpoint that accepts a parameter and returns it in a response. An example of a finished exercise in in [ex-1-server](ex-1-servers/server.go)

- reference: [go-by-example Https server](https://gobyexample.com/http-servers)
* reference: [go-by-example Https server](https://gobyexample.com/http-servers)

_ *NOTE*: `ListenAndServe()` is great for prototyping and non-production development. When you are building out your services, make sure you have security in mind. Use a custom `mux`. We will learn more aabout security best practices later on in the course. Check out [Reliable Webservices](../reliable-webservice-go/README.md).
\_ _NOTE_: `ListenAndServe()` is great for prototyping and non-production development. When you are building out your services, make sure you have security in mind. Use a custom `mux`. We will learn more aabout security best practices later on in the course. Check out [Reliable Webservices](../reliable-webservice-go/README.md).

## [Exercise 2](/ex-2-web-frameworks/framework.go) Build a Server Using an opensource Framework
## [Exercise 2](/ex-2-web-frameworks/framework.go) Build a Server Using an opensource Framework

The [Standard Library](https://pkg.go.dev/net/http) provides all the functionality needed to build a robust web server, but sometimes, instead of building out your own infrastructre, there is a benefit to adopting an opensource frameworks at the backbone of your server infrastructure. This exercise is you chance to experiment with some popular Go web frameworks.

*Instructions:* Pick a web frame work and implement a server that can accept a username at add it to the database. Use the provided webframeworks.go file to implement this server and add the username top the database using the provided functions. Add logic to handle duplicate usernames, empty username parameters, or any additional error cases. An example of the finished exercise using the [Chi]() framework is in the directory [ex-2-webframeworks/framework.go](/ex-2-webframeworks/framework.go)
_Instructions:_ Pick a web framework and implement a server that can accept a username, validate it, and print the username to standard out. Add logic to handle duplicate usernames, empty username parameters, or any additional error cases. When a username is accepted and printed to standard out return a 200 class status code. If the parameter is empty or invalid return a 400 class error code. Handle any other errors. _We will connect this server to a database in the Day 2 exercises_

An example of the finished exercise using the [Chi](https://github.com/go-chi/chi) framework is in the directory [ex-2-webframeworks/framework.go](/ex-2-webframeworks/framework.go).

Another example using [go-fiber](https://github.com/gofiber/fiber) can be found [here](https://github.com/Soypete/WebServices-in-3-weeks/tree/main/restful-go/ex-2-fiber).

_*Note:* you do not have to do this with a web framework. Feel free to use the standard library `net/http` tooling if you are more comfortable. The opstins for using a web framework are just to try you hand at evaluating popular opensource tools. But just like adding any tool to your software start there are risks and rewards._

Here are some examples of opensource frameworks:
- [Chi](https://github.com/go-chi/chi)
- [Gin](https://github.com/gin-gonic/gin) <!-- uses it own context that predates context.Context-->
- [Fiber](https://github.com/gofiber/fiber) <!-- uses fasthhtp -->

* [Chi](https://github.com/go-chi/chi)
* [Gin](https://github.com/gin-gonic/gin) <!-- uses it own context that predates context.Context-->
* [Fiber](https://github.com/gofiber/fiber) <!-- uses fasthhtp -->

### [Exercise 3](/ex-3-clients/client.go) Build a Go app that calls your new endpoint.

Expand All @@ -34,12 +39,14 @@ Using the standard library to create a client that calls your new web endpoint.
Using the [httptest](https://pkg.go.dev/net/http/httptest#example-ResponseRecorder) package add some tests for at least one of our newly created endpoints. You should have one test that checks for a 200s class error and one that tests for a 400s class error. Add additional test validations as time allows.

Here are some examples of tests for the above opensource frameworks.
- [Chi tests](https://go-chi.io/#/pages/testing)
- [Gin tests](https://gin-gonic.com/docs/testing/)
- [Fiber tests](https://docs.gofiber.io/api/app#test)

* [Chi tests](https://go-chi.io/#/pages/testing)
* [Gin tests](https://gin-gonic.com/docs/testing/)
* [Fiber tests](https://docs.gofiber.io/api/app#test)

## Helpful links:

We will be using `curl` to test the functionality of our endpoints and servers. If you are unfamiliar with the `curl` syntax check out these resources:
- [Free code camp](https://www.freecodecamp.org/news/how-to-start-using-curl-and-why-a-hands-on-introduction-ea1c913caaaa/)
- [Curl docs](https://curl.se/docs/manual.html)

* [Free code camp](https://www.freecodecamp.org/news/how-to-start-using-curl-and-why-a-hands-on-introduction-ea1c913caaaa/)
* [Curl docs](https://curl.se/docs/manual.html)
Loading