Files
golib/httpserver/server.go
nabbar 52f0d6fa04 Config Component :
- Change reloading component method : try to reload all component and store errors, report list of errors but don't break the reloading process

Package Logger :
- Fix bug with entry logger filtering

Package Viper:
- reword message log before reloading config file (watchFS)
- add message log after reloading config file (watchFS)

Package Status :
- Fix DATA Race with status/info
- Add component key into log error message for health
- Add component key into function to use it into info (name)
- Reword health message : no OK/KO (still into status info), add error message reporting

Package httpserver :
- status info : apply update status component, use key in name
- status info : optimize code

Package Request :
- Fix error in url path operation
- Status info : optimize code
- Status info : apply update component, add endpoint hostname with name

Package Static :
- apply status component update

Other :
- Bump dependencies
2022-11-17 16:54:51 +01:00

270 lines
5.4 KiB
Go

/*
* MIT License
*
* Copyright (c) 2020 Nicolas JUHEL
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*
*/
package httpserver
import (
"fmt"
"net/http"
"runtime"
"strings"
"sync/atomic"
"time"
"github.com/nabbar/golib/status/config"
liblog "github.com/nabbar/golib/logger"
liberr "github.com/nabbar/golib/errors"
libsts "github.com/nabbar/golib/status"
)
const (
timeoutShutdown = 10 * time.Second
)
type server struct {
log *atomic.Value
run *atomic.Value
cfg *atomic.Value
}
type Server interface {
SetLogger(log liblog.FuncLog)
GetConfig() *ServerConfig
SetConfig(cfg *ServerConfig) bool
GetName() string
GetBindable() string
GetExpose() string
GetHandlerKey() string
IsRunning() bool
IsTLS() bool
WaitNotify()
Merge(srv Server) bool
Listen(handler http.Handler) liberr.Error
Restart()
Shutdown()
StatusInfo() (name string, release string, hash string)
StatusHealth() error
StatusComponent(key string) libsts.Component
}
func NewServer(cfg *ServerConfig) Server {
c := new(atomic.Value)
c.Store(cfg.Clone())
return &server{
log: new(atomic.Value),
cfg: c,
run: new(atomic.Value),
}
}
func (s *server) SetLogger(log liblog.FuncLog) {
s.log.Store(log)
}
func (s *server) GetLogger() liblog.FuncLog {
if i := s.log.Load(); i == nil {
return nil
} else if f, ok := i.(liblog.FuncLog); ok {
return f
}
return nil
}
func (s *server) GetConfig() *ServerConfig {
if s.cfg == nil {
return nil
} else if i := s.cfg.Load(); i == nil {
return nil
} else if c, ok := i.(ServerConfig); !ok {
return nil
} else {
return &c
}
}
func (s *server) IsDisabled() bool {
if c := s.GetConfig(); c != nil {
return c.Disabled
}
return true
}
func (s *server) SetConfig(cfg *ServerConfig) bool {
if cfg == nil {
return false
}
if s.cfg == nil {
s.cfg = new(atomic.Value)
}
c := cfg.Clone()
if c.Name == "" {
c.Name = c.GetListen().Host
}
s.cfg.Store(cfg.Clone())
return true
}
func (s *server) getRun() run {
if s.run == nil {
return newRun(s.GetLogger())
} else if i := s.run.Load(); i == nil {
return newRun(s.GetLogger())
} else if r, ok := i.(run); !ok {
return newRun(s.GetLogger())
} else {
return r
}
}
func (s *server) setRun(r run) {
s.run.Store(r)
}
func (s *server) getErr() error {
if r := s.getRun(); r == nil {
return nil
} else {
return r.GetError()
}
}
func (s server) GetName() string {
return s.GetConfig().Name
}
func (s *server) GetBindable() string {
return s.GetConfig().GetListen().Host
}
func (s *server) GetExpose() string {
return s.GetConfig().GetExpose().String()
}
func (s *server) GetHandlerKey() string {
return s.GetConfig().GetHandlerKey()
}
func (s *server) IsRunning() bool {
return s.getRun().IsRunning()
}
func (s *server) IsTLS() bool {
return s.GetConfig().IsTLS()
}
func (s *server) Listen(handler http.Handler) liberr.Error {
if s.IsDisabled() && !s.IsRunning() {
return nil
} else if s.IsDisabled() {
s.Shutdown()
return nil
}
r := s.getRun()
e := r.Listen(s.GetConfig(), handler)
s.setRun(r)
runtime.GC()
return e
}
func (s *server) WaitNotify() {
r := s.getRun()
r.WaitNotify()
}
func (s *server) Restart() {
_ = s.Listen(nil)
}
func (s *server) Shutdown() {
if s.IsDisabled() && !s.IsRunning() {
return
}
r := s.getRun()
r.Shutdown()
s.setRun(r)
}
func (s *server) Merge(srv Server) bool {
if x, ok := srv.(*server); !ok {
return false
} else {
return s.SetConfig(x.GetConfig())
}
}
func (s *server) StatusInfo() (name string, release string, hash string) {
vers := strings.TrimLeft(runtime.Version(), "go")
vers = strings.TrimLeft(vers, "Go")
vers = strings.TrimLeft(vers, "GO")
return fmt.Sprintf("%s [%s]", s.GetName(), s.GetBindable()), runtime.Version()[2:], ""
}
func (s *server) StatusHealth() error {
if !s.IsDisabled() && s.IsRunning() {
return nil
} else if s.IsDisabled() {
return ErrorServerDisabled.Error(nil)
} else if e := s.getErr(); e != nil {
return e
} else {
return ErrorServerOffline.Error(nil)
}
}
func (s *server) StatusComponent(key string) libsts.Component {
if cfg := s.GetConfig(); cfg == nil {
cnf := config.ConfigStatus{
Mandatory: true,
MessageOK: "",
MessageKO: "",
CacheTimeoutInfo: 30 * time.Second,
CacheTimeoutHealth: 5 * time.Second,
}
return cnf.Component(key, s.StatusInfo, s.StatusHealth)
} else {
return cfg.Status.Component(key, s.StatusInfo, s.StatusHealth)
}
}