Mortar

HTTP Handler/Func

Although it’s very convenient to use gRPC + gRPC-Gateway to serve RESTful APIs, you sometime want to use prebuilt HTTP Handler/HandlerFunc(s) or similar.

Mortar provides 2 fx.Group(s) for that: ExternalHTTPHandlers and ExternalHTTPHandlerFunctions, one for http.Handler and the other for http.HandlerFunc.

To better understand Mortar groups, read here

Registering http.Handler

To register a new http.Handler, you need to define how it’s going to be served == Pattern:

  1. Create a HTTPHandlerPatternPair:

    var customHandler http.Handler ...
    
    func createHandlerPair() types.HTTPHandlerPatternPair {
     return types.HTTPHandlerPatternPair{
         Pattern:"/your/pattern",
         Handler: customHandler,
     }
    }
    
  2. Add this pair to the ExternalHTTPHandlers group:

    var customHandlerOption = fx.Provide(
        fx.Annotated{
            Group: groups.ExternalHTTPHandlers,
            Target: createHandlerPair, // function pointer, will be called by Uber-FX
        }
    )
    
  3. All that’s left is to register it into the Uber-FX graph:

    return fx.New(
     ...
     customHandlerOption,
     ...
    )
    

Registering http.HandlerFunc

Registering http.HandlerFunc is very much similar to registering http.Handler.

Actually, you need to follow the exact same guide and replace Handler with HandlerFunc:

var customHandler http.Handler ...

func createHandlerPair() types.HTTPHandlerPatternPair {
 return types.HTTPHandlerPatternPair{
     Pattern:"/your/pattern",
     Handler: customHandler,
 }
}

var customHandlerOption = fx.Provide(
    fx.Annotated{
        Group: groups.ExternalHTTPHandlers,
        Target: createHandlerPair, // function pointer, will be called by Uber-FX
    }
) 
return fx.New(
 ...
 customHandlerOption,
 ...
)
var customHandlerFunc http.HandlerFunc ...

func createHandlerFuncPair() types.HTTPHandlerFuncPatternPair {
 return types.HTTPHandlerFuncPatternPair{
     Pattern:"/your/pattern",
     Handler: customHandlerFunc,
 }
}

var customHandlerFuncOption = fx.Provide(
    fx.Annotated{
        Group: groups.ExternalHTTPHandlerFunctions,
        Target: createHandlerFuncPair, // function pointer, will be called by Uber-FX
    }
) 
return fx.New(
 ...
 customHandlerFuncOption,
 ...
)