rkboot

package module
v1.4.8 Latest Latest
Warning

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

Go to latest
Published: Jan 30, 2022 License: Apache-2.0 Imports: 6 Imported by: 17

README

rk-boot

build codecov Go Report Card License FOSSA Status

Table of Contents generated with DocToc

Important note

The version of v1.3.X imported rk-gin, rk-echo, rk-grpc and rk-gf in the go.mod file initially. User only needs to [go get rk-boot] for starting web framework.

From v1.4.X, rk-boot will not include those dependencies in one place.

Instead, we use multi-module repository for supported web frameworks.

Release Description Example
v1.4.x Use multi-module repository, [go get] submodule as needed for web frameworks go get github.com/rookie-ninja/rk-boot/gin
v1.3.x Use root-module repository, will import all web frameworks, not suggested! go get github.com/rookie-ninja/rk-boot

The version of submodule will follow version of rk-xxx dependencies.

For example, rk-grpc is currently at release of v1.2.15, so the latest version of submodule would be github.com/rookie-ninja/rk-boot/grpc@v1.2.15

Official document

rkdev.info

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.

Features

Supported web frameworks

Welcome to contribute your web framework dependencies into rk-boot family.

Start with docs and refer rk-gin as example.

Frameworks Status Tag Installation Dependency
gin-gonic/gin Stable v1.2.21 go get github.com/rookie-ninja/rk-boot/gin rk-gin
gRPC Stable v1.2.25 go get github.com/rookie-ninja/rk-boot/grpc rk-grpc
labstack/echo Stable v0.0.16 go get github.com/rookie-ninja/rk-boot/echo rk-echo
gogf/gf Stable v0.0.14 go get github.com/rookie-ninja/rk-boot/gf rk-gf
gofiber/fiber Testing v0.0.11 go get github.com/rookie-ninja/rk-boot/fiber rk-fiber
zeromicro/go-zero Testing v0.0.10 go get github.com/rookie-ninja/rk-boot/zero rk-zero
gorilla/mux Testing v0.0.9 go get github.com/rookie-ninja/rk-boot/mux rk-mux
Supported database ORM

Databases still in Testing stage. Please see examples for detail.

Database Status Tag ORM Installation Dependency
MySQL Stable v0.0.5 gorm go get github.com/rookie-ninja/rk-db/mysql rk-db/mysql
SQLite Stable v0.0.3 gorm go get github.com/rookie-ninja/rk-db/sqlite rk-db/sqlite
SQL Server Stable v0.0.3 gorm go get github.com/rookie-ninja/rk-db/sqlserver rk-db/sqlserver
postgreSQL Stable v0.0.3 gorm go get github.com/rookie-ninja/rk-db/postgres rk-db/postgres
ClickHouse Stable v0.0.3 gorm go get github.com/rookie-ninja/rk-db/clickhouse rk-db/clickhouse

Examples

Name Type Example Docs
gin-gonic/gin Web Framework example docs
gRPC Web Framework example docs
labstack/echo Web Framework example docs
gogf/gf Web Framework example docs
gofiber/fiber Web Framework example docs
zeromicro/go-zero Web Framework example docs
gorilla/mux Web Framework example docs
MySQL ORM - gorm example docs
SQLite ORM - gorm example docs
SQL Server ORM - gorm example docs
postgreSQL ORM - gorm example docs
ClickHouse ORM - gorm example docs

Quick Start for Gin

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

  • Installation

go get github.com/rookie-ninja/rk-boot/gin

  • boot.yaml
---
gin:
  - name: greeter       # Required
    port: 8080          # Required
    enabled: true       # Required
    sw:
      enabled: true     # Optional, enable swagger UI via /sw by default
    commonService:
      enabled: true     # Optional, enable common API like /rk/v1/healthy
    tv:
      enabled:  true    # Optional, enable RK TV via /rk/v1/tv
  • 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"
	"github.com/gin-gonic/gin"
	"github.com/rookie-ninja/rk-boot"
	"github.com/rookie-ninja/rk-boot/gin"
	"net/http"
)

// Application entrance.
func main() {
	// Create a new boot instance.
	boot := rkboot.NewBoot()

	// Register handler
	ginEntry := rkbootgin.GetGinEntry("greeter")
	ginEntry.Router.GET("/v1/greeter", func(ctx *gin.Context) {
		ctx.JSON(http.StatusOK, "Hello!")
	})
	
	// Bootstrap
	boot.Bootstrap(context.Background())

	// Wait for shutdown sig
	boot.WaitForShutdownSig(context.Background())
}
  • validate
$ go run main.go

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

$ curl -X GET localhost:8080/v1/greeter
Hello!

gin-sw

gin-tv

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 {
	BootConfigPath string `yaml:"bootConfigPath" json:"bootConfigPath"`
	EventId        string `yaml:"eventId" json:"eventId"`
}

Boot is a structure for bootstrapping rk style application

func NewBoot

func NewBoot(opts ...BootOption) *Boot

NewBoot create a bootstrapper.

func (*Boot) AddShutdownHookFunc added in v1.2.0

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 in rkentry.GlobalAppCtx including bellow:

Internal entries: 1: rkentry.AppInfoEntry 2: rkentry.ConfigEntry 3: rkentry.ZapLoggerEntry 4: rkentry.EventLoggerEntry 5: rkentry.CertEntry

External entries: User defined entries

func (*Boot) GetAppInfoEntry added in v1.2.0

func (boot *Boot) GetAppInfoEntry() *rkentry.AppInfoEntry

GetAppInfoEntry returns rkentry.AppInfoEntry from rkentry.GlobalAppCtx.

func (*Boot) GetCertEntry added in v1.2.0

func (boot *Boot) GetCertEntry(name string) *rkentry.CertEntry

GetCertEntry returns rkentry.CertEntry from rkentry.GlobalAppCtx.

func (*Boot) GetConfigEntry added in v1.2.0

func (boot *Boot) GetConfigEntry(name string) *rkentry.ConfigEntry

GetConfigEntry returns rkentry.ConfigEntry from rkentry.GlobalAppCtx.

func (*Boot) GetEntry

func (boot *Boot) GetEntry(name string) interface{}

GetEntry returns rkentry.Entry interface which user needs to convert by himself.

func (*Boot) GetEventLoggerEntry added in v1.2.0

func (boot *Boot) GetEventLoggerEntry(name string) *rkentry.EventLoggerEntry

GetEventLoggerEntry returns rkentry.EventLoggerEntry from rkentry.GlobalAppCtx.

func (*Boot) GetEventLoggerEntryDefault added in v1.2.0

func (boot *Boot) GetEventLoggerEntryDefault() *rkentry.EventLoggerEntry

GetEventLoggerEntryDefault returns default rkentry.EventLoggerEntry from rkentry.GlobalAppCtx.

func (*Boot) GetZapLoggerEntry added in v1.2.0

func (boot *Boot) GetZapLoggerEntry(name string) *rkentry.ZapLoggerEntry

GetZapLoggerEntry returns rkentry.ZapLoggerEntry from rkentry.GlobalAppCtx.

func (*Boot) GetZapLoggerEntryDefault added in v1.2.0

func (boot *Boot) GetZapLoggerEntryDefault() *rkentry.ZapLoggerEntry

GetZapLoggerEntryDefault returns default rkentry.ZapLoggerEntry from rkentry.GlobalAppCtx.

func (*Boot) WaitForShutdownSig added in v1.2.0

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) BootOption

WithBootConfigPath provide boot config yaml file.

Directories

Path Synopsis
database
clickhouse Module
mysql Module
postgres Module
sqlite Module
sqlserver Module
echo module
fiber module
gf module
gin module
grpc module
mux module
zero module

Jump to

Keyboard shortcuts

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