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

     1  package iacai
     2  
     3  import (
     4  	//"log"
     5  	"encoding/json"
     6  	"fmt"
     7  	"net/http"
     8  	"time"
     9  
    10  	"github.com/gin-gonic/gin"
    11  //	"github.com/mdaxf/iac/codegen"
    12  	"github.com/mdaxf/iac/config"
    13  	"github.com/mdaxf/iac/controllers/common"
    14  	"github.com/mdaxf/iac/logger"
    15  )
    16  
    17  type IACAIController struct {
    18  }
    19  
    20  type RequestBody struct {
    21  	Image string `json:"image"`
    22  	Text  string `json:"text"`
    23  	Grid  string `json:"grid"`
    24  	Theme string `json:"theme"`
    25  }
    26  
    27  // CreateAI handles the creation of a new AI.
    28  // It retrieves the request body and user information from the context,
    29  func (f *IACAIController) ImagetoHTML(c *gin.Context) {
    30  	iLog := logger.Log{ModuleName: logger.API, User: "System", ControllerName: "iacai"}
    31  	startTime := time.Now()
    32  	defer func() {
    33  		elapsed := time.Since(startTime)
    34  		iLog.PerformanceWithDuration("controllers.iacai.ImagetoHTML", elapsed)
    35  	}()
    36  	/*
    37  		defer func() {
    38  			if err := recover(); err != nil {
    39  				iLog.Error(fmt.Sprintf("IACAI.ImageToHTML error: %s", err))
    40  				c.JSON(http.StatusBadRequest, gin.H{"error": err})
    41  			}
    42  		}()
    43  	*/
    44  	body, clientid, user, err := common.GetRequestBodyandUser(c)
    45  	if err != nil {
    46  		iLog.Error(fmt.Sprintf("Error reading body: %v", err))
    47  		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
    48  		return
    49  	}
    50  	iLog.ClientID = clientid
    51  	iLog.User = user
    52  
    53  	iLog.Debug(fmt.Sprintf("Image to HTML: %v", body))
    54  
    55  	var data RequestBody
    56  
    57  	err = json.Unmarshal(body, &data)
    58  	if err != nil {
    59  		iLog.Error(fmt.Sprintf("Error unmarshalling body: %v", err))
    60  		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
    61  		return
    62  	}
    63  
    64  	iLog.Debug(fmt.Sprintf("Image to HTML: %v", data))
    65  /*
    66  	html, err := codegen.GetHtmlCodeFromImage(data.Image, config.OpenAiKey, data.Text, data.Grid, data.Theme)
    67  
    68  	if err != nil {
    69  		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
    70  		return
    71  	}
    72  */
    73  	c.JSON(http.StatusOK, gin.H{"data": html})
    74  
    75  }