9
"github.com/crc-org/vfkit/pkg/cmdline"
10
"github.com/gin-gonic/gin"
11
"github.com/sirupsen/logrus"
20
func NewEndpoint(input string) (*Endpoint, error) {
21
uri, err := parseRestfulURI(input)
25
scheme, err := toRestScheme(uri.Scheme)
36
func (ep *Endpoint) ToCmdLine() ([]string, error) {
37
args := []string{"--restful-uri"}
40
args = append(args, fmt.Sprintf("unix://%s", ep.Path))
42
args = append(args, fmt.Sprintf("tcp://%s%s", ep.Host, ep.Path))
44
return []string{}, nil
46
return []string{}, errors.New("invalid endpoint scheme")
51
// VFKitService is used for the restful service; it describes
52
// the variables of the service like host/path but also has
54
type VFKitService struct {
59
// Start initiates the already configured gin service
60
func (v *VFKitService) Start() {
65
err = v.router.Run(v.Host)
67
err = v.router.RunUnix(v.Path)
73
// NewServer creates a new restful service
74
func NewServer(inspector VirtualMachineInspector, stateHandler VirtualMachineStateHandler, endpoint string) (*VFKitService, error) {
76
ep, err := NewEndpoint(endpoint)
85
// Handlers for the restful service. This is where endpoints are defined.
86
r.GET("/vm/state", stateHandler.GetVMState)
87
r.POST("/vm/state", stateHandler.SetVMState)
88
r.GET("/vm/inspect", inspector.Inspect)
92
type VirtualMachineInspector interface {
93
Inspect(c *gin.Context)
96
type VirtualMachineStateHandler interface {
97
GetVMState(c *gin.Context)
98
SetVMState(c *gin.Context)
101
// parseRestfulURI validates the input URI and returns an URL object
102
func parseRestfulURI(inputURI string) (*url.URL, error) {
103
restURI, err := url.ParseRequestURI(inputURI)
107
scheme, err := toRestScheme(restURI.Scheme)
111
if scheme == TCP && len(restURI.Host) < 1 {
112
return nil, errors.New("invalid TCP uri: missing host")
114
if scheme == TCP && len(restURI.Path) > 0 {
115
return nil, errors.New("invalid TCP uri: path is forbidden")
117
if scheme == TCP && restURI.Port() == "" {
118
return nil, errors.New("invalid TCP uri: missing port")
120
if scheme == Unix && len(restURI.Path) < 1 {
121
return nil, errors.New("invalid unix uri: missing path")
123
if scheme == Unix && len(restURI.Host) > 0 {
124
return nil, errors.New("invalid unix uri: host is forbidden")
129
// toRestScheme converts a string to a ServiceScheme
130
func toRestScheme(s string) (ServiceScheme, error) {
131
switch strings.ToUpper(s) {
139
return None, fmt.Errorf("invalid scheme %s", s)
142
func validateRestfulURI(inputURI string) error {
143
if inputURI != cmdline.DefaultRestfulURI {
144
if _, err := parseRestfulURI(inputURI); err != nil {