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))
|
||||
}
|
105
lang_templates/proto/application.proto.tmpl
Normal file
105
lang_templates/proto/application.proto.tmpl
Normal file
@ -0,0 +1,105 @@
|
||||
// Code generated by protoc-gen-go. DO NOT EDIT.
|
||||
syntax = "proto3";
|
||||
|
||||
package {{ .AppName }};
|
||||
|
||||
import "gorm/options/gorm.proto";
|
||||
//import "gorm/types/types.proto";
|
||||
import "google/api/annotations.proto";
|
||||
|
||||
option go_package = "./;pb";
|
||||
|
||||
option (grpc.gateway.protoc_gen_openapiv2.options.openapiv2_swagger) = {
|
||||
info: {
|
||||
title: "Your API Title"
|
||||
version: "v1.0"
|
||||
description: "Your API description"
|
||||
}
|
||||
host: "localhost:8080" // Set the server host
|
||||
};
|
||||
|
||||
service {{ .AppNameCaps }} {
|
||||
option (gorm.server).autogen = true;
|
||||
// Add your service methods here
|
||||
|
||||
rpc CreateProduct (CreateProductRequest) returns (CreateProductResponse) {
|
||||
option (google.api.http) = {
|
||||
post: "/v1/Product"
|
||||
body: "*"
|
||||
};
|
||||
}
|
||||
|
||||
rpc ReadProduct (ReadProductRequest) returns (ReadProductResponse) {
|
||||
option (google.api.http) = {
|
||||
get: "/v1/Product/{id}"
|
||||
};
|
||||
}
|
||||
|
||||
rpc ListProducts (ListProductsRequest) returns (ListProductsResponse) {
|
||||
option (google.api.http) = {
|
||||
get: "/v1/Product"
|
||||
};
|
||||
}
|
||||
|
||||
rpc UpdateProduct (UpdateProductRequest) returns (UpdateProductResponse) {
|
||||
option (google.api.http) = {
|
||||
put: "/v1/Product"
|
||||
body: "*"
|
||||
};
|
||||
}
|
||||
|
||||
rpc DeleteProduct (DeleteProductRequest) returns (DeleteProductResponse) {
|
||||
option (gorm.method).object_type = "Product";
|
||||
option (google.api.http) = {
|
||||
delete: "/v1/Product/{id}"
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
message Create{{ .ObjName }}Request {
|
||||
{{ .ObjName }} payload = 1;
|
||||
}
|
||||
|
||||
message Create{{ .ObjName }}Response {
|
||||
{{ .ObjName }} result = 1;
|
||||
}
|
||||
|
||||
message Read{{ .ObjName }}Request {
|
||||
uint64 id = 1;
|
||||
}
|
||||
|
||||
message Read{{ .ObjName }}Response {
|
||||
{{ .ObjName }} result = 1;
|
||||
}
|
||||
|
||||
message List{{ .ObjName }}sRequest {}
|
||||
|
||||
message List{{ .ObjName }}sResponse {
|
||||
repeated {{ .ObjName }} results = 1;
|
||||
}
|
||||
|
||||
message Update{{ .ObjName }}Request {
|
||||
{{ .ObjName }} payload = 1;
|
||||
}
|
||||
|
||||
message Update{{ .ObjName }}Response {
|
||||
{{ .ObjName }} result = 1;
|
||||
}
|
||||
|
||||
message Delete{{ .ObjName }}Request {
|
||||
uint64 id = 1;
|
||||
}
|
||||
|
||||
message Delete{{ .ObjName }}Response {}
|
||||
|
||||
message {{ .ObjName }} {
|
||||
option (gorm.opts).ormable = true;
|
||||
uint64 id = 1;
|
||||
// add object fields here
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user