Skip to content

Commit

Permalink
added gin package
Browse files Browse the repository at this point in the history
Signed-off-by: kumarabd <[email protected]>
  • Loading branch information
kumarabd committed Aug 18, 2021
1 parent a4a6d87 commit cdef50f
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 117 deletions.
88 changes: 88 additions & 0 deletions server/http/gin/gin.go
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)
}
}
6 changes: 3 additions & 3 deletions server/http/handlers.go → server/http/gin/handlers.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package http
package gin

import (
"net/http"
Expand All @@ -24,6 +24,6 @@ func pingHandler(ctx *gin.Context) {
}
}

func (svc *httpServer) statusHandler(ctx *gin.Context) {
ctx.JSON(200, svc)
func (srv *Server) statusHandler(ctx *gin.Context) {
ctx.JSON(200, srv)
}
95 changes: 0 additions & 95 deletions server/http/http.go

This file was deleted.

20 changes: 1 addition & 19 deletions server/server.go
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

0 comments on commit cdef50f

Please sign in to comment.