1
0
mirror of https://github.com/kataras/iris.git synced 2026-01-09 04:51:56 +00:00

Parse server's addr before use it

This commit is contained in:
Makis Maropoulos
2016-07-07 17:17:34 +02:00
parent 981fef9ecf
commit e3b2c68085
5 changed files with 100 additions and 56 deletions

View File

@@ -3,6 +3,7 @@ package config
import (
"os"
"strconv"
"strings"
"github.com/imdario/mergo"
"github.com/kataras/fasthttp"
@@ -51,6 +52,37 @@ type Server struct {
Virtual bool
}
// ServerParseAddr parses the listening addr and returns this
func ServerParseAddr(listeningAddr string) string {
// check if addr has :port, if not do it +:80 ,we need the hostname for many cases
a := listeningAddr
if a == "" {
// check for os environments
if oshost := os.Getenv("HOST"); oshost != "" {
a = oshost
} else if oshost := os.Getenv("ADDR"); oshost != "" {
a = oshost
} else if osport := os.Getenv("PORT"); osport != "" {
a = ":" + osport
}
if a == "" {
a = DefaultServerAddr
}
}
if portIdx := strings.IndexByte(a, ':'); portIdx == 0 {
// if contains only :port ,then the : is the first letter, so we dont have setted a hostname, lets set it
a = DefaultServerHostname + a
}
if portIdx := strings.IndexByte(a, ':'); portIdx < 0 {
// missing port part, add it
a = a + ":80"
}
return a
}
// DefaultServer returns the default configs for the server
func DefaultServer() Server {
return Server{ListeningAddr: DefaultServerAddr,