You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
67 lines
2.0 KiB
67 lines
2.0 KiB
package main
|
|
|
|
import (
|
|
"fmt"
|
|
web "json-gen/dist"
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/miaogaolin/gotl/common/sql2gorm/parser"
|
|
)
|
|
|
|
func main() {
|
|
r := gin.Default()
|
|
r.POST("/sql", func(c *gin.Context) {
|
|
var req struct {
|
|
Content string `json:"content"`
|
|
}
|
|
err := c.BindJSON(&req)
|
|
if err != nil {
|
|
c.String(http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
res, err := parser.ParseSqlFormat(req.Content,
|
|
parser.WithGormType(),
|
|
parser.WithJsonTag(),
|
|
)
|
|
if err != nil {
|
|
c.String(http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
c.String(http.StatusOK, string(res))
|
|
})
|
|
r.StaticFS("/", http.FS(web.Static))
|
|
r.Use(Cors())
|
|
r.Run(":8000")
|
|
}
|
|
|
|
func Cors() gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
method := c.Request.Method
|
|
origin := c.Request.Header.Get("Origin") //请求头部
|
|
if origin != "" {
|
|
//接收客户端发送的origin (重要!)
|
|
c.Writer.Header().Set("Access-Control-Allow-Origin", origin)
|
|
//服务器支持的所有跨域请求的方法
|
|
c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE,UPDATE")
|
|
//允许跨域设置可以返回其他子段,可以自定义字段
|
|
c.Header("Access-Control-Allow-Headers", "Authorization, Content-Length, X-CSRF-Token, Token,session,Accept, Origin, Host, Connection, Accept-Encoding, Accept-Language,DNT, X-CustomHeader, Keep-Alive, User-Agent, X-Requested-With, If-Modified-Since, Cache-Control, Content-Type, Pragma")
|
|
// 允许浏览器(客户端)可以解析的头部 (重要)
|
|
c.Header("Access-Control-Expose-Headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers")
|
|
//允许客户端传递校验信息比如 cookie (重要)
|
|
c.Header("Access-Control-Allow-Credentials", "true")
|
|
}
|
|
|
|
//允许类型校验
|
|
if method == "OPTIONS" {
|
|
c.String(http.StatusOK, "OK!")
|
|
}
|
|
|
|
defer func() {
|
|
if err := recover(); err != nil {
|
|
fmt.Printf("Panic info is: %v", err)
|
|
}
|
|
}()
|
|
c.Next()
|
|
}
|
|
}
|