Skip to content

Commit

Permalink
Reduce Docker Image Size (#146)
Browse files Browse the repository at this point in the history
  • Loading branch information
tahadostifam authored Sep 22, 2024
1 parent 78aad09 commit 8448b04
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 32 deletions.
20 changes: 15 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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"]
FROM golang:alpine as runtime

WORKDIR /server

COPY --from=builder ./build/server /server/server

EXPOSE 8000

CMD ["./build/server"]
27 changes: 1 addition & 26 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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) {
Expand All @@ -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()
Expand Down Expand Up @@ -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)
}
35 changes: 34 additions & 1 deletion delivery/grpc/grpc_server.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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
Expand All @@ -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)

Expand All @@ -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
}

0 comments on commit 8448b04

Please sign in to comment.