-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: kumarabd <[email protected]>
- Loading branch information
Showing
4 changed files
with
92 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package gin | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"os" | ||
|
||
"github.com/gin-gonic/gin" | ||
|
||
"github.com/realnighthawk/bucky/server" | ||
) | ||
|
||
const ( | ||
ApplicationRouteGroup = "application" | ||
MonitoringRouteGroup = "monitoring" | ||
) | ||
|
||
type Server struct { | ||
Options | ||
handlers map[string]*gin.Engine | ||
addresses server.Addresses | ||
} | ||
|
||
type Options struct { | ||
Name string `json:"name,omitempty" yaml:"name,omitempty"` | ||
Environment string `json:"environment,omitempty" yaml:"environment,omitempty"` | ||
Version string `json:"version,omitempty" yaml:"version,omitempty"` | ||
Addresses server.Addresses `json:"addresses,omitempty" yaml:"addresses,omitempty"` | ||
} | ||
|
||
func New(opts Options) (*Server, error) { | ||
gin.SetMode(gin.ReleaseMode) | ||
if os.Getenv("DEBUG") == "true" { | ||
gin.SetMode(gin.DebugMode) | ||
} | ||
|
||
svc := &Server{ | ||
Options: opts, | ||
} | ||
|
||
svc.addresses = opts.Addresses | ||
svc.handlers = make(map[string]*gin.Engine, len(opts.Addresses)) | ||
for key, _ := range svc.addresses { | ||
svc.handlers[key] = gin.New() | ||
if key == "application" { | ||
svc.handlers[key].GET("/health", healthHandler) | ||
svc.handlers[key].GET("/ping", pingHandler) | ||
svc.handlers[key].GET("/status", svc.statusHandler) | ||
} | ||
} | ||
|
||
return svc, nil | ||
} | ||
|
||
func (h *Server) RegisterGeneric(method string, route string, handler http.Handler) { | ||
for key, _ := range h.handlers { | ||
h.handlers[key].Handle(method, route, gin.WrapH(handler)) | ||
break | ||
} | ||
|
||
} | ||
|
||
func (h *Server) Register(method string, route string, handler gin.HandlerFunc) { | ||
for key, _ := range h.handlers { | ||
h.handlers[key].Handle(method, route, handler) | ||
break | ||
} | ||
|
||
} | ||
|
||
func (h *Server) RegisterGenericWithGroup(name string, method string, route string, handler http.Handler) { | ||
h.handlers[name].Group(name).Handle(method, route, gin.WrapH(handler)) | ||
} | ||
|
||
func (h *Server) RegisterWithGroup(name string, method string, route string, handler gin.HandlerFunc) { | ||
h.handlers[name].Group(name).Handle(method, route, handler) | ||
} | ||
|
||
func (h *Server) Run(runCh chan error) { | ||
for name, handler := range h.handlers { | ||
go func(hd *gin.Engine, host, port string) { | ||
err := hd.Run(fmt.Sprintf("%s:%s", host, port)) | ||
if err != nil { | ||
runCh <- err | ||
} | ||
}(handler, h.addresses[name].Host, h.addresses[name].Port) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,12 @@ | ||
package server | ||
|
||
import ( | ||
"github.com/realnighthawk/bucky/apm" | ||
) | ||
|
||
const ( | ||
Development Environment = "development" | ||
Release Environment = "release" | ||
) | ||
|
||
type Environment string | ||
|
||
type Server interface { | ||
Run(chan error) | ||
EnableMetrics(apm.Options) | ||
} | ||
|
||
type HostPort struct { | ||
Name string `json:"name,omitempty" yaml:"name,omitempty"` | ||
Host string `json:"host,omitempty" yaml:"host,omitempty"` | ||
Port string `json:"port,omitempty" yaml:"port,omitempty"` | ||
} | ||
|
||
type Options struct { | ||
Name string `json:"name,omitempty"` | ||
Addresses []HostPort `json:"addresses,omitempty" yaml:"addresses,omitempty"` | ||
Environment Environment `json:"environment,omitempty"` | ||
Version string `json:"version,omitempty"` | ||
} | ||
type Addresses map[string]HostPort |