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

Add lab 10 - Puppy REST #669

Closed
wants to merge 2 commits into from
Closed

Add lab 10 - Puppy REST #669

wants to merge 2 commits into from

Conversation

alextmz
Copy link
Collaborator

@alextmz alextmz commented Sep 16, 2019

Fixes #668

Review of colleague's PR #584

Changes proposed in this PR:

  • Add Lab 10 - REST Puppy

@codecov
Copy link

codecov bot commented Sep 16, 2019

Codecov Report

Merging #669 into master will not change coverage.
The diff coverage is 100%.

Impacted file tree graph

@@          Coverage Diff           @@
##           master   #669    +/-   ##
======================================
  Coverage     100%   100%            
======================================
  Files         285    290     +5     
  Lines        5899   6139   +240     
======================================
+ Hits         5899   6139   +240
Impacted Files Coverage Δ
10_rest/alextmz/cmd/puppy-server/main.go 100% <100%> (ø)
10_rest/alextmz/pkg/puppy/errors.go 100% <100%> (ø)
10_rest/alextmz/pkg/puppy/store/syncstore.go 100% <100%> (ø)
10_rest/alextmz/pkg/rest/rest.go 100% <100%> (ø)
10_rest/alextmz/pkg/puppy/store/mapstore.go 100% <100%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update b79a437...98d56ca. Read the comment docs.

@alextmz alextmz closed this Sep 22, 2019
@alextmz alextmz reopened this Sep 22, 2019
@alextmz
Copy link
Collaborator Author

alextmz commented Sep 22, 2019

Woops.

@alextmz alextmz changed the title Add lab 10 - Puppy REST [WIP] Add lab 10 - Puppy REST Sep 22, 2019
@alextmz alextmz added the lab10 label Sep 22, 2019
10_rest/alextmz/cmd/puppy-server/main.go Outdated Show resolved Hide resolved
10_rest/alextmz/cmd/puppy-server/main.go Outdated Show resolved Hide resolved
10_rest/alextmz/pkg/puppy/errors.go Show resolved Hide resolved
10_rest/alextmz/pkg/puppy/store/mapstore.go Show resolved Hide resolved
@alextmz alextmz changed the title [WIP] Add lab 10 - Puppy REST Add lab 10 - Puppy REST Sep 29, 2019
Copy link
Collaborator

@n0npax n0npax left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some nits

10_rest/alextmz/cmd/puppy-server/main.go Outdated Show resolved Hide resolved
out io.Writer = os.Stdout

// shutdownhttp signals main() to... shutdown the http server
shutdownhttp = make(chan bool)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe it's overkill, but let's wait for code owners. You are not dealing with graceful shutdown to justify this

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My intent was to do real connection testing on main(), not just handler testing, and this was a (relatively) elegant way I found to http.Shutdown a http.ListenAndServe; but as I later found out, Travis doesn't allow connections to localhost.
So this could had been substituted for a global http.Server variable, and the test could had http.Shutdown the server it before calling main().
Which also doesn't feel a good solution despite the simplicity.
So I left it in as it works, and doesn't use another global.
I hope reviewers like it but I admit I'm not high on hopes 🤞

10_rest/alextmz/cmd/puppy-server/main.go Outdated Show resolved Hide resolved
10_rest/alextmz/pkg/puppy/store/mapstore.go Outdated Show resolved Hide resolved
}

puppy, err := ht.Store.ReadPuppy(id)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sometimes you've got new lines here, sometimes you don't have them. This is NIT and can be easily ignored

n++
}

return n, nil
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return len(puppies)-1, nil should allow getting rid of n from this function

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, actually len(puppies). Changed!

Copy link
Contributor

@juliaogris juliaogris left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few things to get started.

10_rest/alextmz/cmd/puppy-server/main.go Show resolved Hide resolved
return
}

jsonfile, err := os.Open(*flagfile)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a kingpimg flag file type that opens the file as part of command line parsing. saves you the error handling here.

jsonfile, err := os.Open(*flagfile)
if err != nil {
fmt.Fprintf(out, "error opening file: %v\n", err)
return
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

os.Exit(1) or log.Fatal(msg) ?

// printpuppies print n puppies contained in the store s
func printpuppies(s puppy.Storer, n int) {
for i := 1; i <= n; i++ {
p, err := s.ReadPuppy(i)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are making assumptions on puppy IDs being a sequence, that might be dangerous imo.
I think it's niver if you just impoment the Stringer interface for puppy, and if you truly want to print a slice of puppies, retrieve them as a list and then maybe fmt.Println(strings.Join(puppies, "\n")) or similar. It is a easier to test correct string creation than it is to test correct printing.

// test if main() is really serving the HTTP we expect
//
// unfortunately, looks like travis-ci does not allow
// connections to 127.0.0.1: we get 'connect: connection refused'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm this sounds strange.
I'm pretty sure I recall using httptest.Server which essentially starts a webserver on localhost in tests executed on Travis.
Can you use httptest.Server?

@juliaogris
Copy link
Contributor

The go-course is now closed. Thank you very much for participating.

@juliaogris juliaogris closed this Feb 8, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Lab 10 - Puppy REST
4 participants