123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244 |
- package ctl
- import (
- "Yunlot/RunCode/context"
- service "Yunlot/RunCode/service"
- "Yunlot/logs"
- "Yunlot/models/Product"
- context2 "context"
- "fmt"
- context1 "github.com/beego/beego/v2/server/web/context"
- "io/ioutil"
- "net/http"
- "os"
- "strconv"
- "strings"
- "time"
- )
- var Go_Main string
- var Go_MainDATA = "\n// ---DATA---\nvar Data_hex = false\nvar Topic = \"/topic/snxxx1\"\nvar T_string = \"A456\"\nvar R_string = \"495051\"\n"
- func init() {
- // 获取 执行文件
- content, err := os.ReadFile("./RunCode/config/main.txt")
- if err != nil {
- panic(any(err))
- }
- //fmt.Println(string(content))
- Go_Main = string(content)
- }
- // LanguagesController Get /languages
- func LanguagesController(writer http.ResponseWriter, request *http.Request) {
- ctx := &context.Context{Writer: writer, Req: request}
- if !ctx.IsGet() {
- ctx.NotAllow()
- return
- }
- var languages []string
- for lang := range service.DockerRunner.Runners {
- languages = append(languages, lang)
- }
- _ = ctx.JSON(http.StatusOK, languages)
- return
- }
- // 根据不同语言生成代码 run 运行/编译
- func CreateCode(run bool, lang_int int, code string) string {
- switch lang_int {
- case 1: //Goland
- if run {
- code = "package main\n" + code + Go_Main
- } else {
- code = "package main\n" + code + Go_MainDATA + strings.Replace(Go_Main, "func main()", "func mainxx()", -1)
- }
- break
- case 2: // C
- zongjian := "\n*/\nimport \"C\"\n\n/////-------\nimport (\n\t\"encoding/hex\"\n\t\"fmt\"\n\t\"strings\"\n\t\"unsafe\"\n)\n\n/*\n设备->平台\n*/\nfunc T(t string, b []byte) string {\n\tvar r_b *C.char\n\tC.T(C.CString(t), C.CString(string(b)), &r_b)\n\tr_b_ := C.GoString(r_b)\n\tC.free(unsafe.Pointer(r_b)) // Free the memory allocated in C code\n\treturn r_b_\n}\n\n/*\n平台->设备\n*/\nfunc R(sn string, b string) (string, []byte) {\n\tvar r_t,r_b *C.char\n\tC.R(C.CString(sn), C.CString(b), &r_t, &r_b)\n\tr_t_ := C.GoString(r_t)\n\tr_b_ := C.GoString(r_b)\n\tC.free(unsafe.Pointer(r_t)) // Free the memory allocated in C code\n\tC.free(unsafe.Pointer(r_b)) // Free the memory allocated in C code\n\n\treturn r_t_, []byte(r_b_)\n}\n"
- if run {
- code_list := strings.Split(code, "// ---DATA---")
- if len(code_list) != 2 {
- logs.Println("len(code_list) != 2,没找到 【// ---DATA---】 ")
- return code
- }
- code = "package main\n/*\n" + code_list[0] + zongjian + code_list[1] + Go_Main
- } else {
- code = "package main\n/*\n" + code + zongjian + Go_MainDATA + strings.Replace(Go_Main, "func main()", "func mainxx()", -1)
- }
- break
- }
- return code
- }
- // RunController Post /run?lang={lang}
- func RunController(writer http.ResponseWriter, request *http.Request) {
- ctx := &context.Context{Writer: writer, Req: request}
- if !ctx.IsPost() {
- ctx.NotAllow()
- return
- }
- lang_int, err := strconv.Atoi(ctx.Get("lang", ""))
- if err != nil {
- ctx.Bad("lang 参数不能为空!")
- return
- }
- lang := Product.ProductLangFind(lang_int)
- if lang == "" {
- ctx.Bad("lang 参数不能为空!")
- return
- } else {
- if !service.DockerRunner.RunnerExists(lang) {
- ctx.Bad(lang + "语言编译异常!")
- return
- }
- body, _ := ioutil.ReadAll(request.Body)
- if len(body) == 0 {
- ctx.Bad("Body 内容为空!")
- return
- }
- // 生成代码
- code := CreateCode(true, lang_int, string(body))
- cancelCtx, cancelFn := context2.WithTimeout(context2.Background(), time.Second*time.Duration(service.DockerRunner.Timeout))
- defer cancelFn()
- content, err := service.DockerRunner.Exec(cancelCtx, lang, code)
- if err == nil {
- content_list := strings.Split(string(content), "|-=&")
- fmt.Println("content_list:", content_list)
- if len(content_list) != 4 {
- ctx.Error("解析错误!!!" + string(content))
- return
- }
- ctx.RunOK(lang, "success", content_list[1], content_list[3], content_list[2])
- return
- }
- if err == service.TimeoutError {
- ctx.Timeout("运行超时!")
- return
- }
- ctx.Error(string(content))
- }
- }
- // RunController Post /run?lang={lang}
- func BuildController(Output *context1.BeegoOutput, writer http.ResponseWriter, request *http.Request) {
- ctx := &context.Context{Writer: writer, Req: request}
- if !ctx.IsPost() {
- ctx.NotAllow()
- return
- }
- lang_int, err := strconv.Atoi(ctx.Get("lang", ""))
- if err != nil {
- ctx.Bad("lang 参数不能为空!")
- return
- }
- lang := Product.ProductLangFind(lang_int)
- if lang == "" {
- ctx.Bad("lang 参数不能为空!")
- return
- } else {
- if !service.DockerRunner.RunnerExists(lang) {
- ctx.Bad(lang + "语言编译异常!")
- return
- }
- body, _ := ioutil.ReadAll(request.Body)
- if len(body) == 0 {
- ctx.Bad("Body 内容为空!")
- return
- }
- // 生成代码
- code := CreateCode(false, lang_int, string(body))
- //code := "package main\n" + string(body) + strings.Replace(Go_Main, "func main()", "func mainxx()", -1)
- cancelCtx, cancelFn := context2.WithTimeout(context2.Background(), time.Second*time.Duration(service.DockerRunner.Timeout))
- defer cancelFn()
- name_so := time.Now().UnixMilli()
- //fmt.Println("code:",code)
- content, err := service.DockerRunner.Build(cancelCtx, name_so, lang, code)
- fmt.Println("content:", content)
- if len(content) > 0 {
- ctx.Error(string(content))
- return
- }
- if err == nil {
- ctx.RunOK(lang, fmt.Sprintf("%d", name_so), "", "", "")
- return
- }
- // 编译超时
- //if err == service.TimeoutError {
- // ctx.Timeout("运行超时!")
- // return
- //}
- ctx.Error("E:" + string(content))
- }
- }
- func BuildGenerate(ProductProtr *Product.ProductProt) {
- lang := Product.ProductLangFind(ProductProtr.T_lang)
- if lang == "" {
- ProductProtr.T_log += time.Now().Format("2006-01-02 15:04:05") + " 找不到编译语言 \n"
- ProductProtr.T_State = 3
- ProductProtr.Update("T_State", "T_log")
- return
- }
- if !service.DockerRunner.RunnerExists(lang) {
- ProductProtr.T_log += time.Now().Format("2006-01-02 15:04:05") + " 编译语言异常 \n"
- ProductProtr.T_State = 3
- ProductProtr.Update("T_State", "T_log")
- return
- }
- body := ProductProtr.T_text
- if len(body) == 0 {
- ProductProtr.T_log += time.Now().Format("2006-01-02 15:04:05") + " 内容为空 \n"
- ProductProtr.T_State = 3
- ProductProtr.Update("T_State", "T_log")
- return
- }
- // 生成代码
- code := CreateCode(false, ProductProtr.T_lang, body)
- cancelCtx, cancelFn := context2.WithTimeout(context2.Background(), time.Second*time.Duration(service.DockerRunner.Timeout))
- defer cancelFn()
- name_so := time.Now().UnixMilli()
- //fmt.Println("code:",code)
- content, err := service.DockerRunner.Build(cancelCtx, name_so, lang, code)
- fmt.Println("content:", content)
- if len(content) > 0 {
- ProductProtr.T_log += time.Now().Format("2006-01-02 15:04:05") + " 编译异常:" + string(content) + " \n|-&"
- ProductProtr.T_State = 3
- ProductProtr.Update("T_State", "T_log")
- return
- }
- if err == nil {
- ProductProtr.T_analysis = fmt.Sprintf("%d", name_so)
- ProductProtr.T_log += time.Now().Format("2006-01-02 15:04:05") + " 编译成功! \n|-&"
- ProductProtr.T_State = 1
- ProductProtr.Update("T_analysis", "T_State", "T_log")
- return
- }
- // 编译超时
- //if err == service.TimeoutError {
- // ctx.Timeout("运行超时!")
- // return
- //}
- ProductProtr.T_log += time.Now().Format("2006-01-02 15:04:05") + " 编译异常:" + string(content) + " \n"
- ProductProtr.T_State = 3
- ProductProtr.Update("T_State", "T_log")
- return
- }
|