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.
46 lines
933 B
46 lines
933 B
package main
|
|
|
|
import (
|
|
web "json-gen/dist"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/gin-contrib/cors"
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/miaogaolin/gotl/common/sql2gorm/parser"
|
|
)
|
|
|
|
func main() {
|
|
r := gin.Default()
|
|
r.Use(Cors())
|
|
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.Run(":8000")
|
|
}
|
|
|
|
func Cors() gin.HandlerFunc {
|
|
return cors.New(cors.Config{
|
|
AllowAllOrigins: true,
|
|
AllowCredentials: true,
|
|
AllowHeaders: []string{"*"},
|
|
MaxAge: time.Second * time.Duration(7200),
|
|
})
|
|
}
|