diff --git a/Dockerfile b/Dockerfile index a9191ca..f4f57da 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,11 +1,21 @@ -FROM golang:latest +FROM golang:alpine as builder -WORKDIR /server +RUN apk add make -COPY . . +WORKDIR /build -EXPOSE 8000 +COPY . /build + +RUN go mod tidy RUN make build -CMD ["./build/server"] \ No newline at end of file +FROM golang:alpine as runtime + +WORKDIR /server + +COPY --from=builder ./build/server /server/server + +EXPOSE 8000 + +CMD ["./build/server"] diff --git a/cmd/server/main.go b/cmd/server/main.go index daa21da..9f3ba23 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -1,11 +1,9 @@ package main import ( - "fmt" "net/http" "net/http/pprof" - connectcors "connectrpc.com/cors" "github.com/kavkaco/Kavka-Core/config" "github.com/kavkaco/Kavka-Core/database" repository_mongo "github.com/kavkaco/Kavka-Core/database/repo_mongo" @@ -19,10 +17,7 @@ import ( "github.com/kavkaco/Kavka-Core/pkg/email" "github.com/kavkaco/Kavka-Core/utils/hash" - "github.com/rs/cors" auth_manager "github.com/tahadostifam/go-auth-manager" - "golang.org/x/net/http2" - "golang.org/x/net/http2/h2c" ) func handleError(err error) { @@ -31,15 +26,6 @@ func handleError(err error) { } } -func handleCORS(allowedOrigins []string, h http.Handler) http.Handler { - return cors.New(cors.Options{ - AllowedOrigins: allowedOrigins, - AllowedMethods: []string{"POST"}, - AllowedHeaders: append(connectcors.AllowedHeaders(), []string{"X-Access-Token"}...), - AllowPrivateNetwork: true, - }).Handler(h) -} - func main() { // [=== Load Config ===] cfg := config.Read() @@ -115,23 +101,12 @@ func main() { } // [=== Init Grpc Server ===] - grpc.NewGrpcServer(router, &grpc.Services{ + err = grpc.NewGrpcServer(&cfg.HTTP, router, &grpc.Services{ AuthService: authService, ChatService: chatService, MessageService: messageService, SearchService: searchService, StreamSubscriber: streamSubscriber, }) - - // [=== Serve HTTP Server ===] - handler := handleCORS(cfg.HTTP.Cors.AllowOrigins, router) - server := &http.Server{ - Addr: fmt.Sprintf("%s:%d", cfg.HTTP.Host, cfg.HTTP.Port), - Handler: h2c.NewHandler(handler, &http2.Server{}), - ReadTimeout: 0, - WriteTimeout: 0, - IdleTimeout: 0, - } - err = server.ListenAndServe() handleError(err) } diff --git a/delivery/grpc/grpc_server.go b/delivery/grpc/grpc_server.go index 1be0dfb..2d23706 100644 --- a/delivery/grpc/grpc_server.go +++ b/delivery/grpc/grpc_server.go @@ -1,9 +1,12 @@ package grpc import ( + "fmt" "net/http" "connectrpc.com/connect" + connectcors "connectrpc.com/cors" + "github.com/kavkaco/Kavka-Core/config" grpc_handlers "github.com/kavkaco/Kavka-Core/delivery/grpc/handlers" "github.com/kavkaco/Kavka-Core/delivery/grpc/interceptor" "github.com/kavkaco/Kavka-Core/infra/stream" @@ -17,8 +20,20 @@ import ( "github.com/kavkaco/Kavka-ProtoBuf/gen/go/protobuf/events/v1/eventsv1connect" "github.com/kavkaco/Kavka-ProtoBuf/gen/go/protobuf/message/v1/messagev1connect" "github.com/kavkaco/Kavka-ProtoBuf/gen/go/protobuf/search/v1/searchv1connect" + "github.com/rs/cors" + "golang.org/x/net/http2" + "golang.org/x/net/http2/h2c" ) +func handleCORS(allowedOrigins []string, h http.Handler) http.Handler { + return cors.New(cors.Options{ + AllowedOrigins: allowedOrigins, + AllowedMethods: []string{"POST"}, + AllowedHeaders: append(connectcors.AllowedHeaders(), []string{"X-Access-Token"}...), + AllowPrivateNetwork: true, + }).Handler(h) +} + type Services struct { AuthService *auth.AuthService ChatService *chat.ChatService @@ -27,7 +42,7 @@ type Services struct { StreamSubscriber stream.StreamSubscriber } -func NewGrpcServer(router *http.ServeMux, services *Services) { +func NewGrpcServer(cfg *config.HTTP, router *http.ServeMux, services *Services) error { authInterceptor := interceptor.NewAuthInterceptor(services.AuthService) interceptors := connect.WithInterceptors(authInterceptor) @@ -46,9 +61,27 @@ func NewGrpcServer(router *http.ServeMux, services *Services) { searchGrpcHandler := grpc_handlers.NewSearchGrpcHandler(log.NewSubLogger("message-handler"), services.SearchService) searchGrpcRoute, searchGrpcRouter := searchv1connect.NewSearchServiceHandler(searchGrpcHandler, interceptors) + fmt.Println(authGrpcRoute) router.Handle(authGrpcRoute, authGrpcRouter) router.Handle(chatGrpcRoute, chatGrpcRouter) router.Handle(eventsGrpcRoute, eventsGrpcRouter) router.Handle(messageGrpcRoute, messageGrpcRouter) router.Handle(searchGrpcRoute, searchGrpcRouter) + + addr := fmt.Sprintf("%s:%d", cfg.Host, cfg.Port) + handler := handleCORS(cfg.Cors.AllowOrigins, router) + server := &http.Server{ + Addr: addr, + Handler: h2c.NewHandler(handler, &http2.Server{}), + ReadTimeout: 0, + WriteTimeout: 0, + IdleTimeout: 0, + } + fmt.Println("Grpc server is listening at ", addr) + err := server.ListenAndServe() + if err != nil { + return err + } + + return nil }