From c2d7c75acffe58b08217f43e0438e8750a41f32d Mon Sep 17 00:00:00 2001 From: darius Date: Thu, 16 May 2024 18:42:31 +0200 Subject: [PATCH] startup fix with goroutines --- Dockerfile | 1 + {routes => api}/apiRoutes.go | 4 ++-- common/docs/Dockerfile | 2 +- docker-compose.yml | 10 +++++----- go.mod | 12 ------------ go.work.sum | 14 ++++++++++++++ main.go | 29 ++++++++++++----------------- {routes => web}/webRoutes.go | 2 +- 8 files changed, 36 insertions(+), 38 deletions(-) rename {routes => api}/apiRoutes.go (86%) rename {routes => web}/webRoutes.go (97%) diff --git a/Dockerfile b/Dockerfile index 7828b04..f64d139 100644 --- a/Dockerfile +++ b/Dockerfile @@ -17,6 +17,7 @@ RUN go generate ./database/ent # Expose port 8080 for incoming traffic EXPOSE 4002 +EXPOSE 4001 # Define the command to run the app when the container starts CMD ["./portfolio"] \ No newline at end of file diff --git a/routes/apiRoutes.go b/api/apiRoutes.go similarity index 86% rename from routes/apiRoutes.go rename to api/apiRoutes.go index 572bf62..2b24215 100644 --- a/routes/apiRoutes.go +++ b/api/apiRoutes.go @@ -1,4 +1,4 @@ -package routes +package api import ( "net/http" @@ -11,7 +11,7 @@ func ApiRoutes() *http.ServeMux { mux := http.NewServeMux() // Register the routes and webHandler - mux.HandleFunc("/api/", handlers.CatchAllHandler) + mux.HandleFunc("/", handlers.CatchAllHandler) mux.HandleFunc("POST /api/user", handlers.CreateUser) mux.HandleFunc("GET /api/user/{id}", handlers.GetUser) mux.HandleFunc("POST /api/login", handlers.Login) diff --git a/common/docs/Dockerfile b/common/docs/Dockerfile index 7b3d3db..7aef232 100644 --- a/common/docs/Dockerfile +++ b/common/docs/Dockerfile @@ -12,7 +12,7 @@ RUN mkdir /opt/sources WORKDIR /opt/sources -COPY --from=openApiGen /go/common/docs/openApi.json ./Writerside +COPY --from=openApiGen /go/common/docs/openApi.json . ADD ./common/docs ./Writerside diff --git a/docker-compose.yml b/docker-compose.yml index b4c538e..9770c87 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -18,6 +18,7 @@ services: build: . ports: - "4002:4002" + - "4001:4001" restart: unless-stopped image: docker.dariusklein.nl/portfolio:latest depends_on: @@ -25,12 +26,11 @@ services: condition: service_healthy docs: - build: . + build: + context: . + dockerfile: ./common/docs/Dockerfile ports: - "4003:80" restart: unless-stopped - image: docker.dariusklein.nl/portfolio:latest - depends_on: - database: - condition: service_healthy + image: docker.dariusklein.nl/portfolio-docs:latest diff --git a/go.mod b/go.mod index 7ed85f7..d489dcd 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,6 @@ go 1.22 require ( entgo.io/ent v0.13.1 - github.com/a-h/rest v0.0.0-20240504113546-6729b3328f85 github.com/joho/godotenv v1.5.1 github.com/lib/pq v1.10.9 golang.org/x/crypto v0.23.0 @@ -14,24 +13,13 @@ require ( ariga.io/atlas v0.19.1-0.20240203083654-5948b60a8e43 // indirect github.com/agext/levenshtein v1.2.3 // indirect github.com/apparentlymart/go-textseg/v15 v15.0.0 // indirect - github.com/getkin/kin-openapi v0.124.0 // indirect github.com/go-openapi/inflect v0.19.0 // indirect - github.com/go-openapi/jsonpointer v0.21.0 // indirect - github.com/go-openapi/swag v0.23.0 // indirect github.com/google/go-cmp v0.6.0 // indirect github.com/google/uuid v1.6.0 // indirect github.com/hashicorp/hcl/v2 v2.19.1 // indirect - github.com/invopop/yaml v0.3.1 // indirect - github.com/josharian/intern v1.0.0 // indirect - github.com/mailru/easyjson v0.7.7 // indirect github.com/mitchellh/go-wordwrap v1.0.1 // indirect - github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect - github.com/perimeterx/marshmallow v1.1.5 // indirect github.com/zclconf/go-cty v1.14.2 // indirect - golang.org/x/exp v0.0.0-20240409090435-93d18d7e34b8 // indirect golang.org/x/mod v0.17.0 // indirect - golang.org/x/sync v0.7.0 // indirect golang.org/x/text v0.15.0 // indirect - golang.org/x/tools v0.20.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.work.sum b/go.work.sum index 8c603c3..dccd8de 100644 --- a/go.work.sum +++ b/go.work.sum @@ -4,16 +4,21 @@ github.com/apparentlymart/go-textseg/v13 v13.0.0 h1:Y+KvPE1NYz0xl601PVImeQfFyEy6 github.com/apparentlymart/go-textseg/v13 v13.0.0/go.mod h1:ZK2fH7c4NqDTLtiYLvIkEghdlcqw7yxLeM89kiTRPUo= github.com/go-chi/chi/v5 v5.0.12 h1:9euLV5sTrTNTRUU9POmDUvfxyj6LAABLUcEWO+JJb4s= github.com/go-chi/chi/v5 v5.0.12/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8= +github.com/go-test/deep v1.0.3 h1:ZrJSEWsXzPOxaZnFteGEfooLba+ju3FYIbOrS+rQd68= +github.com/go-test/deep v1.0.3/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA= github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da h1:oI5xCqsCo564l8iNU+DwB5epxmsaqB+rhGL0m5jtYqE= github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY= github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ= github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc= github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= +github.com/hashicorp/hcl/v2 v2.13.0/go.mod h1:e4z5nxYlWNPdDSNYX+ph14EvWYMFm3eP0zIUqPc2jr0= github.com/jessevdk/go-flags v1.5.0 h1:1jKYvbxEjfUl0fmqTCOfonvskHHXMjBySTLW4y9LFvc= github.com/jessevdk/go-flags v1.5.0/go.mod h1:Fw0T6WPc1dYxT4mKEZRfG5kJhaTDP9pj1c2EWnYs/m4= github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= +github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= +github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY= github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= @@ -22,6 +27,8 @@ github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9G github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8= +github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/vmihailenco/msgpack/v5 v5.3.5 h1:5gO0H1iULLWGhs2H5tbAHIZTV8/cYafcFOr9znI5mJU= github.com/vmihailenco/msgpack/v5 v5.3.5/go.mod h1:7xyJ9e+0+9SaZT0Wt1RGleJXzli6Q/V5KbhBonMG9jc= github.com/vmihailenco/tagparser/v2 v2.0.0 h1:y09buUbR+b5aycVFQs/g70pqKVZNBmxwAhO7/IwNM9g= @@ -32,11 +39,18 @@ github.com/zclconf/go-cty-debug v0.0.0-20191215020915-b22d67c1ba0b h1:FosyBZYxY3 github.com/zclconf/go-cty-debug v0.0.0-20191215020915-b22d67c1ba0b/go.mod h1:ZRKQfBXbGkpdV6QMzT3rU1kSTAnfu1dO8dPKjYprgj8= go.opencensus.io v0.24.0 h1:y73uSU6J157QMP2kn2r30vwW1A2W2WFwSCGnAVxeaD0= go.opencensus.io v0.24.0/go.mod h1:vNK8G9p7aAivkbmorf4v+7Hgx+Zs0yY+0fOtgBfjQKo= +golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4= +golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44= golang.org/x/net v0.24.0 h1:1PcaxkF854Fu3+lvBIx5SYn9wRlBzzcnHZSiaFFAb0w= golang.org/x/net v0.24.0/go.mod h1:2Q7sJY5mzlzWjKtYUEXSlBWCdyaioyXzRB2RtU8KVE8= +golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ= +golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y= golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/telemetry v0.0.0-20240228155512-f48c80bd79b2 h1:IRJeR9r1pYWsHKTRe/IInb7lYvbBVIqOgsX/u0mbOWY= golang.org/x/telemetry v0.0.0-20240228155512-f48c80bd79b2/go.mod h1:TeRTkGYfJXctD9OcfyVLyj2J3IxLnKwHJR8f4D8a3YE= golang.org/x/term v0.20.0 h1:VnkxpohqXaOBYJtBmEppKUG6mXpi+4O6purfc2+sMhw= golang.org/x/term v0.20.0/go.mod h1:8UkIAJTvZgivsXaD6/pH6U9ecQzZ45awqEOzuCvwpFY= +golang.org/x/tools v0.18.0 h1:k8NLag8AGHnn+PHbl7g43CtqZAwG60vZkLqgyZgIHgQ= +golang.org/x/tools v0.18.0/go.mod h1:GL7B4CwcLLeo59yx/9UWWuNOW1n3VZ4f5axWfML7Lcg= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= diff --git a/main.go b/main.go index 5061aa5..83f71c2 100644 --- a/main.go +++ b/main.go @@ -4,8 +4,9 @@ import ( "github.com/joho/godotenv" "log" "net/http" + "portfolio/api" "portfolio/database" - "portfolio/routes" + "portfolio/web" ) func main() { @@ -16,23 +17,17 @@ func main() { return } - //init web routes - web := routes.WebRoutes() - //init api routes - api := routes.ApiRoutes() - //connect to database and migrate database.DB() - // Run the server - err = http.ListenAndServe(":4001", web) - if err != nil { - log.Fatalf("web failed to start: %v", err) - return - } - err = http.ListenAndServe(":4002", api) - if err != nil { - log.Fatalf("api failed to start: %v", err) - return - } + //init web routes + web := web.WebRoutes() + // Run web server + go http.ListenAndServe(":4001", web) + + //init api routes + api := api.ApiRoutes() + //run api server + http.ListenAndServe(":4002", api) + } diff --git a/routes/webRoutes.go b/web/webRoutes.go similarity index 97% rename from routes/webRoutes.go rename to web/webRoutes.go index 023eaf2..c6adfb5 100644 --- a/routes/webRoutes.go +++ b/web/webRoutes.go @@ -1,4 +1,4 @@ -package routes +package web import ( "net/http"