1
0
mirror of https://github.com/muety/wakapi.git synced 2023-08-10 21:12:56 +03:00

feat: add ipv6 and tls support (resolve #79)

This commit is contained in:
Ferdinand Mütsch
2020-12-12 22:07:00 +01:00
parent 439a87dec9
commit 72e42a9c42
6 changed files with 258 additions and 197 deletions

57
main.go
View File

@@ -1,6 +1,7 @@
package main
import (
"fmt"
"github.com/gorilla/handlers"
conf "github.com/muety/wakapi/config"
"github.com/muety/wakapi/migrations/common"
@@ -178,15 +179,55 @@ func main() {
router.PathPrefix("/assets").Handler(http.FileServer(http.Dir("./static")))
// Listen HTTP
portString := config.Server.ListenIpV4 + ":" + strconv.Itoa(config.Server.Port)
s := &http.Server{
Handler: router,
Addr: portString,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
listen(router)
}
func listen(handler http.Handler) {
var s4, s6 *http.Server
// IPv4
if config.Server.ListenIpV4 != "" {
bindString4 := config.Server.ListenIpV4 + ":" + strconv.Itoa(config.Server.Port)
s4 = &http.Server{
Handler: handler,
Addr: bindString4,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
}
}
log.Printf("Listening on %+s\n", portString)
s.ListenAndServe()
// IPv6
if config.Server.ListenIpV6 != "" {
bindString6 := "[" + config.Server.ListenIpV6 + "]:" + strconv.Itoa(config.Server.Port)
s6 = &http.Server{
Handler: handler,
Addr: bindString6,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
}
}
if config.UseTLS() {
if s4 != nil {
fmt.Printf("Listening for HTTPS on %s.\n", s4.Addr)
go s4.ListenAndServeTLS(config.Server.TlsCertPath, config.Server.TlsKeyPath)
}
if s6 != nil {
fmt.Printf("Listening for HTTPS on %s.\n", s6.Addr)
go s6.ListenAndServeTLS(config.Server.TlsCertPath, config.Server.TlsKeyPath)
}
} else {
if s4 != nil {
fmt.Printf("Listening for HTTP on %s.\n", s4.Addr)
go s4.ListenAndServe()
}
if s6 != nil {
fmt.Printf("Listening for HTTP on %s.\n", s6.Addr)
go s6.ListenAndServe()
}
}
<-make(chan interface{}, 1)
}
func runDatabaseMigrations() {