/
root.go
110 lines (92 loc) · 4.43 KB
/
root.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
// Code generated by protoc-gen-gomeet-service. DO NOT EDIT.
// source: pb/profile.proto
package cmd
import (
"fmt"
"os"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/gomeet-examples/svc-profile/service"
)
var (
cfgFile string
caCertificate string
serverCertificate string
serverPrivateKey string
clientCertificate string
clientPrivateKey string
timeoutSeconds int
jwtToken string
svc = service.NewService()
svcName = fmt.Sprintf("%s-%s", svc.Name, svc.Version)
// RootCmd This represents the base command when called without any subcommands
RootCmd = &cobra.Command{
Use: "svc-profile",
Short: "Gomeetexamples's profile microservice",
Long: `To get started run the serve subcommand which will start a server:
$ svc-profile serve
$ svc-profile serve -p 42000
Then you can hit it with the client:
$ svc-profile cli version
$ svc-profile cli services_status
$ svc-profile cli create <gender [UNKNOW|MALE|FEMALE]> <email [string]> <name [string]> <birthday [string]>
$ svc-profile cli read <uuid [string]>
$ svc-profile cli list <page_number [uint32]> <page_size [uint32]> <order [string]> <exclude_soft_deleted [bool]> <soft_deleted_only [bool]> <gender [UNKNOW|MALE|FEMALE]>
$ svc-profile cli update <uuid [string]> <gender [UNKNOW|MALE|FEMALE]> <email [string]> <name [string]> <birthday [string]> <created_at [string]> <updated_at [string]> <deleted_at [string]>
$ svc-profile cli soft_delete <uuid [string]>
$ svc-profile cli hard_delete <uuid [string]>
$ svc-profile cli --address localhost:42000 version
Or over HTTP/1.1 with curl:
$ curl -X GET http://localhost:13000/api/v1/version
$ curl -X GET http://localhost:13000/api/v1/services/status
$ curl -X POST http://localhost:13000/api/v1/create -d '{"gender": "UNKNOW|MALE|FEMALE", "email": "<string>", "name": "<string>", "birthday": "<string>"}'
$ curl -X POST http://localhost:13000/api/v1/read -d '{"uuid": "<string>"}'
$ curl -X POST http://localhost:13000/api/v1/list -d '{"page_number": <number>, "page_size": <number>, "order": "<string>", "exclude_soft_deleted": <boolean>, "soft_deleted_only": <boolean>, "gender": "UNKNOW|MALE|FEMALE"}'
$ curl -X POST http://localhost:13000/api/v1/update -d '{"uuid": "<string>", "gender": "UNKNOW|MALE|FEMALE", "email": "<string>", "name": "<string>", "birthday": "<string>", "created_at": "<string>", "updated_at": "<string>", "deleted_at": "<string>"}'
$ curl -X POST http://localhost:13000/api/v1/soft_delete -d '{"uuid": "<string>"}'
$ curl -X POST http://localhost:13000/api/v1/hard_delete -d '{"uuid": "<string>"}'
$ curl -X GET http://localhost:13000/
$ curl -X GET http://localhost:13000/version
$ curl -X GET http://localhost:13000/metrics
$ curl -X GET http://localhost:13000/status
$ curl -X GET http://localhost:42000/version
Or via an interactive console :
$ svc-profile console
$ svc-profile console --address localhost:42000
`,
// Uncomment the following line if your bare application
// has an action associated with it:
// Run: func(cmd *cobra.Command, args []string) { },
}
)
// Execute adds all child commands to the root command sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the RootCmd.
func Execute() {
if err := RootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(-1)
}
}
func init() {
cobra.OnInitialize(initConfig)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags, which, if defined here,
// will be global for your application.
RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.svc-profile.yaml)")
// Cobra also supports local flags, which will only run
// when this action is called directly.
// RootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
// initConfig reads in config file and ENV variables if set.
func initConfig() {
if cfgFile != "" { // enable ability to specify config file via flag
viper.SetConfigFile(cfgFile)
}
viper.SetConfigName(".svc-profile") // name of config file (without extension)
viper.AddConfigPath("$HOME") // adding home directory as first search path
viper.AutomaticEnv() // read in environment variables that match
// If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
fmt.Println("Using config file:", viper.ConfigFileUsed())
}
}