-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
83 lines (71 loc) · 2.03 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// 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"
"embed"
_ "embed"
"fmt"
"github.com/rookie-ninja/rk-entry/v2/entry"
"github.com/rookie-ninja/rk-mux/boot"
"github.com/rookie-ninja/rk-mux/middleware"
"net/http"
)
// How to use embed.FS for:
//
// - boot.yaml
// - rkentry.DocsEntryType
// - rkentry.SWEntryType
// - rkentry.StaticFileHandlerEntryType
// - rkentry.CertEntry
//
// If we use embed.FS, then we only need one single binary file while packing.
// We suggest use embed.FS to pack swagger local file since rk-entry would use os.Getwd() to look for files
// if relative path was provided.
//
//go:embed docs
var docsFS embed.FS
func init() {
rkentry.GlobalAppCtx.AddEmbedFS(rkentry.SWEntryType, "greeter", &docsFS)
}
//go:embed boot.yaml
var boot []byte
// @title RK Swagger for Mux
// @version 1.0
// @description This is a greeter service with rk-boot.
func main() {
// Bootstrap preload entries
rkentry.BootstrapBuiltInEntryFromYAML(boot)
rkentry.BootstrapPluginEntryFromYAML(boot)
// Bootstrap gin entry from boot config
res := rkmux.RegisterMuxEntryYAML(boot)
// Get MuxEntry
muxEntry := res["greeter"].(*rkmux.MuxEntry)
// Use *mux.Router adding handler.
muxEntry.Router.NewRoute().Path("/v1/greeter").HandlerFunc(Greeter)
// Bootstrap mux entry
muxEntry.Bootstrap(context.Background())
// Wait for shutdown signal
rkentry.GlobalAppCtx.WaitForShutdownSig()
// Interrupt mux entry
muxEntry.Interrupt(context.Background())
}
// Greeter handler
// @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(writer http.ResponseWriter, req *http.Request) {
rkmuxmid.WriteJson(writer, http.StatusOK, &GreeterResponse{
Message: fmt.Sprintf("Hello %s!", req.URL.Query().Get("name")),
})
}
// Response.
type GreeterResponse struct {
Message string
}