controller.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. package ctl
  2. import (
  3. "Yunlot/RunCode/context"
  4. service "Yunlot/RunCode/service"
  5. "Yunlot/logs"
  6. "Yunlot/models/Product"
  7. context2 "context"
  8. "fmt"
  9. context1 "github.com/beego/beego/v2/server/web/context"
  10. "io/ioutil"
  11. "net/http"
  12. "os"
  13. "strconv"
  14. "strings"
  15. "time"
  16. )
  17. var Go_Main string
  18. var Go_MainDATA = "\n// ---DATA---\nvar Data_hex = false\nvar Topic = \"/topic/snxxx1\"\nvar T_string = \"A456\"\nvar R_string = \"495051\"\n"
  19. func init() {
  20. // 获取 执行文件
  21. content, err := os.ReadFile("./RunCode/config/main.txt")
  22. if err != nil {
  23. panic(any(err))
  24. }
  25. //fmt.Println(string(content))
  26. Go_Main = string(content)
  27. }
  28. // LanguagesController Get /languages
  29. func LanguagesController(writer http.ResponseWriter, request *http.Request) {
  30. ctx := &context.Context{Writer: writer, Req: request}
  31. if !ctx.IsGet() {
  32. ctx.NotAllow()
  33. return
  34. }
  35. var languages []string
  36. for lang := range service.DockerRunner.Runners {
  37. languages = append(languages, lang)
  38. }
  39. _ = ctx.JSON(http.StatusOK, languages)
  40. return
  41. }
  42. // 根据不同语言生成代码 run 运行/编译
  43. func CreateCode(run bool, lang_int int, code string) string {
  44. switch lang_int {
  45. case 1: //Goland
  46. if run {
  47. code = "package main\n" + code + Go_Main
  48. } else {
  49. code = "package main\n" + code + Go_MainDATA + strings.Replace(Go_Main, "func main()", "func mainxx()", -1)
  50. }
  51. break
  52. case 2: // C
  53. 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"
  54. if run {
  55. code_list := strings.Split(code, "// ---DATA---")
  56. if len(code_list) != 2 {
  57. logs.Println("len(code_list) != 2,没找到 【// ---DATA---】 ")
  58. return code
  59. }
  60. code = "package main\n/*\n" + code_list[0] + zongjian + code_list[1] + Go_Main
  61. } else {
  62. code = "package main\n/*\n" + code + zongjian + Go_MainDATA + strings.Replace(Go_Main, "func main()", "func mainxx()", -1)
  63. }
  64. break
  65. }
  66. return code
  67. }
  68. // RunController Post /run?lang={lang}
  69. func RunController(writer http.ResponseWriter, request *http.Request) {
  70. ctx := &context.Context{Writer: writer, Req: request}
  71. if !ctx.IsPost() {
  72. ctx.NotAllow()
  73. return
  74. }
  75. lang_int, err := strconv.Atoi(ctx.Get("lang", ""))
  76. if err != nil {
  77. ctx.Bad("lang 参数不能为空!")
  78. return
  79. }
  80. lang := Product.ProductLangFind(lang_int)
  81. if lang == "" {
  82. ctx.Bad("lang 参数不能为空!")
  83. return
  84. } else {
  85. if !service.DockerRunner.RunnerExists(lang) {
  86. ctx.Bad(lang + "语言编译异常!")
  87. return
  88. }
  89. body, _ := ioutil.ReadAll(request.Body)
  90. if len(body) == 0 {
  91. ctx.Bad("Body 内容为空!")
  92. return
  93. }
  94. // 生成代码
  95. code := CreateCode(true, lang_int, string(body))
  96. cancelCtx, cancelFn := context2.WithTimeout(context2.Background(), time.Second*time.Duration(service.DockerRunner.Timeout))
  97. defer cancelFn()
  98. content, err := service.DockerRunner.Exec(cancelCtx, lang, code)
  99. if err == nil {
  100. content_list := strings.Split(string(content), "|-=&")
  101. fmt.Println("content_list:", content_list)
  102. if len(content_list) != 4 {
  103. ctx.Error("解析错误!!!" + string(content))
  104. return
  105. }
  106. ctx.RunOK(lang, "success", content_list[1], content_list[3], content_list[2])
  107. return
  108. }
  109. if err == service.TimeoutError {
  110. ctx.Timeout("运行超时!")
  111. return
  112. }
  113. ctx.Error(string(content))
  114. }
  115. }
  116. // RunController Post /run?lang={lang}
  117. func BuildController(Output *context1.BeegoOutput, writer http.ResponseWriter, request *http.Request) {
  118. ctx := &context.Context{Writer: writer, Req: request}
  119. if !ctx.IsPost() {
  120. ctx.NotAllow()
  121. return
  122. }
  123. lang_int, err := strconv.Atoi(ctx.Get("lang", ""))
  124. if err != nil {
  125. ctx.Bad("lang 参数不能为空!")
  126. return
  127. }
  128. lang := Product.ProductLangFind(lang_int)
  129. if lang == "" {
  130. ctx.Bad("lang 参数不能为空!")
  131. return
  132. } else {
  133. if !service.DockerRunner.RunnerExists(lang) {
  134. ctx.Bad(lang + "语言编译异常!")
  135. return
  136. }
  137. body, _ := ioutil.ReadAll(request.Body)
  138. if len(body) == 0 {
  139. ctx.Bad("Body 内容为空!")
  140. return
  141. }
  142. // 生成代码
  143. code := CreateCode(false, lang_int, string(body))
  144. //code := "package main\n" + string(body) + strings.Replace(Go_Main, "func main()", "func mainxx()", -1)
  145. cancelCtx, cancelFn := context2.WithTimeout(context2.Background(), time.Second*time.Duration(service.DockerRunner.Timeout))
  146. defer cancelFn()
  147. name_so := time.Now().UnixMilli()
  148. //fmt.Println("code:",code)
  149. content, err := service.DockerRunner.Build(cancelCtx, name_so, lang, code)
  150. fmt.Println("content:", content)
  151. if len(content) > 0 {
  152. ctx.Error(string(content))
  153. return
  154. }
  155. if err == nil {
  156. ctx.RunOK(lang, fmt.Sprintf("%d", name_so), "", "", "")
  157. return
  158. }
  159. // 编译超时
  160. //if err == service.TimeoutError {
  161. // ctx.Timeout("运行超时!")
  162. // return
  163. //}
  164. ctx.Error("E:" + string(content))
  165. }
  166. }
  167. func BuildGenerate(ProductProtr *Product.ProductProt) {
  168. lang := Product.ProductLangFind(ProductProtr.T_lang)
  169. if lang == "" {
  170. ProductProtr.T_log += time.Now().Format("2006-01-02 15:04:05") + " 找不到编译语言 \n"
  171. ProductProtr.T_State = 3
  172. ProductProtr.Update("T_State", "T_log")
  173. return
  174. }
  175. if !service.DockerRunner.RunnerExists(lang) {
  176. ProductProtr.T_log += time.Now().Format("2006-01-02 15:04:05") + " 编译语言异常 \n"
  177. ProductProtr.T_State = 3
  178. ProductProtr.Update("T_State", "T_log")
  179. return
  180. }
  181. body := ProductProtr.T_text
  182. if len(body) == 0 {
  183. ProductProtr.T_log += time.Now().Format("2006-01-02 15:04:05") + " 内容为空 \n"
  184. ProductProtr.T_State = 3
  185. ProductProtr.Update("T_State", "T_log")
  186. return
  187. }
  188. // 生成代码
  189. code := CreateCode(false, ProductProtr.T_lang, body)
  190. cancelCtx, cancelFn := context2.WithTimeout(context2.Background(), time.Second*time.Duration(service.DockerRunner.Timeout))
  191. defer cancelFn()
  192. name_so := time.Now().UnixMilli()
  193. //fmt.Println("code:",code)
  194. content, err := service.DockerRunner.Build(cancelCtx, name_so, lang, code)
  195. fmt.Println("content:", content)
  196. if len(content) > 0 {
  197. ProductProtr.T_log += time.Now().Format("2006-01-02 15:04:05") + " 编译异常:" + string(content) + " \n|-&"
  198. ProductProtr.T_State = 3
  199. ProductProtr.Update("T_State", "T_log")
  200. return
  201. }
  202. if err == nil {
  203. ProductProtr.T_analysis = fmt.Sprintf("%d", name_so)
  204. ProductProtr.T_log += time.Now().Format("2006-01-02 15:04:05") + " 编译成功! \n|-&"
  205. ProductProtr.T_State = 1
  206. ProductProtr.Update("T_analysis", "T_State", "T_log")
  207. return
  208. }
  209. // 编译超时
  210. //if err == service.TimeoutError {
  211. // ctx.Timeout("运行超时!")
  212. // return
  213. //}
  214. ProductProtr.T_log += time.Now().Format("2006-01-02 15:04:05") + " 编译异常:" + string(content) + " \n"
  215. ProductProtr.T_State = 3
  216. ProductProtr.Update("T_State", "T_log")
  217. return
  218. }