This repository has been archived by the owner on Jan 10, 2022. It is now read-only.
forked from uadmin/uadmin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmicroservice_example.go
133 lines (121 loc) · 3.29 KB
/
microservice_example.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Schemes: http, https
// Host: localhost
// BasePath: /v2
// Version: 0.0.1
// License: MIT http://opensource.org/licenses/MIT
// Contact: John Doe<[email protected]> http://john.doe.com
//
// Consumes:
// - application/json
// - application/xml
//
// Produces:
// - application/json
// - application/xml
//
// Security:
// - api_key:
//
// SecurityDefinitions:
// api_key:
// type: apiKey
// name: KEY
// in: header
// oauth2:
// type: oauth2
// authorizationUrl: /oauth2/auth
// tokenUrl: /oauth2/token
// in: header
// scopes:
// bar: foo
// flow: accessCode
//
// Extensions:
// x-meta-value: value
// x-meta-array:
// - value1
// - value2
// x-meta-array-obj:
// - name: obj
// value: field
//
// swagger:meta
package uadmin
import (
"fmt"
"github.com/jessevdk/go-flags"
interfaces3 "github.com/sergeyglazyrindev/uadmin/blueprint/auth/interfaces"
"github.com/sergeyglazyrindev/uadmin/core"
"os"
)
type MicroserviceExample struct {
core.Microservice
}
// A ValidationError is an error that is used when the required input fails validation.
// swagger:response validationError
type ValidationError struct {
Code string
Message string
Params []string
}
// A GeneralError is an error that is used when something strange happened
// swagger:response generalError
type GeneralError struct {
Error string
}
type MicroserviceExampleCommand struct {
}
func (c MicroserviceExampleCommand) Proceed(subaction string, args []string) error {
var action string
var help string
var isCorrectActionPassed bool = false
commandRegistry := &core.CommandRegistry{
Actions: make(map[string]core.ICommand),
}
commandRegistry.AddAction("start", &MicroserviceExampleStartCommand{})
if len(os.Args) > 2 {
action = os.Args[2]
isCorrectActionPassed = commandRegistry.IsRegisteredCommand(action)
}
if !isCorrectActionPassed {
helpText := commandRegistry.MakeHelpText()
help = fmt.Sprintf(`
Please provide what do you want to do ?
%s
`, helpText)
fmt.Print(help)
return nil
}
return commandRegistry.RunAction(subaction, "", args)
}
func (c MicroserviceExampleCommand) GetHelpText() string {
return "Start your example microservice"
}
type MicroserviceExampleStartOptions struct {
StartSwagger bool `long:"swagger" description:"Start swagger for your microservice"`
}
type MicroserviceExampleStartCommand struct {
}
func (command MicroserviceExampleStartCommand) Proceed(subaction string, args []string) error {
var opts = &MicroserviceExampleStartOptions{}
parser := flags.NewParser(opts, flags.Default)
var err error
_, err = parser.ParseArgs(args)
if err != nil {
return err
}
appInstance.GetAuthAdapterRegistry().RegisterNewAdapter(&interfaces3.TokenAuthProvider{})
microservice := &MicroserviceExample{Microservice: core.Microservice{
Port: 8089, AuthBackend: "token", Name: "Example microservice",
Prefix: "ExampleMicroservice", SwaggerPort: 8090,
}}
if opts.StartSwagger {
return microservice.StartSwagger(appInstance)
}
r := microservice.RegisterEndpoints(appInstance)
microservice.Start(r)
return nil
}
func (command MicroserviceExampleStartCommand) GetHelpText() string {
return "Start your microservice"
}