ademalidurmus / golang-rest-api-example

Golang REST API Example

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Project structure

yakuter opened this issue · comments

This is a great start. Now we have things to do. First we need a good project structure. You can use this repo as a reference:
https://github.com/golang-standards/project-layout

Now I want you to create these folders first.

..
./cmd
./cmd/golang-rest-api-example/
./cmd/golang-rest-api-example/main.go - Your main function should be here

/internal/api/ - Endpoints will be here (get and return json responses)
/internal/app/ - Business logic will be here (fetch data from database and send to api endpoints)
/internal/model/ - Model files
/internal/database/ - database connection file

If you have any problem, just write a reply to this issue ;) Good luck.

How is it going adem? Do you need help?

Not too bad @yakuter . I refactored project structure and also dockerfile. Now, I'm moving the other methods to new package locations, working on this branch. If I can't succeed, I will ask you for help. Thanks for your interest.

I have completed your suggestions @yakuter :)
Could you review PR? (I couldn't assign to you. I don't know how can I do it. I think, I've got it. First of all, I had to invite you to project.)

Ok this is great. Now there is a new subject. O is strict and if you don't use a variable or struct in a function, it doesn't build. However there is no check for global variables and unused functions. To check them we use linters. Also linters are the best practices of programming languages. They teach you a log about language.

In go environment this repo is good to check linter errors: https://github.com/golangci/golangci-lint

When I used this and check your repo, I get these errors. I want you to do same check at your local and fix them.
Screen Shot 2020-11-14 at 17 36 18

Thanks for your provide guidance. I forgot some struct in main.go. I remove them.

Special thanks for golangci-lint 🎉 😍

Great. Because you are fast, let's get deeper. I want you to write a middleware. Let's say this is a xss security middleware and this middleware will clear all < and > symbols in all fields of the incoming json object. As a hint, you need to use reflect package to iterate all fields in a struct ;)

I apologize for the late response. Could you check it my solution?

Generate your own go struct tag. The tag shoud be like this

type Person struct {
Name string adem:true
}

encrypt with md5 which fields which are true.

person := model.Person.,vsdlkfn

yeniPerson := sifrele(person)

I have done, could you check it?

image