Documentation
¶
Index ¶
- Constants
- func HttpError401(ctx *gin.Context)
- func IsJSONRequest(ctx *gin.Context) bool
- func NewRouter(serverName string, debug bool) *gin.Engine
- type AuthMiddlewareInterface
- type JSONErrorDetail
- type JSONResponse
- type JSONResponseError
- type Server
- func (c *Server) AddMiddleware(middlewareFunc gin.HandlerFunc)
- func (c *Server) Group(relativePath string) *gin.RouterGroup
- func (c *Server) Route() *gin.Engine
- func (c *Server) Shutdown(ctx context.Context) error
- func (c *Server) Start() error
- func (c *Server) UseAuth(authMiddleware AuthMiddlewareInterface)
- type ServerConfig
Constants ¶
const ( ServerDefaultReadTimeout = 600 ServerDefaultWriteTimeout = 600 ServerDefaultPort = 5000 ServerDefaultName = "http" HeaderAccept = "Accept" HeaderContentType = "Content-Type" ContentTypeHtml = "text/html" ContentTypeJson = "application/json" ContentTypeBinary = "application/octet-stream" ErrNilConfig = utils.Error("Config is nil") )
Variables ¶
This section is empty.
Functions ¶
func IsJSONRequest ¶
IsJSONRequest returns true if request is a JSON request
Types ¶
type AuthMiddlewareInterface ¶
type JSONErrorDetail ¶
type JSONErrorDetail struct {
Message string `json:"message,omitempty"`
FormError interface{} `json:"formError,omitempty"`
}
type JSONResponse ¶
type JSONResponse struct {
Success bool `json:"success"`
Data interface{} `json:"data,omitempty"`
}
type JSONResponseError ¶
type JSONResponseError struct {
Success bool `json:"success"`
Error JSONErrorDetail `json:"error"`
}
type Server ¶
type Server struct {
Config *ServerConfig
Router *gin.Engine
Server *http.Server
}
func NewServer ¶
func NewServer(cfg *ServerConfig) (*Server, error)
NewServer creates a new http server.
Example usage:
cfg := &ServerConfig{...}
server, err := NewServer(cfg)
if err != nil {
log.Fatal(err)
}
server.Start()
func (*Server) AddMiddleware ¶
func (c *Server) AddMiddleware(middlewareFunc gin.HandlerFunc)
AddMiddleware adds the specified middleware function to the server's router. The middlewareFunc parameter should be a function that accepts a *gin.Context parameter. The function is added as middleware to the server's router using the Use() method of the gin.Engine. This allows the middleware to be executed for each incoming request before reaching the final handler. Example usage:
server.AddMiddleware(myMiddleware)
func myMiddleware(ctx *gin.Context) {
// do something before reaching the final handler
ctx.Next()
// do something after the final handler
}
Note: The AddMiddleware method is defined on the Server struct which contains a Router field of type gin.Engine.
func (*Server) Group ¶
func (c *Server) Group(relativePath string) *gin.RouterGroup
Group creates a new RouterGroup with the specified relativePath. A RouterGroup is used to group routes together and apply common middleware and settings. The relativePath parameter is the base path for all routes added to the group. The returned value is a pointer to the newly created RouterGroup.
Example usage:
server := &Server{
// initialize other fields
Router: gin.New(),
}
group := server.Group("/api") group.GET("/users", getUsers)
This will create a group with the base path "/api" and add a route for GET "/users". All routes added to the group will have the "/api" prefix.
func (*Server) Route ¶
Route returns the gin.Engine instance associated with the Server.
It is used to access the underlying gin.Engine for adding routes and defining middleware.
Example usage:
server := Server{
// initialize other fields
}
engine := server.Route()
engine.GET("/hello", func(c *gin.Context) {
// handle request
})
Note: The gin.Engine instance is stored in the Router field of the Server struct.
func (*Server) Shutdown ¶
Shutdown gracefully shuts down the server by calling the Shutdown method of the underlying httpserver.Server. It takes a context.Context object as a parameter, which can be used to control the shutdown process. The method returns an error if the shutdown process fails.
func (*Server) Start ¶
Start starts the HTTP server of the Server instance. If the Server's TLSConfig is nil, it starts the server using ListenAndServe method of httpserver.Server. Otherwise, it starts the server using ListenAndServeTLS method of httpserver.Server. If the returned error from the server is not http.ErrServerClosed, it is returned. Otherwise, nil is returned.
Usage example:
func (a *SampleApplication) Run() {
// register http destructor callback
blueprint.RegisterDestructor(func() error {
return a.httpServer.Shutdown(a.container.GetContext())
})
// Start application - http server
a.container.Run(func(app interface{}) error {
go func() {
log.Info().Msg(fmt.Sprintf("Running Sample Application API at %s:%d", a.httpServer.Config.Host, a.httpServer.Config.Port))
a.container.AbortFatal(a.httpServer.Start())
}()
return nil
})
}
func (*Server) UseAuth ¶
func (c *Server) UseAuth(authMiddleware AuthMiddlewareInterface)
UseAuth registers an auth middleware
type ServerConfig ¶
type ServerConfig struct {
Host string `json:"host"`
Port int `json:"port"`
ReadTimeout int `json:"readTimeout"`
WriteTimeout int `json:"writeTimeout"`
Debug bool `json:"debug"`
Options map[string]string `json:"options"`
tlsProvider.ServerConfig
}
func NewServerConfig ¶
func NewServerConfig() *ServerConfig
func (*ServerConfig) GetOption ¶
func (c *ServerConfig) GetOption(key string, defaultValue string) string
GetOption retrieves the value associated with the specified key from the Options map of the ServerConfig. If the key exists, the corresponding value is returned. Otherwise, the defaultValue is returned. The Options map is defined as map[string]string in the ServerConfig struct. Example usage:
serverConfig := ServerConfig{
// initialize other fields
Options: map[string]string{
"key1": "value1",
"key2": "value2",
},
}
option := serverConfig.GetOption("key1", "default")
// option is "value1"
option := serverConfig.GetOption("key3", "default")
// option is "default"
func (*ServerConfig) NewServer ¶
func (c *ServerConfig) NewServer() (*Server, error)
func (*ServerConfig) Validate ¶
func (c *ServerConfig) Validate() error