Added webserver tests
This commit is contained in:
parent
16c5e37a8c
commit
3492884cb1
|
@ -0,0 +1,87 @@
|
|||
package config
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"os/user"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
|
||||
"github.com/creasty/defaults"
|
||||
"gopkg.in/yaml.v3"
|
||||
"scm.yoorie.de/go-lib/util"
|
||||
)
|
||||
|
||||
func GetConfigurationFiles(appName string) []string {
|
||||
// First checking local config
|
||||
executablePath, _ := os.Executable()
|
||||
fname := filepath.Base(executablePath)
|
||||
fext := filepath.Ext(executablePath)
|
||||
fdir := filepath.Dir(executablePath)
|
||||
fnameWoExt := fname[0 : len(fname)-len(fext)]
|
||||
ymlExts := [2]string{".yml", ".yaml"}
|
||||
fileNames := make([]string, 0, 4)
|
||||
for _, ext := range ymlExts {
|
||||
fileNames = append(fileNames, filepath.Join(fdir, fnameWoExt+ext))
|
||||
}
|
||||
usr, _ := user.Current()
|
||||
fileNames = append(fileNames, filepath.Join(usr.HomeDir, "."+appName, "config"))
|
||||
// Check for global config
|
||||
fileNames = append(fileNames, util.GetGlobalConfigurationFile(appName, "config"))
|
||||
return fileNames
|
||||
}
|
||||
|
||||
func GetConfigurationFile(appName string) string {
|
||||
// First checking local config
|
||||
files := GetConfigurationFiles(appName)
|
||||
for _, localConfigFile := range files {
|
||||
if util.FileExists(localConfigFile) {
|
||||
return localConfigFile
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func LoadConfiguration[T any](config T) error {
|
||||
return LoadConfigurationFromFile(config, "")
|
||||
}
|
||||
|
||||
func IsStructPointer[T any](config T) bool {
|
||||
value := reflect.ValueOf(config)
|
||||
return value.Type().Kind() == reflect.Pointer && reflect.Indirect(value).Type().Kind() == reflect.Struct
|
||||
}
|
||||
|
||||
func LoadConfigurationFromFile[T any](config T, configFile string) error {
|
||||
if !IsStructPointer(config) {
|
||||
genericType := reflect.TypeOf(config)
|
||||
return errors.New(fmt.Sprintf("Type: %s.%s is not a struct pointer", genericType.PkgPath(), genericType.Name()))
|
||||
}
|
||||
defaults.Set(config)
|
||||
if configFile != "" {
|
||||
if !util.FileExists(configFile) {
|
||||
return errors.New(fmt.Sprintf("given configuration file %s cannot be found", configFile))
|
||||
}
|
||||
} else {
|
||||
if method := reflect.ValueOf(config).MethodByName("appName"); !method.IsNil() {
|
||||
result := method.String()
|
||||
if result != "" {
|
||||
configFile = GetConfigurationFile(result)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if configFile == "" {
|
||||
return nil
|
||||
}
|
||||
ymldata, err := ioutil.ReadFile(configFile)
|
||||
if err != nil {
|
||||
return errors.New(fmt.Sprintf("cannot read configuration file %s. %v", configFile, err))
|
||||
}
|
||||
err = yaml.Unmarshal(ymldata, config)
|
||||
if err != nil {
|
||||
return errors.New(fmt.Sprintf("error unmarshalling configuration data: %v", err))
|
||||
}
|
||||
return nil
|
||||
}
|
23
go.mod
23
go.mod
|
@ -1,11 +1,32 @@
|
|||
module scm.yoorie.de/go-lib/micro
|
||||
|
||||
go 1.16
|
||||
go 1.18
|
||||
|
||||
require (
|
||||
github.com/creasty/defaults v1.5.2
|
||||
github.com/go-chi/chi v1.5.4
|
||||
github.com/go-chi/cors v1.2.0
|
||||
github.com/go-chi/render v1.0.1
|
||||
github.com/stretchr/testify v1.7.1
|
||||
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
|
||||
scm.yoorie.de/go-lib/certs v0.0.1
|
||||
scm.yoorie.de/go-lib/gelf v0.0.1
|
||||
scm.yoorie.de/go-lib/util v0.0.4
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||
github.com/google/uuid v1.3.0 // indirect
|
||||
github.com/kr/pretty v0.1.0 // indirect
|
||||
github.com/mattn/go-colorable v0.1.12 // indirect
|
||||
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d // indirect
|
||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||
github.com/sergi/go-diff v1.2.0 // indirect
|
||||
golang.org/x/crypto v0.0.0-20220331220935-ae2d96664a29 // indirect
|
||||
golang.org/x/net v0.0.0-20220403103023-749bd193bc2b // indirect
|
||||
golang.org/x/sys v0.0.0-20220405052023-b1e9470b6e64 // indirect
|
||||
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect
|
||||
gopkg.in/aphistic/golf.v0 v0.0.0-20180712155816-02c07f170c5a // indirect
|
||||
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
|
||||
gopkg.in/yaml.v2 v2.4.0 // indirect
|
||||
)
|
||||
|
|
140
go.sum
140
go.sum
|
@ -1,134 +1,66 @@
|
|||
github.com/aphistic/sweet v0.3.0 h1:xZTMfCoMsjWubPNxOBODluBC4qfGP0CdRJ88jon46XE=
|
||||
github.com/aphistic/sweet v0.3.0/go.mod h1:fWDlIh/isSE9n6EPsRmC0det+whmX6dJid3stzu0Xys=
|
||||
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
|
||||
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
|
||||
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
|
||||
github.com/creasty/defaults v1.5.2 h1:/VfB6uxpyp6h0fr7SPp7n8WJBoV8jfxQXPCnkVSjyls=
|
||||
github.com/creasty/defaults v1.5.2/go.mod h1:FPZ+Y0WNrbqOVw+c6av63eyHUAl6pMHZwqLPvXUZGfY=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
|
||||
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
|
||||
github.com/go-chi/chi v1.5.4 h1:QHdzF2szwjqVV4wmByUnTcsbIg7UGaQ0tPF2t5GcAIs=
|
||||
github.com/go-chi/chi v1.5.4/go.mod h1:uaf8YgoFazUOkPBG7fxPftUylNumIev9awIWOENIuEg=
|
||||
github.com/go-chi/cors v1.2.0 h1:tV1g1XENQ8ku4Bq3K9ub2AtgG+p16SmzeMSGTwrOKdE=
|
||||
github.com/go-chi/cors v1.2.0/go.mod h1:sSbTewc+6wYHBBCW7ytsFSn836hqM7JxpglAy2Vzc58=
|
||||
github.com/go-chi/render v1.0.1 h1:4/5tis2cKaNdnv9zFLfXzcquC9HbeZgCnxGnKrltBS8=
|
||||
github.com/go-chi/render v1.0.1/go.mod h1:pq4Rr7HbnsdaeHagklXub+p6Wd16Af5l9koip1OvJns=
|
||||
github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0/go.mod h1:fyg7847qk6SyHyPtNmDHnmrv/HOrqktSC+C9fM+CJOE=
|
||||
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
||||
github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8=
|
||||
github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA=
|
||||
github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs=
|
||||
github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w=
|
||||
github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0=
|
||||
github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI=
|
||||
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
|
||||
github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
|
||||
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
|
||||
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
|
||||
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||
github.com/google/go-cmp v0.5.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o=
|
||||
github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE=
|
||||
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
|
||||
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
|
||||
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
|
||||
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
|
||||
github.com/mattn/go-colorable v0.1.1 h1:G1f5SKeVxmagw/IyvzvtZE4Gybcc4Tr1tf7I8z0XgOg=
|
||||
github.com/mattn/go-colorable v0.1.1/go.mod h1:FuOcm+DKB9mbwrcAfNl7/TZVBZ6rcnceauSikq3lYCQ=
|
||||
github.com/mattn/go-isatty v0.0.5 h1:tHXDdz1cpzGaovsTB+TVB8q90WEokoVmfMqoVcrLUgw=
|
||||
github.com/mattn/go-isatty v0.0.5/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
|
||||
github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b h1:j7+1HpAFS1zy5+Q4qx1fWh90gTKwiN4QCGoY9TWyyO4=
|
||||
github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b/go.mod h1:01TrycV0kFyexm33Z7vhZRXopbI8J3TDReVlkTgMUxE=
|
||||
github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A=
|
||||
github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU=
|
||||
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
|
||||
github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108oapk=
|
||||
github.com/onsi/ginkgo v1.16.4/go.mod h1:dX+/inL/fNMqNlz0e9LfyB9TswhZpCVdJM/Z6Vvnwo0=
|
||||
github.com/onsi/ginkgo/v2 v2.1.3/go.mod h1:vw5CSIxN1JObi/U8gcbwft7ZxR2dgaR70JSE3/PpL4c=
|
||||
github.com/onsi/gomega v1.5.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
|
||||
github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY=
|
||||
github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo=
|
||||
github.com/onsi/gomega v1.17.0/go.mod h1:HnhC7FXeEQY45zxNK3PPoIUhzk/80Xly9PcubAlGdZY=
|
||||
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
|
||||
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
|
||||
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
||||
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
|
||||
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
||||
github.com/mattn/go-colorable v0.1.12 h1:jF+Du6AlPIjs2BiUiQlKOX0rt3SujHxPnksPKZbaA40=
|
||||
github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
|
||||
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
|
||||
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
|
||||
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d h1:5PJl274Y63IEHC+7izoQE9x6ikvDFZS2mDVS3drnohI=
|
||||
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d/go.mod h1:01TrycV0kFyexm33Z7vhZRXopbI8J3TDReVlkTgMUxE=
|
||||
github.com/onsi/gomega v1.19.0 h1:4ieX6qQjPP/BfC3mpsAtIGGlxTWPeA3Inl/7DtXw1tw=
|
||||
github.com/onsi/gomega v1.19.0/go.mod h1:LY+I3pBVzYsTBU1AnDwOSxaYi9WoWiqgwooUqq9yPro=
|
||||
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
||||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/sergi/go-diff v1.0.0 h1:Kpca3qRNrduNnOQeazBd0ysaKrUJiIuISHxogkT9RPQ=
|
||||
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
|
||||
github.com/sergi/go-diff v1.2.0 h1:XU+rvMAioB0UC3q1MFrIQy4Vo5/4VsRDQQXHsEya6xQ=
|
||||
github.com/sergi/go-diff v1.2.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
|
||||
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20190426145343-a29dc8fdc734/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
||||
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
||||
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 h1:psW17arqaxU48Z5kZ0CQnkZWQJsqcURM6tKiBApRjXI=
|
||||
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
||||
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
|
||||
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.0.0-20200520004742-59133d7f0dd7/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
|
||||
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
|
||||
golang.org/x/net v0.0.0-20210428140749-89ef3d95e781/go.mod h1:OJAsFXCWl8Ukc7SiCT/9KSuxbyM7479/AVlXFRxuMCk=
|
||||
golang.org/x/net v0.0.0-20220225172249-27dd8689420f h1:oA4XRj0qtSt8Yo1Zms0CUlsT3KG69V2UGQWPBxujDmc=
|
||||
golang.org/x/net v0.0.0-20220225172249-27dd8689420f/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
|
||||
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20190904154756-749cb33beabd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20191204072324-ce4227a45e2e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210112080510-489259a85091/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e h1:fLOSk5Q00efkSvAm+4xcoXD+RRmLmmulPn5I3Y9F2EM=
|
||||
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
|
||||
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
|
||||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
golang.org/x/crypto v0.0.0-20220331220935-ae2d96664a29 h1:tkVvjkPTB7pnW3jnid7kNyAMPVWllTNOf/qKDze4p9o=
|
||||
golang.org/x/crypto v0.0.0-20220331220935-ae2d96664a29/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
|
||||
golang.org/x/net v0.0.0-20220403103023-749bd193bc2b h1:vI32FkLJNAWtGD4BwkThwEy6XS7ZLLMHkSkYfF8M0W0=
|
||||
golang.org/x/net v0.0.0-20220403103023-749bd193bc2b/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
|
||||
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220405052023-b1e9470b6e64 h1:D1v9ucDTYBtbz5vNuBbAhIMAGhQhJ6Ym5ah3maMVNX4=
|
||||
golang.org/x/sys v0.0.0-20220405052023-b1e9470b6e64/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 h1:JGgROgKl9N8DuW20oFS5gxc+lE67/N3FcwmBPMe7ArY=
|
||||
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
|
||||
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||
golang.org/x/tools v0.0.0-20201224043029-2b0845dc783e/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
|
||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8=
|
||||
google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0=
|
||||
google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM=
|
||||
google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE=
|
||||
google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo=
|
||||
google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
|
||||
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
|
||||
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
|
||||
gopkg.in/aphistic/golf.v0 v0.0.0-20180712155816-02c07f170c5a h1:34vqlRjuZiE9c8eHsuZ9nn+GbcimFpvGUEmW+vyfhG8=
|
||||
gopkg.in/aphistic/golf.v0 v0.0.0-20180712155816-02c07f170c5a/go.mod h1:fvTxI2ZW4gO1d+4q4VCKOo+ANBs4gPN3IW00MlCumKc=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
|
||||
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
|
||||
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
|
||||
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
|
||||
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
|
||||
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo=
|
||||
gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw=
|
||||
scm.yoorie.de/go-lib/certs v0.0.1 h1:4FwWaIjtMQbhpr75kVD7Ymvz5fwpeM5kt1jrjrIHFsg=
|
||||
scm.yoorie.de/go-lib/certs v0.0.1/go.mod h1:CYe8HvRaply1NRTQ9YnllvdijoZwNR4yXGhrN1L9BhM=
|
||||
scm.yoorie.de/go-lib/gelf v0.0.1 h1:VcCUihF1zyOiDfPg2NWLyaBoJjundua624RBAPJx/vU=
|
||||
scm.yoorie.de/go-lib/gelf v0.0.1/go.mod h1:p4HaHQX4mcgclzYB8nomFHvYLLdz64yr7MYlV5+Oz/I=
|
||||
scm.yoorie.de/go-lib/util v0.0.4 h1:0gyUM4689jwrwzeQFAY8mF74YO68apznIhSxhYI3pxA=
|
||||
scm.yoorie.de/go-lib/util v0.0.4/go.mod h1:ckOH3jMooqDIaksOVCFIx/2S8aTDlXuBYfIKsLwznIU=
|
||||
|
|
|
@ -0,0 +1,82 @@
|
|||
package test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
cfg "scm.yoorie.de/go-lib/micro/config"
|
||||
)
|
||||
|
||||
type MyConfig struct {
|
||||
Value string `default:"Bar" yaml:"value"`
|
||||
Value1 bool `default:"false" yaml:"value1"`
|
||||
Value2 int `default:"8080" yaml:"value2"`
|
||||
}
|
||||
|
||||
func (config *MyConfig) appName() string {
|
||||
return "myconfig"
|
||||
}
|
||||
|
||||
func TestIsStructPointerStruct(t *testing.T) {
|
||||
assert.False(t, cfg.IsStructPointer(MyConfig{}))
|
||||
}
|
||||
func TestIsStructPointerString(t *testing.T) {
|
||||
assert.False(t, cfg.IsStructPointer("Bar"))
|
||||
}
|
||||
|
||||
func TestIsStructPointerSimpleArray(t *testing.T) {
|
||||
assert.False(t, cfg.IsStructPointer([2]int{292, 2}))
|
||||
}
|
||||
|
||||
func TestIsStructPointerInt(t *testing.T) {
|
||||
assert.False(t, cfg.IsStructPointer(12))
|
||||
}
|
||||
|
||||
func TestIsStructPointerBool(t *testing.T) {
|
||||
assert.False(t, cfg.IsStructPointer(false))
|
||||
}
|
||||
|
||||
func TestIsStructPointerStructPointer(t *testing.T) {
|
||||
assert.True(t, cfg.IsStructPointer(&MyConfig{}))
|
||||
}
|
||||
|
||||
func TestIsStructPointerStructPointerArray(t *testing.T) {
|
||||
cfg1 := &MyConfig{
|
||||
Value: "Foo",
|
||||
}
|
||||
cfg2 := &MyConfig{
|
||||
Value: "Bar",
|
||||
}
|
||||
array := [2]*MyConfig{cfg1, cfg2}
|
||||
assert.False(t, cfg.IsStructPointer(array))
|
||||
}
|
||||
|
||||
func TestLoadConfigurationFromFileWithNoPointerInvalidType(t *testing.T) {
|
||||
myConfig := MyConfig{}
|
||||
err := cfg.LoadConfigurationFromFile(myConfig, "testdata/config.yml")
|
||||
assert.NotNil(t, err)
|
||||
}
|
||||
|
||||
func TestLoadConfigurationFromFile(t *testing.T) {
|
||||
myConfig := &MyConfig{}
|
||||
assert.Equal(t, "myconfig", myConfig.appName())
|
||||
cfg.LoadConfigurationFromFile(myConfig, "testdata/config.yml")
|
||||
assert.Equal(t, "Foo", myConfig.Value)
|
||||
assert.Equal(t, true, myConfig.Value1)
|
||||
assert.Equal(t, 8080, myConfig.Value2)
|
||||
}
|
||||
func TestLoadConfigurationFromFileNotExist(t *testing.T) {
|
||||
myConfig := &MyConfig{}
|
||||
assert.Equal(t, "myconfig", myConfig.appName())
|
||||
err := cfg.LoadConfigurationFromFile(myConfig, "testdata/config1.yml")
|
||||
assert.NotNil(t, err)
|
||||
}
|
||||
|
||||
func TestGetConfigurationFiles(t *testing.T) {
|
||||
fileNames := cfg.GetConfigurationFiles("myapp")
|
||||
assert.Equal(t, 4, len(fileNames))
|
||||
|
||||
for _, fileName := range fileNames {
|
||||
t.Log(fileName)
|
||||
}
|
||||
}
|
|
@ -0,0 +1,2 @@
|
|||
value : 'Foo'
|
||||
value1 : true
|
|
@ -0,0 +1,155 @@
|
|||
package test
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/go-chi/chi"
|
||||
"github.com/go-chi/render"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"scm.yoorie.de/go-lib/micro/web"
|
||||
"scm.yoorie.de/go-lib/util"
|
||||
)
|
||||
|
||||
var (
|
||||
internalCount int
|
||||
httpPort int
|
||||
sslPort int
|
||||
)
|
||||
|
||||
type MyData struct {
|
||||
Message string `json:"message"`
|
||||
Count int `json:"count"`
|
||||
}
|
||||
|
||||
func MyTestEndpoint(w http.ResponseWriter, r *http.Request) {
|
||||
internalCount++
|
||||
render.JSON(w, r, &MyData{
|
||||
Message: "A message",
|
||||
Count: internalCount,
|
||||
})
|
||||
}
|
||||
|
||||
func CreateTestRouter() *chi.Mux {
|
||||
router := chi.NewRouter()
|
||||
router.Get("/myendpoint", MyTestEndpoint)
|
||||
return router
|
||||
}
|
||||
|
||||
// GetFreePort asks the kernel for a free open port that is ready to use.
|
||||
func GetFreePort() (int, error) {
|
||||
addr, err := net.ResolveTCPAddr("tcp", "localhost:0")
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
l, err := net.ListenTCP("tcp", addr)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
defer l.Close()
|
||||
return l.Addr().(*net.TCPAddr).Port, nil
|
||||
}
|
||||
|
||||
func getServerURL(ssl bool, path string) string {
|
||||
var scheme string
|
||||
var port int
|
||||
if ssl {
|
||||
scheme = "https"
|
||||
port = sslPort
|
||||
} else {
|
||||
scheme = "http"
|
||||
port = httpPort
|
||||
}
|
||||
return util.JoiningSlash(fmt.Sprintf("%s://localhost:%d", scheme, port), path)
|
||||
}
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
internalCount = 0
|
||||
var err error
|
||||
httpPort, err = GetFreePort()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
sslPort, err = GetFreePort()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
config := &web.WebServerConfiguration{
|
||||
Port: httpPort,
|
||||
SslPort: sslPort,
|
||||
}
|
||||
server, err := web.NewWebServer(config)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
server.Mount("/api", CreateTestRouter())
|
||||
server.Start()
|
||||
|
||||
// Allow insecure calls to https
|
||||
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
|
||||
|
||||
exitVal := m.Run()
|
||||
server.Stop()
|
||||
os.Exit(exitVal)
|
||||
}
|
||||
|
||||
func TestReady(t *testing.T) {
|
||||
uri := getServerURL(false, "/readyz")
|
||||
resp, err := http.Get(uri)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, http.StatusOK, resp.StatusCode)
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
healthData := web.HealthData{}
|
||||
err = json.Unmarshal(body, &healthData)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, "service is ready", healthData.Message)
|
||||
assert.NotEmpty(t, healthData.LastChecked)
|
||||
}
|
||||
|
||||
func TestReadySSL(t *testing.T) {
|
||||
uri := getServerURL(true, "/health/readyz")
|
||||
resp, err := http.Get(uri)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, http.StatusOK, resp.StatusCode)
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
healthData := web.HealthData{}
|
||||
err = json.Unmarshal(body, &healthData)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, "service is ready", healthData.Message)
|
||||
assert.NotEmpty(t, healthData.LastChecked)
|
||||
}
|
||||
|
||||
func TestHealthy(t *testing.T) {
|
||||
resp, err := http.Get(getServerURL(false, "/healthz"))
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, http.StatusOK, resp.StatusCode)
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
healthData := web.HealthData{}
|
||||
err = json.Unmarshal(body, &healthData)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, "service up and running", healthData.Message)
|
||||
assert.NotEmpty(t, healthData.LastChecked)
|
||||
}
|
||||
|
||||
func TestSslEndpoint(t *testing.T) {
|
||||
uri := getServerURL(true, "/api/myendpoint")
|
||||
resp, err := http.Get(uri)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, http.StatusOK, resp.StatusCode)
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
assert.Nil(t, err)
|
||||
t.Log(fmt.Sprintf("Body: %s", string(body)))
|
||||
myData := MyData{}
|
||||
err = json.Unmarshal(body, &myData)
|
||||
assert.Nil(t, err)
|
||||
assert.NotNil(t, myData)
|
||||
assert.Equal(t, "A message", myData.Message)
|
||||
assert.Greater(t, myData.Count, 0)
|
||||
}
|
|
@ -5,7 +5,7 @@ import (
|
|||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/go-chi/chi"
|
||||
"github.com/go-chi/render"
|
||||
log "scm.yoorie.de/go-lib/gelf"
|
||||
)
|
||||
|
||||
|
@ -16,6 +16,7 @@ type healthChecker struct {
|
|||
message string
|
||||
healthy bool
|
||||
lastChecked time.Time
|
||||
startTime time.Time
|
||||
period int
|
||||
ticker *time.Ticker
|
||||
done chan bool
|
||||
|
@ -23,9 +24,15 @@ type healthChecker struct {
|
|||
checkFunc func() (bool, string)
|
||||
}
|
||||
|
||||
type HealthData struct {
|
||||
Message string `json:"message,omitempty"`
|
||||
LastChecked time.Time `json:"lastChecked"`
|
||||
}
|
||||
|
||||
func newHealthChecker(checkFunction func() (bool, string)) *healthChecker {
|
||||
hc := &healthChecker{}
|
||||
hc.checkFunc = checkFunction
|
||||
hc.startTime = time.Now()
|
||||
return hc
|
||||
}
|
||||
|
||||
|
@ -66,40 +73,43 @@ func (h *healthChecker) doCheck() {
|
|||
h.lastChecked = time.Now()
|
||||
}
|
||||
|
||||
// Routes add routes for liveness and readyness probes
|
||||
/* Routes add routes for liveness and readyness probes
|
||||
func (h *healthChecker) Routes() *chi.Mux {
|
||||
router := chi.NewRouter()
|
||||
router.Get("/healthz", h.healthyEndpoint)
|
||||
router.Get("/readyz", h.readinessEndpoint)
|
||||
return router
|
||||
}
|
||||
}*/
|
||||
|
||||
/*
|
||||
GetHealthyEndpoint is this service healthy
|
||||
*/
|
||||
func (h *healthChecker) healthyEndpoint(response http.ResponseWriter, req *http.Request) {
|
||||
func (h *healthChecker) healthyEndpoint(responseWriter http.ResponseWriter, request *http.Request) {
|
||||
t := time.Now()
|
||||
status := http.StatusOK
|
||||
healtData := &HealthData{
|
||||
Message: "service up and running",
|
||||
LastChecked: h.lastChecked,
|
||||
}
|
||||
if t.Sub(h.lastChecked) > (time.Second * time.Duration(2*h.period)) {
|
||||
h.healthy = false
|
||||
h.message = "Healthcheck not running"
|
||||
}
|
||||
response.Header().Add("Content-Type", "application/json")
|
||||
if h.healthy {
|
||||
response.WriteHeader(http.StatusOK)
|
||||
message := fmt.Sprintf(`{ "message": "service up and running", "lastCheck": "%s" }`, h.lastChecked.String())
|
||||
response.Write([]byte(message))
|
||||
} else {
|
||||
response.WriteHeader(http.StatusServiceUnavailable)
|
||||
message := fmt.Sprintf(`{ "message": "service is unavailable: %s", "lastCheck": "%s" }`, h.message, h.lastChecked.String())
|
||||
response.Write([]byte(message))
|
||||
if !h.healthy {
|
||||
status = http.StatusServiceUnavailable
|
||||
healtData.Message = fmt.Sprintf("service is unavailable: %s", h.message)
|
||||
}
|
||||
responseWriter.WriteHeader(status)
|
||||
render.JSON(responseWriter, request, healtData)
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
GetReadinessEndpoint is this service ready for taking requests
|
||||
*/
|
||||
func (h *healthChecker) readinessEndpoint(response http.ResponseWriter, req *http.Request) {
|
||||
response.Header().Add("Content-Type", "application/json")
|
||||
response.WriteHeader(http.StatusOK)
|
||||
response.Write([]byte(`{ "message": "service started" }`))
|
||||
func (h *healthChecker) readinessEndpoint(responseWriter http.ResponseWriter, request *http.Request) {
|
||||
render.JSON(responseWriter, request, &HealthData{
|
||||
Message: "service is ready",
|
||||
LastChecked: h.startTime,
|
||||
})
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@ import (
|
|||
"compress/flate"
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
|
@ -11,6 +12,7 @@ import (
|
|||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/creasty/defaults"
|
||||
"github.com/go-chi/chi"
|
||||
"github.com/go-chi/chi/middleware"
|
||||
"github.com/go-chi/cors"
|
||||
|
@ -21,11 +23,11 @@ import (
|
|||
|
||||
type WebServerConfiguration struct {
|
||||
Host string `yaml:"host"`
|
||||
Port int `yaml:"port"`
|
||||
Port int `default:"7080" yaml:"port"`
|
||||
SslPort int `yaml:"sslport"`
|
||||
SslCert string `yaml:"sslcert"`
|
||||
SslKey string `yaml:"sslkey"`
|
||||
HealthCheckPeriod int `yaml:"healthcheckperiod"`
|
||||
HealthCheckPeriod int `default:"30" yaml:"healthcheckperiod"`
|
||||
CertificateGenerationParams TLSCertificateGenerationParams `yaml:"tlsgenerationparams"`
|
||||
}
|
||||
|
||||
|
@ -39,7 +41,6 @@ type WebServer struct {
|
|||
serviceConfig *WebServerConfiguration
|
||||
sslsrv *http.Server
|
||||
srv *http.Server
|
||||
ssl bool
|
||||
healthChecker *healthChecker
|
||||
router *chi.Mux
|
||||
healthRouter *chi.Mux
|
||||
|
@ -47,6 +48,17 @@ type WebServer struct {
|
|||
HealthCheck func() (bool, string)
|
||||
}
|
||||
|
||||
func NewWebServer(config *WebServerConfiguration) (*WebServer, error) {
|
||||
if config == nil {
|
||||
return nil, errors.New("config may not be null")
|
||||
}
|
||||
defaults.Set(config)
|
||||
return &WebServer{
|
||||
serviceConfig: config,
|
||||
mounts: make(map[string]http.Handler),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (server *WebServer) healthRoutes() *chi.Mux {
|
||||
compressor := middleware.NewCompressor(flate.DefaultCompression)
|
||||
router := chi.NewRouter()
|
||||
|
@ -55,10 +67,15 @@ func (server *WebServer) healthRoutes() *chi.Mux {
|
|||
compressor.Handler,
|
||||
middleware.Recoverer,
|
||||
)
|
||||
router.Mount("/", server.healthChecker.Routes())
|
||||
router.Get("/healthz", server.healthChecker.healthyEndpoint)
|
||||
router.Get("/readyz", server.healthChecker.readinessEndpoint)
|
||||
return router
|
||||
}
|
||||
|
||||
func (server *WebServer) isSsl() bool {
|
||||
return server.serviceConfig.SslPort > 0
|
||||
}
|
||||
|
||||
func (server *WebServer) Mount(pattern string, handler http.Handler) {
|
||||
server.mounts[pattern] = handler
|
||||
}
|
||||
|
@ -83,6 +100,8 @@ func (server *WebServer) routes() *chi.Mux {
|
|||
for pattern, handler := range server.mounts {
|
||||
router.Mount(pattern, handler)
|
||||
}
|
||||
// clean map
|
||||
server.mounts = make(map[string]http.Handler)
|
||||
return router
|
||||
}
|
||||
|
||||
|
@ -157,22 +176,27 @@ func (server *WebServer) performHealthCheck() (bool, string) {
|
|||
return true, ""
|
||||
}
|
||||
|
||||
func (server *WebServer) Start(config *WebServerConfiguration) error {
|
||||
server.serviceConfig = config
|
||||
hc := newHealthChecker(server.performHealthCheck)
|
||||
hc.start(config.HealthCheckPeriod)
|
||||
func (server *WebServer) Start() error {
|
||||
if server.serviceConfig == nil {
|
||||
return errors.New("use NewWebServer(config) for initialising the web server")
|
||||
}
|
||||
if len(server.mounts) == 0 {
|
||||
return errors.New("No mounts points added")
|
||||
}
|
||||
|
||||
server.ssl = server.serviceConfig.SslPort > 0
|
||||
if server.ssl {
|
||||
server.healthChecker = newHealthChecker(server.performHealthCheck)
|
||||
server.healthChecker.start(server.serviceConfig.HealthCheckPeriod)
|
||||
|
||||
ssl := server.isSsl()
|
||||
if ssl {
|
||||
log.Debugf("Running in SSL mode")
|
||||
}
|
||||
|
||||
server.router = server.routes()
|
||||
|
||||
server.healthRouter = server.healthRoutes()
|
||||
server.router = server.routes()
|
||||
server.DebugRoutes("Main", server.router)
|
||||
server.DebugRoutes("Health", server.healthRouter)
|
||||
if server.ssl {
|
||||
if ssl {
|
||||
err := server.setupSsl()
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -268,7 +292,7 @@ func (server *WebServer) Stop() {
|
|||
|
||||
log.Info("Shutting down server ...")
|
||||
server.srv.Shutdown(ctx)
|
||||
if server.ssl {
|
||||
if server.isSsl() {
|
||||
server.sslsrv.Shutdown(ctx)
|
||||
}
|
||||
log.Info("Server has been shutted down")
|
||||
|
|
Loading…
Reference in New Issue