feat(api): adding api server code
templater can be used as a commmand line tool or as an api server.
This commit is contained in:
44
api/main.go
Normal file
44
api/main.go
Normal file
@ -0,0 +1,44 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"forge.cadoles.com/pcaseiro/templatefile/pkg/templater"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type Template struct {
|
||||
Type string
|
||||
Content string
|
||||
Config string
|
||||
}
|
||||
|
||||
func Generate(c *gin.Context) {
|
||||
var template Template
|
||||
|
||||
err := c.Request.ParseForm()
|
||||
if err != nil {
|
||||
c.String(500, err.Error())
|
||||
}
|
||||
|
||||
err = c.ShouldBindJSON(&template)
|
||||
if err != nil {
|
||||
c.String(500, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
templateType := template.Type
|
||||
templateFile := template.Content
|
||||
config := []byte(template.Config)
|
||||
res := ""
|
||||
if templateType == "go" {
|
||||
res = templater.ProcessGoTemplate(templateFile, config)
|
||||
c.JSON(http.StatusOK, gin.H{"data": res})
|
||||
} else if templateType == "hcl" {
|
||||
res = templater.ProcessHCLTemplate(templateFile, config)
|
||||
c.JSON(http.StatusOK, gin.H{"data": res})
|
||||
} else {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"data": "Unkown template type"})
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user