ccgo

package module
v1.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 11, 2023 License: Apache-2.0 Imports: 22 Imported by: 5

README

Golib

Common core for Golang project.

Setup instruction

Both go get and go mod are supported.

go get github.com/crosect/cc-go
Usage

Using fx.Option to include dependencies for injection.

See some simple examples:

Configuration
1. Environment variables
Var Default Description
APP_PROFILES or APP_ENV local Defines the list of active profiles, separate by comma. By default, default profile is always load even this env configured.
Example: when APP_PROFILES=internal,uat then both default internal and uat will be loaded by order.
APP_CONFIG_PATHS ./config Defines the location of config directory, when the application is started, it will scan profiles in this path.
APP_CONFIG_FORMAT yaml Defines the format of config file. Currently we only support Yaml format (both yaml yml are accepted).

Besides, all our configs can be overridden by environment variables. For example:

store:
    name: Fruit store # Equivalent to STORE_NAME
    items:
        -   name: Apple # Equivalent to STORE_ITEMS_0_NAME
            price: 5 # Equivalent to STORE_ITEMS_0_PRICE
        -   name: Lemon # Equivalent to STORE_ITEMS_1_NAME
            price: 0.5 # Equivalent to STORE_ITEMS_1_PRICE
2. Available configurations
app:
    # Configuration available for AppOpt()
    name: Service Name # Specify application name. Default `unspecified`
    port: 8080 # Defines the running port. Default `8080`
    path: /service-base-path/ # Defines base path (context path). Default `/`

    # Configuration available for LoggingOpt()
    logging:
        development: false # Enable or disable development mode. Default `false`
        jsonOutputMode: true # Enable or disable json output. Default `true`

    # Configuration available for EventOpt()
    event:
        # Default event channel accept maximum 10 events,
        # other incoming events will be blocked until the channel has free space.
        channelSize: 10
        notLogPayloadForEvents:
            - OrderCreatedEvent
            - OrderUpdatedEvent

    # Configuration for HttpClientOpt()
    httpClient:
        timeout: 60s # Request timeout, in duration format. Default 60s
        maxIdleConns: 100 # Default 100
        maxIdleConnsPerHost: 10 # Default 10
        maxConnsPerHost: 100 # Default 100
        proxy:
            url: http://localhost:8080 # Proxy url
            appliedUris: # List of URIs, which will be requested under above proxy
                - https://foo.com/path/
                - https://bar.com/path/
    httpRequest:
        logging:
            disabled: false # Enable/disable all http request log
            predefinedDisabledUrls: # Shouldn't modify it, use disabledUrls instead
                - { urlPattern: "^/actuator/.*" } # By default, we disable all actuator requests
            disabledUrls: # Not log request for urls that matching method & url pattern
                - { method: "GET", urlPattern: "^/an-url-with-disabled-log/.*" }
                - { method: "POST", urlPattern: "^/another-url$" }

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ActuatorEndpointOpt

func ActuatorEndpointOpt() fx.Option

func AppOpt added in v1.0.1

func AppOpt() fx.Option

func BuildInfoOpt

func BuildInfoOpt(version string, commitHash string, time string) fx.Option

func EventOpt

func EventOpt() fx.Option

func HttpClientOpt

func HttpClientOpt() fx.Option

func HttpRequestLogOpt

func HttpRequestLogOpt() fx.Option

func LoggingOpt

func LoggingOpt() fx.Option

func NewContextualHttpClient

func NewContextualHttpClient(in ContextualHttpClientIn) (client.ContextualHttpClient, error)

NewContextualHttpClient Initiate a client.ContextualHttpClient with configs are loaded automatically. Alternatively you can wrap the default client.ContextualHttpClient with one or more other client.ContextualHttpClient to customize the behavior. To do that, your provider have to return ContextualHttpClientWrapper. For example https://github.com/crosect/cc-go-security/-/blob/develop/httpclient.go

func NewDefaultEventBus

func NewDefaultEventBus(in EventBusIn) pubsub.EventBus

func NewDefaultEventPublisher

func NewDefaultEventPublisher(in EventPublisherIn) pubsub.Publisher

func NewPropertiesLoader

func NewPropertiesLoader(in PropertiesLoaderIn) (config.Loader, error)

func OnStopEventOpt

func OnStopEventOpt() fx.Option

func PropertiesOpt

func PropertiesOpt() fx.Option

func ProvideEventBusOpt

func ProvideEventBusOpt(optConstructor interface{}) fx.Option

func ProvideEventListener

func ProvideEventListener(listener interface{}) fx.Option

func ProvideEventPublisherOpt

func ProvideEventPublisherOpt(optConstructor interface{}) fx.Option

func ProvideHealthChecker

func ProvideHealthChecker(healthCheckerConstructor interface{}) fx.Option

ProvideHealthChecker A simple way to provide a health checker

func ProvideInformer

func ProvideInformer(informerConstructor interface{}) fx.Option

ProvideInformer A simple way to provide an informer

func ProvideProps

func ProvideProps(propConstructor interface{}) fx.Option

func ProvidePropsOption

func ProvidePropsOption(option Option) fx.Option

func RegisterEventPublisher

func RegisterEventPublisher(in RegisterEventPublisherIn)

func RegisterLogger

func RegisterLogger(in RegisterLoggerIn)

func RunEventBus

func RunEventBus(bus pubsub.EventBus)

func SupplyEventBusOpt

func SupplyEventBusOpt(opt pubsub.EventBusOpt) fx.Option

func SupplyEventPublisherOpt

func SupplyEventPublisherOpt(opt pubsub.PublisherOpt) fx.Option

Types

type ActuatorIn

type ActuatorIn struct {
	fx.In
	Props     *config.AppProperties
	Checkers  []actuator.HealthChecker `group:"actuator_health_checker"`
	Informers []actuator.Informer      `group:"actuator_informer"`
}

type ActuatorOut

type ActuatorOut struct {
	fx.Out
	Endpoint        *webActuator.Endpoint
	HealthService   actuator.HealthService
	InformerService actuator.InfoService
}

func NewActuatorEndpoint

func NewActuatorEndpoint(in ActuatorIn) ActuatorOut

NewActuatorEndpoint Initiate actuator endpoint with health checker and informer automatically.

================= Health Checker ====================== To register a Health Checker, your component have to produce an actuator.HealthChecker with group `actuator_health_checker` in the result of provider function. For example, a redis provider produce the following result:

type RedisOut struct {
   fx.Out
   Client        *redis.Client
   HealthChecker actuator.HealthChecker `group:"actuator_health_checker"`
}
func NewRedis() (RedisOut, error) {}

or using ProvideHealthChecker

func NewSampleHealthChecker() actuator.HealthChecker {
	return &SampleHealthChecker{}
}
ProvideHealthChecker(NewSampleHealthChecker)

=================== Informer ========================= Similar to Health Checker, an Informer also registered by produce an actuator.Informer. For example, a GitRevision provider produce the following result:

type GitRevisionOut struct {
   fx.Out
   Informer actuator.Informer `group:"actuator_informer"`
}
func NewGitRevision() (GitRevisionOut, error) {}

or using ProvideInformer

func NewSampleInformer() actuator.Informer {
	return &SampleInformer{}
}
ProvideInformer(NewSampleInformer)

type App

type App struct {
	// contains filtered or unexported fields
}

func New added in v1.0.1

func New(context context.Context, props *config.AppProperties) *App

func (*App) AddHandler added in v1.0.1

func (a *App) AddHandler(handlers ...func(next http.Handler) http.Handler)

func (App) Context added in v1.0.1

func (a App) Context() context.Context

func (App) Handlers added in v1.0.1

func (a App) Handlers() []func(next http.Handler) http.Handler

func (App) Name added in v1.0.1

func (a App) Name() string

func (App) Path added in v1.0.1

func (a App) Path() string

func (App) Port added in v1.0.1

func (a App) Port() int

type ContextualHttpClientIn

type ContextualHttpClientIn struct {
	fx.In
	AppProperties *config.AppProperties
	HttpClient    client.HttpClient
	Wrappers      []ContextualHttpClientWrapper `group:"contextual_http_client_wrapper"`
}

type EventBusIn

type EventBusIn struct {
	fx.In
	Options []pubsub.EventBusOpt `group:"event_bus_opt"`
}

type EventPublisherIn

type EventPublisherIn struct {
	fx.In
	Bus     pubsub.EventBus
	Options []pubsub.PublisherOpt `group:"event_publisher_opt"`
}

type NewLoggerOut

type NewLoggerOut struct {
	fx.Out
	Core log.Logger
	Web  log.Logger `name:"web_logger"`
}

func NewLogger

func NewLogger(props *log.Properties) (NewLoggerOut, error)

type Option

type Option func(option *config.Option)

func WithActiveProfiles

func WithActiveProfiles(activeProfiles []string) Option

func WithDebugLog

func WithDebugLog(debugFunc config.DebugFunc) Option

func WithFormat

func WithFormat(configFormat string) Option

WithFormat accept yaml, json values

func WithPaths

func WithPaths(configPaths []string) Option

type PropertiesLoaderIn

type PropertiesLoaderIn struct {
	fx.In
	Properties []config.Properties `group:"properties"`
	Options    []Option            `group:"properties_option"`
}

type RegisterEventPublisherIn

type RegisterEventPublisherIn struct {
	fx.In
	Bus         pubsub.EventBus
	Publisher   pubsub.Publisher
	Subscribers []pubsub.Subscriber `group:"event_listener"`
}

type RegisterLoggerIn

type RegisterLoggerIn struct {
	fx.In
	Core log.Logger
	Web  log.Logger `name:"web_logger"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL