Code Monkey home page Code Monkey logo

building-microservices-youtube's Issues

7-Swagger refactor

Just saying that there're some flaws in the Swagger branch. The Documenting RESTful APIs with Swagger.
But it's quite helpful actually since it really makes me dive deep into the code and read everything three, four times.

Validation logs not showing up using cURL

Hi, and thank you for the great series!

I'm currently after Episode 6, where a JSON validator was introduced to handle validation in PUT and POST requests. It does work for me as in your video but with a one difference. If I use cURL I don't see validation errors while making those requests. It returns only correct HTTP status (Bad Request) but logs only something like parse error: Invalid numeric literal at line 1, column 6. In this case I can only see a printed log message inside the terminal, where the server runs. Oddly enough it does work if using Postman or any other HTTP Client, which display that error in the response from the server.

// products.go
err = prod.Validate()
		if err != nil {
                         // get logged in terminal
			t.l.Printf("[ERROR] validating product: %s\n", err)
			// get returned in response while using Postman but not in cUrl
                         http.Error(
				rw,
				fmt.Sprintf("Error validating product: %s", err),
				http.StatusBadRequest)
			return
		}

It was enough jq to delete from the request.

put and post not working

after swagger and the client generation branches the validator does not work after a successful product is validated, it fails a bad sku or price fine. keeps returning empty requests on posts and puts. A giant refactor without vids to back it up ๐Ÿ‘Ž

this is code that i used git clone with no code I typed myself up to episode 8 i was typing it out and it was great for learning ๐Ÿ‘

Delete product doesn't work properly

Given a list of products like:

โŸฉ curl localhost:9090/products | jq
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   630  100   630    0     0   615k      0 --:--:-- --:--:-- --:--:--  615k
[
  {
    "id": 1,
    "name": "Latte",
    "description": "Frothy milky coffee",
    "price": 2.45,
    "sku": "abc323"
  },
  {
    "id": 2,
    "name": "Esspresso",
    "description": "Short and strong coffee without milk",
    "price": 1.99,
    "sku": "fjd34"
  },
  {
    "id": 3,
    "name": "Esspresso",
    "description": "Short and strong coffee without milk",
    "price": 1.99,
    "sku": "fjd34"
  },
  {
    "id": 4,
    "name": "Esspresso",
    "description": "Short and strong coffee without milk",
    "price": 1.99,
    "sku": "fjd34"
  },
  {
    "id": 5,
    "name": "Esspresso",
    "description": "Short and strong coffee without milk",
    "price": 1.99,
    "sku": "fjd34"
  },
  {
    "id": 6,
    "name": "Esspresso",
    "description": "Short and strong coffee without milk",
    "price": 1.99,
    "sku": "fjd34"
  }
]

After deleting one of them the list is not updated as expected:

โŸฉ curl localhost:9090/products/2 -XDELETE
โŸฉ curl localhost:9090/products | jq
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   198  100   198    0     0  66000      0 --:--:-- --:--:-- --:--:-- 99000
[
  {
    "id": 1,
    "name": "Latte",
    "description": "Frothy milky coffee",
    "price": 2.45,
    "sku": "abc323"
  },
  {
    "id": 3,
    "name": "Esspresso",
    "description": "Short and strong coffee without milk",
    "price": 1.99,
    "sku": "fjd34"
  }
]

Valid request throws error: Interface conversion error is nil not validator.validationerrors for valid request body

Issue

The below validator works for invalid request body, but for a valid body, it initiates a panic

Code sample, to showcase or reproduce:

data/validation.go

func (v *Validation) Validate(i interface{}) ValidationErrors {
	errs := v.validate.Struct(i).(validator.ValidationErrors)

	if len(errs) == 0 {
		return nil
	}

	var returnErrs []ValidationError
	for _, err := range errs {
		// cast the FieldError into our ValidationError and append to the slice
		ve := ValidationError{err.(validator.FieldError)}
		returnErrs = append(returnErrs, ve)
	}

	return returnErrs
}

handlers/post.go :

func (p *Products) Create(rw http.ResponseWriter, r *http.Request) {
	prod := r.Context().Value(KeyProduct{}).(data.Product) // Panic originate here. Check below for struct definiton

	p.l.Printf("[DEBUG] Inserting product: %#v\n", prod)
	data.AddProduct(prod)
}

data/products.go

// data.Product
type Product struct {

	ID int `json:"id"` // Unique identifier for the product

	Name string `json:"name" validate:"required"`

	Description string `json:"description"`

	SKU string `json:"sku" validate:"sku"`
}

Stack Trace:

products-api 2020/07/04 17:04:27 http: panic serving 127.0.0.1:47468: interface conversion: interface {} is *data.Product, not data.Product
goroutine 50 [running]:
net/http.(*conn).serve.func1(0xc0005fe0a0)
        /usr/local/go/src/net/http/server.go:1772 +0x139
panic(0x951fe0, 0xc00030a480)
        /usr/local/go/src/runtime/panic.go:973 +0x3e3
github.com/nicholasjackson/building-microservices-youtube/product-api/handlers.(*Products).Create(0xc0003192c0, 0xaa9320, 0xc00046a000, 0xc000400400)
        /tmp/building-microservices-youtube/product-api/handlers/post.go:20 +0x349
net/http.HandlerFunc.ServeHTTP(0xc000319510, 0xaa9320, 0xc00046a000, 0xc000400400)
        /usr/local/go/src/net/http/server.go:2012 +0x44
github.com/nicholasjackson/building-microservices-youtube/product-api/handlers.(*Products).MiddlewareValidateProduct.func1(0xaa9320, 0xc00046a000, 0xc000400200)
        /tmp/building-microservices-youtube/product-api/handlers/middleware.go:42 +0x553
net/http.HandlerFunc.ServeHTTP(0xc00027c0a0, 0xaa9320, 0xc00046a000, 0xc000400200)
        /usr/local/go/src/net/http/server.go:2012 +0x44
github.com/gorilla/mux.(*Router).ServeHTTP(0xc000156300, 0xaa9320, 0xc00046a000, 0xc000400000)
        /home/ayman/go/pkg/mod/github.com/gorilla/[email protected]/mux.go:212 +0xe2
net/http.serverHandler.ServeHTTP(0xc00055c1c0, 0xaa9320, 0xc00046a000, 0xc000400000)
        /usr/local/go/src/net/http/server.go:2807 +0xa3
net/http.(*conn).serve(0xc0005fe0a0, 0xaaa460, 0xc00021c040)
        /usr/local/go/src/net/http/server.go:1895 +0x86c
created by net/http.(*Server).Serve
        /usr/local/go/src/net/http/server.go:2933 +0x35c

Could it be the version 10 from go validator has broken the implementation? I have cloned your repo and checked out episode_7 branch and it throws the above error for valid requests.

Hi, will the series continue, also will you cover deployment??

Hi Nic,

I hope you are well ๐Ÿ‘

Two important questions:

  1. Can you cover Docker and cloud deployment for microservices written in Go?
  2. Will the service continue?

Microservice deployment is super important would be great to finish this tutorial with full deployment with docker etc..

Bests,

Sean.

issue in currency dependancy

F:\gocode\pkg\mod\github.com\nicholasjackson\building-microservices-youtube\[email protected]\data\products.go:55:11: undefined: "github.com/nicholasjackson/building-microservices-youtube/currency/protos/currency".Currency_SubscribeRatesClient

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.