github.com/wfusion/gofusion@v1.1.14/http/types.go (about)

     1  package http
     2  
     3  import (
     4  	"net/http"
     5  	"time"
     6  
     7  	"github.com/gin-gonic/gin"
     8  	"github.com/go-resty/resty/v2"
     9  
    10  	"github.com/wfusion/gofusion/common/utils"
    11  	"github.com/wfusion/gofusion/log"
    12  )
    13  
    14  type IRouter interface {
    15  	Use(middlewares ...gin.HandlerFunc) IRouter
    16  
    17  	Handle(uri string, fn routerHandler, opts ...utils.OptionExtender) IRouter
    18  	Any(uri string, fn routerHandler, opts ...utils.OptionExtender) IRouter
    19  	GET(uri string, fn routerHandler, opts ...utils.OptionExtender) IRouter
    20  	POST(uri string, fn routerHandler, opts ...utils.OptionExtender) IRouter
    21  	DELETE(uri string, fn routerHandler, opts ...utils.OptionExtender) IRouter
    22  	PATCH(uri string, fn routerHandler, opts ...utils.OptionExtender) IRouter
    23  	PUT(uri string, fn routerHandler, opts ...utils.OptionExtender) IRouter
    24  	OPTIONS(uri string, fn routerHandler, opts ...utils.OptionExtender) IRouter
    25  	HEAD(uri string, fn routerHandler, opts ...utils.OptionExtender) IRouter
    26  	Group(relativePath string, handlers ...gin.HandlerFunc) IRouter
    27  
    28  	StaticFile(string, string) IRouter
    29  	StaticFileFS(string, string, http.FileSystem) IRouter
    30  	Static(string, string) IRouter
    31  	StaticFS(string, http.FileSystem) IRouter
    32  
    33  	ServeHTTP(http.ResponseWriter, *http.Request)
    34  	Config() OutputConf
    35  	ListenAndServe() error
    36  	Start()
    37  
    38  	Running() <-chan struct{}
    39  	Closing() <-chan struct{}
    40  
    41  	shutdown()
    42  }
    43  
    44  // Conf http configure
    45  //nolint: revive // struct field annotation issue
    46  type Conf struct {
    47  	Port            int                    `yaml:"port" json:"port" toml:"port" default:"80"`
    48  	TLS             bool                   `yaml:"tls" json:"tls" toml:"tls" default:"false"`
    49  	Cert            string                 `yaml:"cert" json:"cert" toml:"cert"`
    50  	Key             string                 `yaml:"key" json:"key" toml:"key"`
    51  	NextProtos      []string               `yaml:"next_protos" json:"next_protos" toml:"next_protos" default:"[http/1.1]"` // h2, http/1.1 is ok
    52  	SuccessCode     int                    `yaml:"success_code" json:"success_code" toml:"success_code"`
    53  	ErrorCode       int                    `yaml:"error_code" json:"error_code" toml:"error_code" default:"-1"`
    54  	Pprof           bool                   `yaml:"pprof" json:"pprof" toml:"pprof"`
    55  	XSSWhiteURLList []string               `yaml:"xss_white_url_list" json:"xss_white_url_list" toml:"xss_white_url_list" default:"[]"`
    56  	ColorfulConsole bool                   `yaml:"colorful_console" json:"colorful_console" toml:"colorful_console" default:"false"`
    57  	ReadTimeout     string                 `yaml:"read_timeout" json:"read_timeout" toml:"read_timeout" default:"10s"`
    58  	WriteTimeout    string                 `yaml:"write_timeout" json:"write_timeout" toml:"write_timeout" default:"10s"`
    59  	EnableLogger    bool                   `yaml:"enable_logger" json:"enable_logger" toml:"enable_logger"`
    60  	LogInstance     string                 `yaml:"log_instance" json:"log_instance" toml:"log_instance" default:"default"`
    61  	Logger          string                 `yaml:"logger" json:"logger" toml:"logger" default:"github.com/wfusion/gofusion/log/customlogger.httpLogger"`
    62  	Asynq           []asynqConf            `yaml:"asynq" json:"asynq" toml:"asynq"`
    63  	Clients         map[string]*clientConf `yaml:"clients" json:"clients" toml:"clients"`
    64  	Metrics         metricsConf            `yaml:"metrics" json:"metrics" toml:"metrics"`
    65  }
    66  
    67  type asynqConf struct {
    68  	Path              string       `yaml:"path" json:"path" toml:"path"`
    69  	Instance          string       `yaml:"instance" json:"instance" toml:"instance"`
    70  	InstanceType      instanceType `yaml:"instance_type" json:"instance_type" toml:"instance_type"`
    71  	Readonly          bool         `yaml:"readonly" json:"readonly" toml:"readonly"`
    72  	PrometheusAddress string       `yaml:"prometheus_address" json:"prometheus_address" toml:"prometheus_address"`
    73  }
    74  
    75  // clientConf http client configure
    76  //nolint: revive // struct field annotation issue
    77  type clientConf struct {
    78  	Mock                  bool     `yaml:"mock" json:"mock" toml:"mock"`
    79  	Timeout               string   `yaml:"timeout" json:"timeout" toml:"timeout" default:"30s"`
    80  	DialTimeout           string   `yaml:"dial_timeout" json:"dial_timeout" toml:"dial_timeout" default:"30s"`
    81  	DialKeepaliveTime     string   `yaml:"dial_keepalive_time" json:"dial_keepalive_time" toml:"dial_keepalive_time" default:"30s"`
    82  	ForceAttemptHTTP2     bool     `yaml:"force_attempt_http2" json:"force_attempt_http2" toml:"force_attempt_http2" default:"true"`
    83  	TLSHandshakeTimeout   string   `yaml:"tls_handshake_timeout" json:"tls_handshake_timeout" toml:"tls_handshake_timeout" default:"10s"`
    84  	DisableCompression    bool     `yaml:"disable_compression" json:"disable_compression" toml:"disable_compression"`
    85  	MaxIdleConns          int      `yaml:"max_idle_conns" json:"max_idle_conns" toml:"max_idle_conns" default:"100"`
    86  	MaxIdleConnsPerHost   int      `yaml:"max_idle_conns_per_host" json:"max_idle_conns_per_host" toml:"max_idle_conns_per_host" default:"100"`
    87  	MaxConnsPerHost       int      `yaml:"max_conns_per_host" json:"max_conns_per_host" toml:"max_conns_per_host"`
    88  	IdleConnTimeout       string   `yaml:"idle_conn_timeout" json:"idle_conn_timeout" toml:"idle_conn_timeout" default:"90s"`
    89  	ExpectContinueTimeout string   `yaml:"expect_continue_timeout" json:"expect_continue_timeout" toml:"expect_continue_timeout" default:"1s"`
    90  	RetryCount            int      `yaml:"retry_count" json:"retry_count" toml:"retry_count"`
    91  	RetryWaitTime         string   `yaml:"retry_wait_time" json:"retry_wait_time" toml:"retry_wait_time" default:"100ms"`
    92  	RetryMaxWaitTime      string   `yaml:"retry_max_wait_time" json:"retry_max_wait_time" toml:"retry_max_wait_time" default:"2s"`
    93  	RetryConditionFuncs   []string `yaml:"retry_condition_funcs" json:"retry_condition_funcs" toml:"retry_condition_funcs"`
    94  	RetryHooks            []string `yaml:"retry_hooks" json:"retry_hooks" toml:"retry_hooks"`
    95  }
    96  
    97  // metricsConf http metrics configure
    98  
    99  type metricsConf struct {
   100  	HeaderLabels []string `yaml:"header_labels" json:"header_labels" toml:"header_labels"`
   101  }
   102  
   103  type OutputConf struct {
   104  	Port         int
   105  	TLS          bool
   106  	Cert         string
   107  	Key          string
   108  	NextProtos   []string
   109  	SuccessCode  int
   110  	ReadTimeout  time.Duration
   111  	WriteTimeout time.Duration
   112  	AsynqConf    []asynqConf
   113  }
   114  
   115  type cfg struct {
   116  	c       *clientConf
   117  	appName string
   118  	logger  resty.Logger
   119  }
   120  
   121  type instanceType string
   122  
   123  const (
   124  	instanceTypeRedis instanceType = "redis"
   125  )
   126  
   127  type customLogger interface {
   128  	Init(log log.Loggable, appName string)
   129  }