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:
2022-03-24 00:49:23 +01:00
parent b22cbcaf78
commit 67e8485958
8 changed files with 348 additions and 41 deletions

44
api/main.go Normal file
View 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"})
}
}