Skip to content

Embed

User can use embedFS to provide static files to rk-boot.

Overview#

User can provide Swagger UI config file, API Docs config file and boot.yaml file to rk-boot with embedFS.

Quick start#

1.Install#

$ go get github.com/rookie-ninja/rk-boot/v2
$ go get github.com/rookie-ninja/rk-zero

2.Create boot.yaml#

zero:
  - name: greeter
    port: 8080
    enabled: true

3.Create main.go#

package main

import (
  "context"
  _ "embed"
  "encoding/json"
  "fmt"
  "github.com/rookie-ninja/rk-boot/v2"
  "github.com/rookie-ninja/rk-zero/boot"
  "github.com/zeromicro/go-zero/rest"
  "net/http"
)

//go:embed boot.yaml
var bootRaw []byte

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

  // Register handler
  zeroEntry := rkzero.GetZeroEntry("greeter")
  zeroEntry.Server.AddRoute(rest.Route{
    Method:  http.MethodGet,
    Path:    "/v1/greeter",
    Handler: Greeter,
  })

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

  boot.WaitForShutdownSig(context.TODO())
}

func Greeter(writer http.ResponseWriter, request *http.Request) {
  writer.WriteHeader(http.StatusOK)
  resp := &GreeterResponse{
    Message: fmt.Sprintf("Hello %s!", request.URL.Query().Get("name")),
  }
  bytes, _ := json.Marshal(resp)
  writer.Write(bytes)
}

type GreeterResponse struct {
  Message string
}

4.Start main.go#

$ go run main.go
2022-05-09T12:55:30.681+0800    INFO    boot/zero_entry.go:761  Bootstrap zeroEntry     {"eventId": "a18ffe3c-6aa5-4555-b721-5b9073de7e23", "entryName": "greeter", "entryType": "ZeroEntry"}
------------------------------------------------------------------------
endTime=2022-05-09T12:55:30.681732+08:00
startTime=2022-05-09T12:55:30.681643+08:00
elapsedNano=88747
timezone=CST
ids={"eventId":"a18ffe3c-6aa5-4555-b721-5b9073de7e23"}
app={"appName":"rk","appVersion":"local","entryName":"greeter","entryType":"ZeroEntry"}
env={"arch":"amd64","domain":"*","hostname":"lark.local","localIP":"192.168.1.104","os":"darwin"}
payloads={"zeroPort":8080}
counters={}
pairs={}
timing={}
remoteAddr=localhost
operation=Bootstrap
resCode=OK
eventStatus=Ended
EOE

5.Validate#

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

Swagger UI & Docs UI#

1.Swagger JSON in local FS#

├── docs
│   ├── swagger.json
│   └── swagger.yaml

2.Create boot.yaml#

zero:
  - name: greeter
    port: 8080
    enabled: true
    sw:
      enabled: true
    docs:
      enabled: true

3.Create main.go#

package main

import (
    "context"
    "embed"
    _ "embed"
    "encoding/json"
    "fmt"
    "github.com/rookie-ninja/rk-boot/v2"
    "github.com/rookie-ninja/rk-entry/v2/entry"
    "github.com/rookie-ninja/rk-zero/boot"
    "github.com/zeromicro/go-zero/rest"
    "net/http"
)

//go:embed boot.yaml
var bootRaw []byte

//go:embed ../../..
var docsFS embed.FS

//go:embed docs
var swFS embed.FS

func init() {
    rkentry.GlobalAppCtx.AddEmbedFS(rkentry.SWEntryType, "greeter", &docsFS)
    rkentry.GlobalAppCtx.AddEmbedFS(rkentry.DocsEntryType, "greeter", &swFS)
}

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

    // Register handler
    zeroEntry := rkzero.GetZeroEntry("greeter")
    zeroEntry.Server.AddRoute(rest.Route{
        Method:  http.MethodGet,
        Path:    "/v1/greeter",
        Handler: Greeter,
    })

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

    boot.WaitForShutdownSig(context.TODO())
}

func Greeter(writer http.ResponseWriter, request *http.Request) {
    writer.WriteHeader(http.StatusOK)
    resp := &GreeterResponse{
        Message: fmt.Sprintf("Hello %s!", request.URL.Query().Get("name")),
    }
    bytes, _ := json.Marshal(resp)
    writer.Write(bytes)
}

type GreeterResponse struct {
    Message string
}