rkboot

package module
v2.2.3 Latest Latest
Warning

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

Go to latest
Published: Jun 24, 2022 License: Apache-2.0 Imports: 10 Imported by: 0

README

rk-boot

build codecov Go Report Card Sourcegraph GoDoc Release License FOSSA Status

Official site

rkdev.info

Join discussing channel

Channel Code
Wechat group (CN) image
Slack channel (EN) #rk-boot

Important note about V2

RK family is bumping up to V2 which will not be full compatible with V1 including documentation. Please refer to V1 as needed.

From V2, rk-boot will not include any of dependencies which implement rkentry.Entry. As a result, user need to pull rk-boot and rk-xxx or user implemented Entry manually.

We think it will be a better experience for dependency management.

For example, if we hope to start Gin web framework, we need to pull both of rk-boot and rk-gin. example

Official document

V2 documentation will be updated soon. Please refer to github docs now.

Concept

rk-boot is a library which support bootstrapping server at runtime via YAML file. It is a little like spring boot way.

arch

Goal

We hope user can achieve bellow goals while designing/implementing microservice.

  • 1: Decide which dependencies to use. (For example, MySQL, Redis, AWS, Gin are the required dependencies.)
  • 2: Add dependencies in boot.yaml (where rk-boot will automatically initiate dependency client instances.)
  • 3: Implement your own codes (without caring about logging, metrics and tracing of dependency client.)
  • 4: Monitor service and dependency (via a standard dashboard.)

We are planning to achieve this goal by unify dependency input(by boot.yaml) and output(logging format, metrics format etc).

We will add more bootstrapper for popular third-party dependencies.

Why do I want it?
  • Build application with unified project layout at enterprise level .
  • Build API with the unified format of logging, metrics, tracing, authorization at enterprise level.
  • Make application replace core dependencies quickly.
  • Save learning time of writing initializing procedure of popular frameworks and libraries.
  • User defined Entry for customization.

Plugins V2

We will migrate dependencies from v1 to v2 as quick as possible.

Category Name V2 go get Example
Web
Framework
gin-gonic/gin github.com/rookie-ninja/rk-gin/v2 example
gRPC github.com/rookie-ninja/rk-grpc/v2 example
labstack/echo github.com/rookie-ninja/rk-echo example
gogf/gf github.com/rookie-ninja/rk-gf example
gofiber/fiber github.com/rookie-ninja/rk-fiber example
zeromicro/go-zero github.com/rookie-ninja/rk-zero example
gorilla/mux github.com/rookie-ninja/rk-mux example
Database
ORM
MySQL github.com/rookie-ninja/rk-db/mysql example
SQLite github.com/rookie-ninja/rk-db/sqlite example
SQL Server github.com/rookie-ninja/rk-db/sqlserver example
postgreSQL github.com/rookie-ninja/rk-db/postgres example
ClickHouse github.com/rookie-ninja/rk-db/clickhouse example
MongoDB github.com/rookie-ninja/rk-db/mongodb example
Redis github.com/rookie-ninja/rk-db/redis example
Caching Redis github.com/rookie-ninja/rk-cache/redis example
Cloud AWS github.com/rookie-ninja/rk-cloud/aws TODO
AWS/KMS github.com/rookie-ninja/rk-cloud/aws/kms TODO
AWS/KMS/Signer github.com/rookie-ninja/rk-cloud/aws/signer TODO
AWS/KMS/Crypto github.com/rookie-ninja/rk-cloud/aws/crypto TODO
Tencent github.com/rookie-ninja/rk-cloud/tencent TODO
Tencent/KMS github.com/rookie-ninja/rk-cloud/tencent/kms TODO
Tencent/KMS/Signer github.com/rookie-ninja/rk-cloud/tencent/signer TODO
Tencent/KMS/Crypto github.com/rookie-ninja/rk-cloud/tencent/crypto TODO

Quick Start for Gin

We will start gin-gonic/gin server with rk-boot.

  • Installation rk-boot is required one for all RK family. We pulled rk-gin as dependency since we are testing GIN.
go get github.com/rookie-ninja/rk-boot/v2
go get github.com/rookie-ninja/rk-gin/v2
  • boot.yaml
---
gin:
  - name: greeter                                          # Required
    port: 8080                                             # Required
    enabled: true                                          # Required
    sw:
      enabled: true                                        # Optional, default: false
    docs:
      enabled: true                                        # Optional, default: false
  • main.go
// Copyright (c) 2021 rookie-ninja
//
// Use of this source code is governed by an Apache-style
// license that can be found in the LICENSE file.

package main

import (
	"context"
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/rookie-ninja/rk-boot/v2"
	"github.com/rookie-ninja/rk-gin/v2/boot"
	"net/http"
)

// @title Swagger Example API
// @version 1.0
// @description This is a sample rk-demo server.
// @termsOfService http://swagger.io/terms/

// @securityDefinitions.basic BasicAuth

// @contact.name API Support
// @contact.url http://www.swagger.io/support
// @contact.email support@swagger.io

// @license.name Apache 2.0
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html
func main() {
	// Create a new boot instance.
	boot := rkboot.NewBoot()

	// Register handler
	entry := rkgin.GetGinEntry("greeter")
	entry.Router.GET("/v1/greeter", Greeter)

	// Bootstrap
	boot.Bootstrap(context.TODO())

	boot.WaitForShutdownSig(context.TODO())
}

// Greeter handler
// @Summary Greeter
// @Id 1
// @Tags Hello
// @version 1.0
// @Param name query string true "name"
// @produce application/json
// @Success 200 {object} GreeterResponse
// @Router /v1/greeter [get]
func Greeter(ctx *gin.Context) {
	ctx.JSON(http.StatusOK, &GreeterResponse{
		Message: fmt.Sprintf("Hello %s!", ctx.Query("name")),
	})
}

type GreeterResponse struct {
	Message string
}
  • validate
$ go run main.go

$ curl -X GET localhost:8080/v1/greeter?name=rk-dev
{"Message":"Hello rk-dev!"}

$ curl -X GET localhost:8080/rk/v1/ready
{
  "ready": true
}

$ curl -X GET localhost:8080/rk/v1/alive
{
  "alive": true
}

image

image

Development Status: Stable

Build instruction

Simply run make all to validate your changes. Or run codes in example/ folder.

  • make all If proto or files in boot/assets were modified, then we need to run it.

Test instruction

Run unit test with make test command.

github workflow will automatically run unit test and golangci-lint for testing and lint validation.

Contributing

We encourage and support an active, healthy community of contributors — including you! Details are in the contribution guide and the code of conduct. The rk maintainers keep an eye on issues and pull requests, but you can also report any negative conduct to lark@rkdev.info.

Released under the Apache 2.0 License.

License

FOSSA Status

Documentation

Overview

Package rkboot is bootstrapper for rk style application

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Boot

type Boot struct {
	EventId string `yaml:"-" json:"-"`
	// contains filtered or unexported fields
}

Boot is a structure for bootstrapping rk style application

func NewBoot

func NewBoot(opts ...BootOption) *Boot

NewBoot create a bootstrapper.

func (*Boot) AddHookFuncAfterBootstrap

func (boot *Boot) AddHookFuncAfterBootstrap(entryType, entryName string, f func(ctx context.Context))

AddHookFuncAfterBootstrap run functions before certain entry Bootstrap()

func (*Boot) AddHookFuncBeforeBootstrap

func (boot *Boot) AddHookFuncBeforeBootstrap(entryType, entryName string, f func(ctx context.Context))

AddHookFuncBeforeBootstrap run functions before certain entry Bootstrap()

func (*Boot) AddShutdownHookFunc

func (boot *Boot) AddShutdownHookFunc(name string, f rkentry.ShutdownHook)

AddShutdownHookFunc add shutdown hook function

func (*Boot) Bootstrap

func (boot *Boot) Bootstrap(ctx context.Context)

Bootstrap entries as sequence of plugin, user defined and web framework

func (*Boot) WaitForShutdownSig

func (boot *Boot) WaitForShutdownSig(ctx context.Context)

WaitForShutdownSig wait for shutdown signal. 1: Call shutdown hook function added by user. 2: Call interrupt function of entries in rkentry.GlobalAppCtx.

type BootOption

type BootOption func(*Boot)

BootOption is used as options while bootstrapping from code

func WithBootConfigPath

func WithBootConfigPath(filePath string, fs *embed.FS) BootOption

WithBootConfigPath provide boot config yaml file.

func WithBootConfigRaw

func WithBootConfigRaw(raw []byte) BootOption

WithBootConfigRaw provide boot config as string.

Jump to

Keyboard shortcuts

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