Files
golib/httpserver/server.go
Nicolas JUHEL 573af6ce3a Package AWS :
- Config Model : add a config model with a golib RouerStatus Config Model to use a AWS connection for API with a request status health check
  - Config Interface : add function GetAccessKey to retrieve the accesskey value currently used into the current connection
  - function Walk (Object, version, ...) : fix bug with pointer of string not initialized

Package Config :
  - interface Component : add status router pointer into the Init function to allow used a global router status for all component status registration as router status component

Package Status :
  - move Status Config as an sub package of Package Status

Package Errors :
  - add CamelCase const

Package Logger :
  - fix following bump dependencies

Global :
  - bump dependencies
  - change init of errors files : change function never call vy panic to prevent an error code collision
2022-09-13 12:31:04 +02:00

270 lines
5.3 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() 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()), vers, ""
}
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() 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(s.StatusInfo, s.StatusHealth)
} else {
return cfg.Status.Component(s.StatusInfo, s.StatusHealth)
}
}