github.com/gramework/gramework@v1.8.1-0.20231027140105-82555c9057f5/behind.go (about)

     1  package gramework
     2  
     3  import (
     4  	"net"
     5  )
     6  
     7  // Behind is an interface that allow you to parse provider-dependent
     8  // headers in a compatible way.
     9  //
    10  // WARNING: this interface is currently in a WIP state and unfrozen.
    11  // If you want to support your provider, please open an issue or PR
    12  // at https://github.com/gramework/gramework.
    13  // In that way we can ensure that you have always working implementation.
    14  type Behind interface {
    15  	RemoteIP(ctx *Context) net.IP
    16  	RemoteAddr(ctx *Context) net.Addr
    17  }
    18  
    19  func (ctx *Context) RemoteIP() net.IP {
    20  	if ctx.App.behind != nil {
    21  		return ctx.App.behind.RemoteIP(ctx)
    22  	}
    23  
    24  	return ctx.RequestCtx.RemoteIP()
    25  }
    26  
    27  func (ctx *Context) RemoteAddr() net.Addr {
    28  	if ctx.App.behind != nil {
    29  		return ctx.App.behind.RemoteAddr(ctx)
    30  	}
    31  
    32  	return ctx.RequestCtx.RemoteAddr()
    33  }
    34  
    35  type internalBehindActivationHook interface {
    36  	OnAppActivation(*App)
    37  }
    38  
    39  func (app *App) Behind(u Behind) {
    40  	if hook, ok := u.(internalBehindActivationHook); ok {
    41  		hook.OnAppActivation(app)
    42  	}
    43  
    44  	app.behind = u
    45  }