github.com/mdaxf/iac@v0.0.0-20240519030858-58a061660378/controllers/function/function.go (about)

     1  package function
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"time"
     7  
     8  	//"log"
     9  	"net/http"
    10  
    11  	"github.com/gin-gonic/gin"
    12  
    13  	"github.com/mdaxf/iac/controllers/common"
    14  
    15  	funcs "github.com/mdaxf/iac/engine/function"
    16  	"github.com/mdaxf/iac/engine/types"
    17  	"github.com/mdaxf/iac/logger"
    18  )
    19  
    20  type FunctionController struct {
    21  }
    22  
    23  type FuncData struct {
    24  	Content string      `json:"content"`
    25  	Inputs  interface{} `json:"inputs"`
    26  	Outputs []string    `json:"outputs"`
    27  	Type    int         `json:"type"`
    28  }
    29  
    30  // TestExecFunction is a controller function that tests the execution of a function.
    31  // It receives a request context and returns the test results as JSON.
    32  // The function logs performance metrics and any errors encountered during execution.
    33  // It supports two types of functions: Go expressions and JavaScript functions.
    34  // For Go expressions, it uses the GoExprFuncs package to execute the function.
    35  // For JavaScript functions, it uses the JSFuncs package to execute the function.
    36  // If the function type is not supported, it returns an error response.
    37  
    38  func (f *FunctionController) TestExecFunction(c *gin.Context) {
    39  	iLog := logger.Log{ModuleName: logger.API, User: "System", ControllerName: "Function"}
    40  	startTime := time.Now()
    41  	defer func() {
    42  		elapsed := time.Since(startTime)
    43  		iLog.PerformanceWithDuration("controllers.function.TestExecFunction", elapsed)
    44  	}()
    45  	/*
    46  		defer func() {
    47  			if err := recover(); err != nil {
    48  				iLog.Error(fmt.Sprintf("DeleteDataFromTable error: %s", err))
    49  				c.JSON(http.StatusBadRequest, gin.H{"error": err})
    50  			}
    51  		}() */
    52  	/*	_, user, clientid, err := common.GetRequestUser(c)
    53  		if err != nil {
    54  			iLog.Error(fmt.Sprintf("Get user information Error: %v", err))
    55  			c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
    56  			return
    57  		}
    58  		iLog.ClientID = clientid
    59  		iLog.User = user
    60  
    61  		iLog.Debug(fmt.Sprintf("Test Exec Function"))
    62  
    63  		body, err := common.GetRequestBody(c)
    64  
    65  		if err != nil {
    66  			iLog.Error(fmt.Sprintf("Test Exec Function error: %s", err.Error()))
    67  			c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
    68  			return
    69  		} */
    70  	body, clientid, user, err := common.GetRequestBodyandUser(c)
    71  	if err != nil {
    72  		iLog.Error(fmt.Sprintf("Error reading body: %v", err))
    73  		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
    74  		return
    75  	}
    76  	iLog.ClientID = clientid
    77  	iLog.User = user
    78  
    79  	var funcdata FuncData
    80  	err = json.Unmarshal(body, &funcdata)
    81  	if err != nil {
    82  		iLog.Error(fmt.Sprintf("Test Exec Function get the message body error: %s", err.Error()))
    83  		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
    84  		return
    85  	}
    86  
    87  	iLog.Debug(fmt.Sprintf("Test Exec Function funcdata: %v", funcdata))
    88  
    89  	if funcdata.Type == int(types.GoExpr) {
    90  		gofuncs := funcs.GoExprFuncs{}
    91  		outputs, err := gofuncs.Testfunction(funcdata.Content, funcdata.Inputs, funcdata.Outputs)
    92  
    93  		if err != nil {
    94  			iLog.Error(fmt.Sprintf("Test Exec Function error: %s", err.Error()))
    95  			c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
    96  			return
    97  		}
    98  		c.JSON(http.StatusOK, gin.H{"data": outputs})
    99  		return
   100  	} else if funcdata.Type == int(types.Javascript) {
   101  		jsfuncs := funcs.JSFuncs{}
   102  		outputs, err := jsfuncs.Testfunction(funcdata.Content, funcdata.Inputs, funcdata.Outputs)
   103  
   104  		if err != nil {
   105  			iLog.Error(fmt.Sprintf("Test Exec Function error: %s", err.Error()))
   106  			c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
   107  			return
   108  		}
   109  		c.JSON(http.StatusOK, gin.H{"data": outputs})
   110  		return
   111  	}
   112  
   113  	c.JSON(http.StatusBadRequest, gin.H{"error": "not supported type"})
   114  	return
   115  }