working interpreter for template files
This commit is contained in:
91
lang_templates/golang/basic_go_server.tmpl
Normal file
91
lang_templates/golang/basic_go_server.tmpl
Normal file
@ -0,0 +1,91 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"encoding/json"
|
||||
"log"
|
||||
"github.com/gorilla/mux"
|
||||
)
|
||||
|
||||
{{- range .AST.Definitions }}
|
||||
{{- if .Server }}
|
||||
// Server configuration
|
||||
const (
|
||||
HOST = "{{ .Server.Settings | getHost }}"
|
||||
PORT = {{ .Server.Settings | getPort }}
|
||||
)
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
|
||||
{{- range .AST.Definitions }}
|
||||
{{- if .Entity }}
|
||||
// {{ .Entity.Name }} represents {{ .Entity.Description }}
|
||||
type {{ .Entity.Name }} struct {
|
||||
{{- range .Entity.Fields }}
|
||||
{{ .Name | title }} {{ .Type | goType }} `json:"{{ .Name }}"{{ if .Required }} validate:"required"{{ end }}`
|
||||
{{- end }}
|
||||
}
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
|
||||
{{- $endpoints := slice }}
|
||||
{{- range .AST.Definitions }}
|
||||
{{- if .Endpoint }}
|
||||
{{- $endpoints = append $endpoints . }}
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
|
||||
{{- range $endpoints }}
|
||||
// {{ .Endpoint.Description }}
|
||||
func {{ .Endpoint.Path | pathToHandlerName }}{{ .Endpoint.Method | title }}Handler(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
|
||||
{{- if .Endpoint.Auth }}
|
||||
// TODO: Add authentication middleware
|
||||
{{- end }}
|
||||
|
||||
{{- range .Endpoint.Params }}
|
||||
{{- if eq .Source "path" }}
|
||||
vars := mux.Vars(r)
|
||||
{{ .Name }} := vars["{{ .Name }}"]
|
||||
{{- else if eq .Source "query" }}
|
||||
{{ .Name }} := r.URL.Query().Get("{{ .Name }}")
|
||||
{{- else if eq .Source "body" }}
|
||||
var {{ .Name }} {{ .Type | goType }}
|
||||
if err := json.NewDecoder(r.Body).Decode(&{{ .Name }}); err != nil {
|
||||
http.Error(w, "Invalid JSON", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
|
||||
{{- if .Endpoint.CustomLogic }}
|
||||
// Custom logic: {{ .Endpoint.CustomLogic }}
|
||||
{{- else }}
|
||||
// TODO: Implement {{ .Endpoint.Method }} {{ .Endpoint.Path }} logic
|
||||
{{- end }}
|
||||
|
||||
{{- if .Endpoint.Response }}
|
||||
{{- if eq .Endpoint.Response.Type "list" }}
|
||||
response := []{{ .Endpoint.Entity }}{}
|
||||
{{- else }}
|
||||
response := {{ .Endpoint.Entity }}{}
|
||||
{{- end }}
|
||||
json.NewEncoder(w).Encode(response)
|
||||
{{- else }}
|
||||
w.WriteHeader(http.StatusOK)
|
||||
{{- end }}
|
||||
}
|
||||
{{- end }}
|
||||
|
||||
func main() {
|
||||
router := mux.NewRouter()
|
||||
|
||||
{{- range $endpoints }}
|
||||
router.HandleFunc("{{ .Endpoint.Path }}", {{ .Endpoint.Path | pathToHandlerName }}{{ .Endpoint.Method | title }}Handler).Methods("{{ .Endpoint.Method }}")
|
||||
{{- end }}
|
||||
|
||||
fmt.Printf("Server starting on %s:%d\n", HOST, PORT)
|
||||
log.Fatal(http.ListenAndServe(fmt.Sprintf("%s:%d", HOST, PORT), router))
|
||||
}
|
Reference in New Issue
Block a user