-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Skip CI][WIP] Cleanarch template improvement
- Loading branch information
Paul Lhussiez
committed
Oct 31, 2018
1 parent
f7deaea
commit 9da31ad
Showing
4 changed files
with
111 additions
and
7 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
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 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,31 @@ | ||
package router | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
// Interface is the interface that has to be implemented in order to handle | ||
// every functionality of the router | ||
type Interface interface { | ||
Health(c *gin.Context) | ||
} | ||
|
||
// New returns a new router | ||
func New() Interface { | ||
return Impl{} | ||
} | ||
|
||
// Impl is the implementation | ||
type Impl struct{} | ||
|
||
// Health is the implementation of health check | ||
func (r Impl) Health(c *gin.Context) { | ||
c.JSON(http.StatusOK, gin.H{"alive": true}) | ||
} | ||
|
||
//SetRoutes update router with routes | ||
func SetRoutes(router *gin.Engine, r Interface) { | ||
router.GET("/health", r.Health) | ||
} |
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,65 @@ | ||
package infra | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/itsjamie/gin-cors" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
// GinServer is the struct gathering all the server details | ||
type GinServer struct { | ||
host string | ||
port int | ||
Router *gin.Engine | ||
} | ||
|
||
// NewServer creates the gin Server | ||
func NewServer(host string, port int, mode, allowedOrigins string) GinServer { | ||
s := GinServer{} | ||
s.port = port | ||
s.host = host | ||
|
||
s.Router = gin.New() | ||
s.setMode(mode) | ||
s.Router.Use(gin.Recovery()) | ||
s.setCORS(allowedOrigins) | ||
|
||
return s | ||
} | ||
|
||
func (s *GinServer) setMode(mode string) { | ||
switch mode { | ||
case "debug": | ||
gin.SetMode(gin.DebugMode) | ||
case "test": | ||
gin.SetMode(gin.TestMode) | ||
case "release": | ||
gin.SetMode(gin.ReleaseMode) | ||
default: | ||
logrus.WithField("mode", mode).Warn("Unknown gin mode, fallback to release") | ||
gin.SetMode(gin.ReleaseMode) | ||
} | ||
} | ||
|
||
// setCORS is a helper to set current engine CORS | ||
func (s *GinServer) setCORS(allowedOrigins string) { | ||
s.Router.Use(cors.Middleware(cors.Config{ | ||
Origins: allowedOrigins, | ||
Methods: "GET, PUT, POST, DELETE, OPTION, PATCH", | ||
RequestHeaders: "Origin, Authorization, Content-Type", | ||
ExposedHeaders: "", | ||
MaxAge: 50 * time.Second, | ||
Credentials: true, | ||
ValidateHeaders: false, | ||
})) | ||
} | ||
|
||
// Start tells the router to start listening | ||
func (s GinServer) Start() { | ||
if err := s.Router.Run(fmt.Sprintf("%s:%d", s.host, s.port)); err != nil { | ||
logrus.WithError(err).Fatal("Couldn't start router") | ||
} | ||
} |