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

Improve feature #5

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ taskService/vendor
apiService/vendor
apiService/.idea

reactUserInterface/node_modules

protos/vendor
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions .idea/aws.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions .idea/codeStyles/codeStyleConfig.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions .idea/microservice-demo.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions .idea/modules/taskService.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ The project is the world's simplest task management software. A user can registe
| [Project Service](https://github.com/Joker666/microservice-demo/tree/main/projectService) | Python, MySQL | It handles project and tags creation and update |
| [Task Service](https://github.com/Joker666/microservice-demo/tree/main/taskService) | Ruby, PostgreSQL | It handles task creation, add tags to task and assign task to a user |
| [API Service](https://github.com/Joker666/microservice-demo/tree/main/apiService) | Go | It handles routing api calls to all the services and a proxy server to handle HTTP 1.0 requests |
| [User Interface](https://github.com/Joker666/microservice-demo/tree/main/reactUserInterface) | React/NodeJs | User Interface to have visual representation of the GRPC calls exposed via Proxy |

We have chosen to use monorepo for all the services since it will ease the process for us.

Expand Down Expand Up @@ -113,6 +114,7 @@ and it will spin up all the services. If it fails for some reason due to MySQL/M
![List of services](https://i.imgur.com/WypWbA9.png)

And now you have a set of microservices running that you can access with the proxy server's url `localhost:9090`
The url for the user interface for login and register 'localhost:3000'

## Roadmap
- Write development docs
Expand Down
7 changes: 7 additions & 0 deletions apiService/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ WORKDIR ${SRC_DIR}
COPY . ${SRC_DIR}

# Build App
#the commented lines below are for when you are using windows machine

#RUN apt-get update && apt-get install -y dos2unix
#COPY /build.sh ${SRC_DIR}/buildNew.sh
#RUN dos2unix ${SRC_DIR}/buildNew.sh && apt-get --purge remove -y dos2unix && rm -rf /var/lib/apt/lists/*

#on linux based machines the below command will do
RUN ./build.sh

EXPOSE 50059
Expand Down
30 changes: 29 additions & 1 deletion apiService/cmd/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import (
"context"
"flag"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"github.com/rs/cors"
"github.com/spf13/cobra"
"google.golang.org/grpc"
"google.golang.org/protobuf/proto"
"net/http"
)

Expand Down Expand Up @@ -57,13 +59,39 @@ func proxy(cmd *cobra.Command, args []string) error {
func newGateway(ctx context.Context) (http.Handler, error) {
grpcServerAddress := os.Getenv("HOST") + ":" + os.Getenv("PORT")
grpcServerEndpoint := flag.String("grpc-server-endpoint", grpcServerAddress, "gRPC server endpoint")
//add specific headers to handle CORS in new instance of ServeMux
//mux := runtime.NewServeMux(runtime.WithForwardResponseOption(httpResponseModifier))
mux := runtime.NewServeMux()
handler := cors.Default().Handler(mux)
opts := []grpc.DialOption{grpc.WithInsecure()}
err := gw.RegisterAPIHandlerFromEndpoint(ctx, mux, *grpcServerEndpoint, opts)
if err != nil {
return nil, err
}
return mux, nil
return handler, nil
}

//Modifier to append CORS headers
func httpResponseModifier(ctx context.Context, w http.ResponseWriter, _ proto.Message) error {
setDefaultHeaders(w)

return nil
}

//CORS specific headers - these values are only for dev, for prod the allow origin needs to be narrowed down
func setDefaultHeaders(w http.ResponseWriter) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PATCH, PUT, DELETE, OPTIONS")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With")
w.Header().Set("Access-Control-Allow-Credentials", "true")
w.Header().Set("Cache-Control", "no-store, no-cache, must-revalidate, post-check=0, pre-check=0")
w.Header().Set("Content-Type", "application/json; charset=UTF-8; application/x-www-form-urlencoded")
w.Header().Set("Vary", "Accept-Encoding")
w.Header().Set("Access-Control-Max-Age", "3600")
w.Header().Set("Access-Control-Expose-Headers", "Authorization")
w.Header().Set("Access-Control-Expose-Headers", "responseType")
w.Header().Set("Access-Control-Expose-Headers", "observe")
w.WriteHeader(http.StatusOK)
}

func serveSwagger(w http.ResponseWriter, r *http.Request) {
Expand Down
25 changes: 14 additions & 11 deletions apiService/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@ module github.com/Joker666/microservice-demo/apiService
go 1.15

require (
github.com/Joker666/microservice-demo/protos v0.0.0-20201123092353-3a5df4f866e5
github.com/golang/protobuf v1.4.3 // indirect
github.com/Joker666/microservice-demo/protos v0.0.0-20201212172044-93c42dffa776
github.com/grpc-ecosystem/go-grpc-middleware v1.2.2
github.com/grpc-ecosystem/grpc-gateway v1.16.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.0.1
github.com/grpc-ecosystem/grpc-gateway/v2 v2.2.0
github.com/joho/godotenv v1.3.0
github.com/spf13/cobra v1.1.1
golang.org/x/net v0.0.0-20201110031124-69a78807bb2b // indirect
golang.org/x/sys v0.0.0-20201113233024-12cec1faf1ba // indirect
golang.org/x/text v0.3.4 // indirect
google.golang.org/genproto v0.0.0-20201113130914-ce600e9a6f9e // indirect
google.golang.org/grpc v1.33.2
google.golang.org/protobuf v1.25.0 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/rs/cors v1.7.0
github.com/spf13/cobra v1.1.3
github.com/stretchr/testify v1.7.0 // indirect
golang.org/x/net v0.0.0-20210119194325-5f4716e94777 // indirect
golang.org/x/sys v0.0.0-20210217105451-b926d437f341 // indirect
golang.org/x/text v0.3.5 // indirect
google.golang.org/genproto v0.0.0-20210212180131-e7f2df4ecc2d // indirect
google.golang.org/grpc v1.35.0
google.golang.org/protobuf v1.25.0
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
)
Loading