Why it gives the error 'socket hang up' if you use http.Server?

Viewed 48

At first I started http like this:

port := fmt.Sprintf(":%v", a.cfg.Port)
log.Fatal(http.ListenAndServe(port, a.router))

And it worked great and accepted requests.

Then I decided to replace this with http.Server:

server := &http.Server{
        Handler:      a.router,
        WriteTimeout: 15 * time.Second,
        ReadTimeout:  15 * time.Second,
}
listener, err := net.Listen("tcp", ":8080")
a.checkErr(err)
if err := server.Serve(listener); err != nil {
        switch {
        case errors.Is(err, http.ErrServerClosed):
            a.logger.Warn("server shutdown")
        default:
            a.logger.Fatal(err)
        }
}

And now when I start the program and send a request, I get : Error: socket hang up

I run the application itself through docker:

services:
    redis:
        image: redis:6.2
        volumes:
          - /var/run/docker.sock:/var/run/docker.sock      
        ports:
          - 6379:6379 
    app:
        build: ./
        ports:
          - 8080:8080
        depends_on:
          - redis

I tried to find a solution, but everything I tried doesn't help. I can't understand why the requests stopped being accepted after the replacement? I will be glad if you help me understand what the problem is.

0 Answers
Related