Skip to content

Commit

Permalink
perf: added http and grpc server in diff goroutines
Browse files Browse the repository at this point in the history
Signed-off-by: Sanskarzz <[email protected]>
  • Loading branch information
Sanskarzz committed Mar 13, 2024
1 parent 720c4ad commit f82174f
Showing 1 changed file with 23 additions and 11 deletions.
34 changes: 23 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,37 +9,49 @@ import (
"google.golang.org/grpc"
)

func startHTTPserver() {
http.HandleFunc("/", handler)
type Servers struct {
httpServer *http.Server
grpcServer *grpc.Server
}

err := http.ListenAndServe(":8080", nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
func NewServers() *Servers {
return &Servers{}
}

func (s *Servers) startHTTPServer() {

s.httpServer = &http.Server{
Addr: ":8080",
Handler: http.HandlerFunc(handler),
}
fmt.Println("Starting HTTP server on Port 8080")
if err := s.httpServer.ListenAndServe(); err != nil {
log.Fatal("ListenAndServe: ", err)
}
}

func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Hello World!")
}

func startGRPCServer() {
func (s *Servers) startGRPCServer() {

lis, err := net.Listen("tcp", ":9090")
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
grpcServer := grpc.NewServer()
s.grpcServer = grpc.NewServer()
fmt.Println("Starting GRPC server on Port 9090")
if err := grpcServer.Serve(lis); err != nil {
if err := s.grpcServer.Serve(lis); err != nil {
log.Fatalf("failed to serve: %v", err)
}

}

func main() {

startHTTPserver()
startGRPCServer()
srv := NewServers()
go srv.startHTTPServer()
go srv.startGRPCServer()
select {}

}

0 comments on commit f82174f

Please sign in to comment.