Skip to content

Commit

Permalink
fix: spelling and go version
Browse files Browse the repository at this point in the history
  • Loading branch information
Soypete committed May 27, 2024
1 parent f9017a4 commit aa768cb
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 76 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,6 @@ jobs:
with:
version: v1.29
only-new-issues: true
- name: test
# run only valid tests
run: go test -v ./resftul-go/...
15 changes: 0 additions & 15 deletions Dockerfile

This file was deleted.

44 changes: 24 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
# Golang Webservices in 3 weeks

This repo contains the examples and exercises for The O'reilly online learning course[Go Web Development in 3 weeks](https://www.oreilly.com/live-events/go-for-web-development-in-3-weeks/0636920091015/).

[![wakatime](https://wakatime.com/badge/user/953eeb5a-d347-44af-9d8b-a5b8a918cecf/project/815add1c-01f3-412e-b6cd-730805338e0e.svg)](https://wakatime.com/badge/user/953eeb5a-d347-44af-9d8b-a5b8a918cecf/project/815add1c-01f3-412e-b6cd-730805338e0e)
---
## [![wakatime](https://wakatime.com/badge/user/953eeb5a-d347-44af-9d8b-a5b8a918cecf/project/815add1c-01f3-412e-b6cd-730805338e0e.svg)](https://wakatime.com/badge/user/953eeb5a-d347-44af-9d8b-a5b8a918cecf/project/815add1c-01f3-412e-b6cd-730805338e0e)

In this course, you will learn all the steps to build a web service in [Go](https://go.dev/). From starting the service to monitoring your service, it is meant to give you a comprehensive guide for building production-level service. The first section of the course will handle building restful best practices in Go. Communication is key for designing and building your services and is the foundation on which your functionality will be built. The second section is all about databases. Each web service needs a layer to store, fetch, and manipulate the data communicated with it. We need to make sure our data foundations are strong so we maintain the state of our services. The last section is an overview of reliability. This section just goes over reliability basics, but they are vital things that every engineer should include when building a web service. This course does not go over [Go](https://go.dev/) basics.
In this course, you will learn all the steps to build a web service in [Go](https://go.dev/). From starting the service to monitoring your service, it is meant to give you a comprehensive guide for building production-level service. The first section of the course will handle building restful best practices in Go. Communication is key for designing and building your services and is the foundation on which your functionality will be built. The second section is all about databases. Each web service needs a layer to store, fetch, and manipulate the data communicated with it. We need to make sure our data foundations are strong so we maintain the state of our services. The last section is an overview of reliability. This section just goes over reliability basics, but they are vital things that every engineer should include when building a web service. This course does not go over [Go](https://go.dev/) basics.

## pre-requisites
- [Go](https://go.dev/) installed and running
- Working knowledge of go

* [Go](https://go.dev/) installed and running
* Working knowledge of go

## New to go?

If you are new to go, work through these exercises first
- [Golang Zero to Hero](https://github.com/Soypete/Golang_tutorial_zero_to_hero)
- [Tour of Go](https://go.dev/tour/welcome/1)
- [Gophercises](https://gophercises.com/)

* [Golang Zero to Hero](https://github.com/Soypete/Golang_tutorial_zero_to_hero)
* [Tour of Go](https://go.dev/tour/welcome/1)
* [Gophercises](https://gophercises.com/)

---

Expand All @@ -24,9 +26,9 @@ If you are new to go, work through these exercises first
* [Exercise 1](restful-go/README.md): std lib listenAndServe
* [Quiz](http-quiz/): Status Codes
* [Exercise 2](restful-go/README.md): Using a web framework
* [Exercise 3](restful-go/README.md): Client to query hosted server
* [Exercise 3](restful-go/README.md): Client to query hosted server
* [Exercise 4](restful-go/README/md): HttpTests

# Day 2 - Databases for webservices

* [Exercise 1](database/README.md): Connect to a live database
Expand All @@ -35,7 +37,7 @@ If you are new to go, work through these exercises first
* [Exercise 3](database/README.md): Unit tests with mock client
* [SQL quiz](sql-quiz)

# Day 3 - Metrics and Monitoring
# Day 3 - Metrics and Monitoring

* [Exercise 1](reliable-webservice-go/README.md): API Auth
* [Exercise 2](reliable-webservice-go/README.md): Middleware
Expand All @@ -46,15 +48,17 @@ If you are new to go, work through these exercises first
# Companion Service

This is a live production service that implements the game 20 questions. Code from this service is pulled out for the course exercises. The service code can be found [here](https://github.com/Soypete/golang-cli-game/).

---

## Explore More
- [Echo](https://echo.labstack.com/)
- [Chi](https://github.com/go-chi/chi)
- [Gin](https://github.com/gin-gonic/gin)
- [Fiber](https://github.com/gofiber/fiber)
- [Expvar](https://pkg.go.dev/expvar)
- [pgx](https://github.com/jackc/pgx)
- [pg](https://github.com/lib/pq)
- [sqlx](https://github.com/jmoiron/sqlx)
- [sqlc](https://sqlc.dev/)

* [Echo](https://echo.labstack.com/)
* [Chi](https://github.com/go-chi/chi)
* [Gin](https://github.com/gin-gonic/gin)
* [Fiber](https://github.com/gofiber/fiber)
* [Expvar](https://pkg.go.dev/expvar)
* [pgx](https://github.com/jackc/pgx)
* [pg](https://github.com/lib/pq)
* [sqlx](https://github.com/jmoiron/sqlx)
* [sqlc](https://sqlc.dev/)
17 changes: 9 additions & 8 deletions database/README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
# Day 2 Databases and your Go app

This section of the course contains examples and exercises for effectively leveraging a database in the backend of your Web services.. The following are examples of finished exercises for day 2 of the [O'reilly Media Online Learning Course](https://www.oreilly.com/live-events/go-for-web-development-in-3-weeks/0636920091015/). They can be completed by following along with the instruction or independently.
This section of the course contains examples and exercises for effectively leveraging a database in the backend of your Web services. The following are examples of finished exercises for day 2 of the [O'reilly Media Online Learning Course](https://www.oreilly.com/live-events/go-for-web-development-in-3-weeks/0636920091015/). They can be completed by following along with the instruction or independently.

## Exercise 1

In your server project, add your preferred database driver and connect to the database in the main function (if you missed day one's exercises or have them in a different location using the [ex-1-connection/main.go](ex-1-connection/main.go)). After you have connected and verified your connection, explore the database. Make sure to query the database's users table and handle the error. Try running `SELECT`, `INSERT`, and `UPDATE` statements

### Follow up questions:

* what kind of package organization would make sense for organizing your database logic?
* what data base driver did you pick?
* did the data persist?
* What kind of package organization would make sense for organizing your database logic?
* What data base driver did you pick?
* Did the data persist?

_NOTE_: If you are not using postgres or are completeling this indepently. You can run many datbases locally using docker. Below is an example of running postgres locally in a docker container.
_NOTE_: If you are not using postgres or are completing this independently. You can run many databases locally using docker. Below is an example of running postgres locally in a docker container.

```
docker pull postgres
Expand All @@ -25,9 +25,10 @@ An example of a go app that connect to a local postgres instance is in [database

## Live Demo

Using tools to manage a database. Tools like goose and sqlc can be used to easliy abstract database management into your software stack. The following page is the resulting [code from the live demo](database/demo/main.go)
Using tools to manage a database. Tools like [goose](https://github.com/pressly/goose) and [sqlc](https://sqlc.dev/) can be used to easily abstract database management into your software stack. The following page is the resulting [code from the live demo](database/demo/main.go)

Recording to come
[Demo Recording - sqlc](https://youtu.be/X5VGxx4aQAU)
[Demo Recording - goose](https://youtu.be/3TnEeRttvyo)

## Exercise 2

Expand All @@ -41,4 +42,4 @@ Follow-up Questions:

Using your new database interface mock the database functions into your [tests from last week.](../restful-go/ex-4-tests/framework_test.go). The goal is to imitate db interactions without connecting to the db. You will need to add the DB package to the same repo that your server lives in.

[Here](https://github.com/Soypete/golang-cli-game/blob/24dc57852dee27bb17120555d3d390bd17a78d13/server/api_test.go#L14) are some working tests that use `passBD{}` and `failDB{}` to mock database functionality in an api test.
[Here](https://github.com/Soypete/golang-cli-game/blob/24dc57852dee27bb17120555d3d390bd17a78d13/server/api_test.go#L14) are some working tests that use `passBD{}` and `failDB{}` to mock database functionality in an API test.
11 changes: 0 additions & 11 deletions docker-compose.yml

This file was deleted.

4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
module github.com/soypete/WebServices-in-3-weeks

go 1.20
go 1.21

require (
github.com/go-chi/chi v1.5.4
github.com/jmoiron/sqlx v1.3.5
)

require (
github.com/gofiber/fiber/v2 v2.49.2
github.com/gofiber/fiber/v2 v2.52.1
github.com/mattn/go-sqlite3 v1.14.17
github.com/pressly/goose/v3 v3.11.2
)
Expand Down
18 changes: 9 additions & 9 deletions reliable-webservice-go/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ These are the code-samples, exercises, quizzes, and links needed for this course

### [Exercise 1](/ex-1-auth/auth.go) Add auth to your server endpoints

Auth tooling is sometimes the first or last measure of security for your endpoints. There are various methods for adding auth to your server endpoint. When building production services the methods you choose for authentiction and authorization will be determined by security professionals, but how you implement them is up to you as a developer.
Auth tooling is sometimes the first or last measure of security for your endpoints. There are various methods for adding auth to your server endpoint. When building production services the methods you choose for authentication and authorization will be determined by security professionals, but how you implement them is up to you as a developer.

Add an auth method to your server. You can use any method you like such as a middle-ware, a helper functions or, by manually adding the logic to a single function.

Expand All @@ -22,9 +22,9 @@ Here are some examples of how to add different kinds of auth in your apps. You c

### [Exercise 2](/ex-2-middleware/middleware.go) Add middleware to your go server

_Middleware_: _Middleware also refers to the software that separates two or more APIs and provides services such as rate-limiting, authentication, and logging._[wikipedia](https://en.wikipedia.org/wiki/Middleware) The implementation is typically “built-in” functions. In Go, this tends to be platform style tooling shared across the organization. It allows you add complex funcitonality to your endpoints.
_Middleware_: _Middleware also refers to the software that separates two or more APIs and provides services such as rate-limiting, authentication, and logging._[wikipedia](https://en.wikipedia.org/wiki/Middleware) The implementation is typically “built-in” functions. In Go, this tends to be platform style tooling shared across the organization. It allows you add complex functionality to your endpoints.

Using the same web frameworks you used for your webserver or the go standard library, add a middleware function to your server. You can use middleware to add metrics, auth, profiling or custom logic to your programs. In this exercise add logging, retry, rate limiting or replace the auth from exercise 1 with a middleware.
Using the same web frameworks you used for your web server or the go standard library, add a middleware function to your server. You can use middleware to add metrics, auth, profiling or custom logic to your programs. In this exercise add logging, retry, rate limiting or replace the auth from exercise 1 with a middleware.

Below are framework docs, they will contain examples of build in middleware that you can add with single line functions. They also show you ways of adding custom middleware to your services.

Expand Down Expand Up @@ -63,7 +63,7 @@ Monitoring is often setup as part of the middleware for commonly used metrics li

_NOTE:_ In this exercise it is not intended to have a prometheus instance up and running, just to setup the endpoint where you can manually view the metrics.

Using Expvars and/or Prometheus sdk add some custom metrics.
Using Expvars and/or Prometheus SDK add some custom metrics.

[Example](https://github.com/Soypete/golang-cli-game/blob/main/server/setup.go#L53)

Expand Down Expand Up @@ -92,7 +92,7 @@ Using Expvars and/or Prometheus sdk add some custom metrics.

#### Bonus exercise: Add Pprof

Add pprof to your service to see how it uses memory when handling api calls. Run pprof and see what insights are available to you.
Add pprof to your service to see how it uses memory when handling API calls. Run pprof and see what insights are available to you.

First add the pprof driver to your app.

Expand All @@ -102,7 +102,7 @@ import _ "net/http/pprof"

_*NOTE*: the "\_" means that the import is added globally as a backend system. This is common for servers, db drivers, etc_

add a pprof server as it's own goroutine in your main function.
Add a pprof server as it's own goroutine in your main function.

```go
// run pprof
Expand All @@ -111,7 +111,7 @@ go func() {
}()
```

install [graphviz](https://graphviz.org/download/) on your machine to get the visual insights.
Install [graphviz](https://graphviz.org/download/) on your machine to get the visual insights.

_Mac:_

Expand All @@ -125,9 +125,9 @@ run pprof while your worker-pool is executing
go tool pprof -http=:18080 http://localhost:6060/debug/pprof/profile?seconds=30
```

In the default graph each node is a function that your program is running. Size and color indicate how much cpu and time each function is taking.
In the default graph each node is a function that your program is running. Size and color indicate how much CPU and time each function is taking.

to access the commandline tool tool run
to access the command-line tool tool run

```bash
go tool pprof http://localhost:6060/debug/pprof/allocs
Expand Down
Loading

0 comments on commit aa768cb

Please sign in to comment.