Gin: Run with custom TLSConfig

Created on 5 Sep 2017  路  9Comments  路  Source: gin-gonic/gin

Is it even possible? With bare http:

server := http.Server{
    Addr:      cfg.ListenHttps,
    Handler:   http.HandlerFunc(handler),
    TLSConfig: tlsConf,
}
server.ListenAndServeTLS("", "")

Most helpful comment

I think you can do this code like in main function:

    r := gin.Default()
    server := http.Server{
        Addr:      addr,
        Handler:   r,
        TLSConfig: tlsConfig,
    }
    err = server.ListenAndServeTLS("", "")
}

All 9 comments

@hryamzik Yes it is possible. You can find it here : https://golang.org/pkg/net/http/#Server.ListenAndServeTLS

Filenames containing a certificate and matching private key for the server must be provided if neither the Server's TLSConfig.Certificates nor TLSConfig.GetCertificate are populated

@aghasoroush I'm able to do this with net.http, the question is how to serve in the same way with gin?

Hi @hryamzik the example?

@thinkerou actually the question is how to run with multiple certificates, like here.

I'm looking to do the same thing. RunTLS uses http.ListenAndServeTLS which does not allow for the setting of TLSConfig.

https://github.com/gin-gonic/gin/blob/master/gin.go#L290

It would be nice if RunTLS used the Server.ListenAndServeTLS variant so that TLSConfig can be set.

https://golang.org/pkg/net/http/#Server.ListenAndServeTLS

Without this it's impossible to set ClientAuthType for TLS Client Authentication.

@hryamzik because ServeTLS only set one cert/key pair, it not support multiple certificates, see:
https://github.com/golang/go/blob/master/src/net/http/server.go#L2786

@thinkerou it's actually possible with net/http

I think you can do this code like in main function:

    r := gin.Default()
    server := http.Server{
        Addr:      addr,
        Handler:   r,
        TLSConfig: tlsConfig,
    }
    err = server.ListenAndServeTLS("", "")
}

Can't check right now but if gin can be just set as a handler that should work, thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

boneq picture boneq  路  3Comments

xpbliss picture xpbliss  路  3Comments

iiinsomnia picture iiinsomnia  路  3Comments

nxvl picture nxvl  路  3Comments

frederikhors picture frederikhors  路  3Comments