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

Use Standard Go program layout #37

Open
wants to merge 104 commits into
base: master
Choose a base branch
from
Open

Conversation

kimanimichael
Copy link
Owner

Guided by project-layout

Copy link

@rodneyosodo rodneyosodo left a comment

Choose a reason for hiding this comment

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

It would be a good idea to separate the API from the service logic and repository layers. This is a well-known architectural pattern called Hexagonal Architecture https://dev.to/bagashiz/building-restful-api-with-hexagonal-architecture-in-go-1mij

.gitignore Outdated
@@ -1,2 +1,2 @@
whitepointinventory
build
.idea/

Choose a reason for hiding this comment

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

Add new line

Copy link
Owner Author

Choose a reason for hiding this comment

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

Done


go build -o build/whitepointinventory ./cmd/whitepointinventory

build/whitepointinventory

Choose a reason for hiding this comment

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

add new line

Copy link
Owner Author

Choose a reason for hiding this comment

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

Done

@@ -0,0 +1,111 @@
package main

Choose a reason for hiding this comment

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

Since this is one binary project you can use cmd/main.go rather than cmd/whitepointinventory/whitepointinventory.go

Copy link
Owner Author

Choose a reason for hiding this comment

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

Done

}
log.Printf("Server starting on port %s", portString)

err = srv.ListenAndServe()

Choose a reason for hiding this comment

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

inline error check

if err := srv.ListenAndServer(); err != nil {

Copy link
Owner Author

Choose a reason for hiding this comment

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

Implemented

@@ -10,7 +10,7 @@ RUN go mod download

COPY . .

RUN go build -o whitepointinventory
RUN go build -o whitepointinventory ./cmd/whitepointinventory

FROM alpine:latest

Choose a reason for hiding this comment

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

You can use scratch instead of an alpine image

Copy link
Owner Author

Choose a reason for hiding this comment

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

Changed to a scratch base image

@@ -10,7 +10,7 @@ RUN go mod download

COPY . .

RUN go build -o whitepointinventory
RUN go build -o whitepointinventory ./cmd/whitepointinventory

FROM alpine:latest

Choose a reason for hiding this comment

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

Don't copy the env file

Copy link
Owner Author

Choose a reason for hiding this comment

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

Removed.

@@ -0,0 +1,5 @@
rm -rf build/whitepointinventory

Choose a reason for hiding this comment

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

It will overwrite no need to remove

Copy link
Owner Author

Choose a reason for hiding this comment

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

The script runs the old binary if there's an error in compilation, which is undesired behaviour for this particular use case. Hence why I remove it.

@@ -1,12 +1,12 @@
package main
package jsonresponses

Choose a reason for hiding this comment

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

Not a good package name you can rename it to internal/api -> package api

Copy link
Owner Author

Choose a reason for hiding this comment

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

I intend the APIs here to be public APIs. What would be a good package name to still have them in the pkg directory?

Choose a reason for hiding this comment

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

Public you mean people outside the project would import it as a package?

Copy link
Owner Author

Choose a reason for hiding this comment

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

Yes.

Choose a reason for hiding this comment

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

The change the name

"net/http"
)

func HandlerHealth(w http.ResponseWriter, r *http.Request) {

Choose a reason for hiding this comment

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

Copy link
Owner Author

Choose a reason for hiding this comment

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

Done.

@kimanimichael
Copy link
Owner Author

73 commits one PR. Just merge 😂

Had to refactor entire code-base. I am doing some clean up and then you can re-review. You can just check of all your comments are handled.

@kimanimichael
Copy link
Owner Author

It would be a good idea to separate the API from the service logic and repository layers. This is a well-known architectural pattern called Hexagonal Architecture https://dev.to/bagashiz/building-restful-api-with-hexagonal-architecture-in-go-1mij

Done.

@kimanimichael
Copy link
Owner Author

73 commits one PR. Just merge 😂

Had to refactor entire code-base. I am doing some clean up and then you can re-review. You can just check of all your comments are handled.

You can check if your comments are handled now.


func (a *UserAuth) MiddlewareAuth(handler authedHandler) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
APIKey, err := auth.GetAPIKey(r.Header)

Choose a reason for hiding this comment

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

Suggested change
APIKey, err := auth.GetAPIKey(r.Header)
apiKey, err := auth.GetAPIKey(r.Header)

For non exported variables start with small letter. This applies to everyplace.

@@ -0,0 +1,33 @@
package middleware

Choose a reason for hiding this comment

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

rename file to auth.go

@@ -0,0 +1,11 @@
package domain

Choose a reason for hiding this comment

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

Try and reduce the underscores in file naming. Use DDD where we have different domains farmer, payments, purchases and users

domain(for example users)

api
repository

repository.go
middleware(if you have any)
domain(for example user).go (defines the repository and service interface)
service.go (implements service)

# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

Choose a reason for hiding this comment

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants