github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/cmd/srv-applet-mgr/apis/deploy/get.go (about)

     1  package deploy
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/machinefi/w3bstream/cmd/srv-applet-mgr/apis/middleware"
     7  	"github.com/machinefi/w3bstream/pkg/depends/kit/httptransport/httpx"
     8  	"github.com/machinefi/w3bstream/pkg/modules/vm"
     9  	"github.com/machinefi/w3bstream/pkg/types"
    10  )
    11  
    12  type GetInstanceByInstanceID struct {
    13  	httpx.MethodGet
    14  	InstanceID types.SFID `in:"path" name:"instanceID"`
    15  }
    16  
    17  func (r *GetInstanceByInstanceID) Path() string {
    18  	return "/instance/:instanceID"
    19  }
    20  
    21  func (r *GetInstanceByInstanceID) Output(ctx context.Context) (interface{}, error) {
    22  	ctx, err := middleware.MustCurrentAccountFromContext(ctx).
    23  		WithInstanceContextBySFID(ctx, r.InstanceID)
    24  	if err != nil {
    25  		return nil, err
    26  	}
    27  
    28  	ins := types.MustInstanceFromContext(ctx)
    29  	ins.State, _ = vm.GetInstanceState(ins.InstanceID)
    30  	return ins, nil
    31  }
    32  
    33  type GetInstanceByAppletID struct {
    34  	httpx.MethodGet
    35  	AppletID types.SFID `in:"path" name:"appletID"`
    36  }
    37  
    38  func (r *GetInstanceByAppletID) Path() string {
    39  	return "/applet/:appletID"
    40  }
    41  
    42  func (r *GetInstanceByAppletID) Output(ctx context.Context) (interface{}, error) {
    43  	ctx, err := middleware.MustCurrentAccountFromContext(ctx).
    44  		WithAppletContextBySFID(ctx, r.AppletID)
    45  	if err != nil {
    46  		return nil, err
    47  	}
    48  
    49  	ins := types.MustInstanceFromContext(ctx)
    50  	ins.State, _ = vm.GetInstanceState(ins.InstanceID)
    51  	return ins, nil
    52  }