I. INTRODUCTION to RK

RK (short for Rookie) is the open source Go language (Golang) framework. It facilitates the establishment of production-level restful and GRPC services.

Github:github.com/rookie-ninj…

RK official website: rkdev.info/cn

RK Demo code: github.com/rookie-ninj…

2. The Demo

This time I used RK to build a simple restful service.

1. Create the Go project

mkdir rk-hello
cd rk-hello
go mod init rk-hello
Copy the code

2. Install fairly rk – the boot

go get github.com/rookie-ninja/rk-boot
Copy the code

3. Create the boot.yaml file

gin:
  - name: greeter
    port: 8080
    sw:
      enabled: false
      jsonPath: "docs"
    commonService:
      enabled: true
    tv:
      enabled:  true
    prom:
      enabled: false
    interceptors:
      loggingZap:
        enabled: true
      metricsProm:
        enabled: true
      meta:
        enabled: true
Copy the code

4. Create a main. Go

package main

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

// @title RK Swagger for Gin
/ / @ version 1.0
// @description This is a greeter service with rk-boot.

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

	// Register handler
	boot.GetGinEntry("greeter").Router.GET("/v1/greeter", Greeter)

	// Bootstrap
	boot.Bootstrap(context.Background())

	// Wait for shutdown sig
	boot.WaitForShutdownSig(context.Background())
}

// Greeter @Summary Greeter service
// @Id 1
/ / @ version 1.0
// @produce application/json
// @Param name query string true "Input name"
// @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")),})}// GreeterResponse response
type GreeterResponse struct {
	Message string
}
Copy the code

5. Run

go run main.go 
Copy the code

6. View the current service status

Can through own fairly rk – TV component view: http://localhost:8080/rk/v1/tv/info

End of three.

The simple restful service is set up. Rk also has many features, including support for Swagger, promtheus and more, available on Github as well. If you have any questions, you can leave a message directly, or leave an issue on GitHub RK-boot project.

thank you