V
V
Vadim Rublev2021-05-31 23:21:36
go
Vadim Rublev, 2021-05-31 23:21:36

What is the correct way to write ListenAndServeTLS() with a custom servemux type?

Swears that there are many arguments in ListenAndServeTLS().
60b543aadf952695546146.png
How to write correctly? And why are there only two arguments in this case (in the documentation-syntax for ListenAndServeTLS()- 4)?
The code:

func main() {
    var mux = http.NewServeMux()
    mux.HandleFunc("/", indexPage)
    var serv = &http.Server {
        Addr:         serverPort,
        ReadTimeout:  15 * time.Second,
        WriteTimeout: 15 * time.Second,
    }
    log.Fatal(serv.ListenAndServeTLS(TLScert, TLSkey, mux))
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Mamonov, 2021-06-01
@Vadim Rublev

You call the ListenAndServeTLS method on the Server struct, it has only two parameters.
The four parameters of the ListenAndServeTLS method for the http package.
You need to add the Handler: mux parameter to the struct, and call the ListenAndServeTLS method of the Server struct. You should end up with something like this:

func main() {
    var mux = http.NewServeMux()
    mux.HandleFunc("/", indexPage)
    var serv = &http.Server{
        // добавляете параметр
        Handler:      mux,
        Addr:         serverPort,
        ReadTimeout:  15 * time.Second,
        WriteTimeout: 15 * time.Second,
    }
    // вызываете с двумя параметрами
    log.Fatal(serv.ListenAndServeTLS(TLScert, TLSkey))
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question