Documentation
¶
Index ¶
- Constants
- Variables
- func ExecuteTemplate(tmpl *Template, params map[string]interface{}) (string, error)
- func GetConfigTemplate(serviceType, environment string) string
- func ListTemplates(templateType string) []string
- type APISpec
- type ConfigTemplate
- type EndpointDef
- type FieldDef
- type Template
- type TemplateParameter
- type TypeDef
Constants ¶
const APIConfigDevelopment = `` /* 138-byte string literal not displayed */
APIConfigDevelopment is the development config template for API services
const APIConfigProduction = `` /* 387-byte string literal not displayed */
APIConfigProduction is the production config template for API services
const APIConfigTest = `` /* 144-byte string literal not displayed */
APIConfigTest is the test config template for API services
const APISpecTemplate = `syntax = "v1"
info (
title: "{{.ServiceName}} API"
desc: "API specification for {{.ServiceName}}"
version: "1.0"
)
{{range .Types}}
type {{.Name}} {
{{range .Fields}} {{.Name}} {{.Type}} ` + "`json:\"{{.JsonTag}}\"`" + `{{if .Comment}} // {{.Comment}}{{end}}
{{end}}}
{{end}}
service {{.ServiceName}}-api {
{{range .Endpoints}} @handler {{.Handler}}
{{.Method}} {{.Path}}{{if .Request}} ({{.Request}}){{end}}{{if .Response}} returns ({{.Response}}){{end}}
{{end}}}
`
const AuthMiddlewareTemplate = `` /* 1146-byte string literal not displayed */
const BasicErrorHandlerTemplate = `package handler
import (
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
)
type ErrorResponse struct {
Code int ` + "`json:\"code\"`" + `
Message string ` + "`json:\"message\"`" + `
}
func ErrorHandler(err error) (int, interface{}) {
switch err.(type) {
default:
return http.StatusInternalServerError, ErrorResponse{
Code: http.StatusInternalServerError,
Message: "Internal server error",
}
}
}
`
const DetailedErrorHandlerTemplate = `package handler
import (
"net/http"
"time"
"fmt"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/rest/httpx"
)
type DetailedErrorResponse struct {
Code int ` + "`json:\"code\"`" + `
Message string ` + "`json:\"message\"`" + `
Details string ` + "`json:\"details,omitempty\"`" + `
Timestamp int64 ` + "`json:\"timestamp\"`" + `
Path string ` + "`json:\"path,omitempty\"`" + `
}
type ValidationError struct {
Field string
Message string
}
func (e ValidationError) Error() string {
return fmt.Sprintf("%s: %s", e.Field, e.Message)
}
type BusinessError struct {
Code int
Message string
}
func (e BusinessError) Error() string {
return e.Message
}
func ErrorHandler(err error) (int, interface{}) {
logx.Errorf("Error occurred: %v", err)
timestamp := time.Now().Unix()
switch e := err.(type) {
case ValidationError:
return http.StatusBadRequest, DetailedErrorResponse{
Code: http.StatusBadRequest,
Message: "Validation failed",
Details: e.Error(),
Timestamp: timestamp,
}
case BusinessError:
return e.Code, DetailedErrorResponse{
Code: e.Code,
Message: e.Message,
Timestamp: timestamp,
}
default:
return http.StatusInternalServerError, DetailedErrorResponse{
Code: http.StatusInternalServerError,
Message: "Internal server error",
Timestamp: timestamp,
}
}
}
`
const DockerfileTemplate = `` /* 394-byte string literal not displayed */
const KubernetesTemplate = `` /* 1301-byte string literal not displayed */
const LoggingMiddlewareTemplate = `` /* 1080-byte string literal not displayed */
const RPCConfigDevelopment = `Name: {{.ServiceName}}
ListenOn: 0.0.0.0:{{.Port}}
Log:
Mode: console
Level: info
Encoding: plain
Timeout: 3000
`
RPCConfigDevelopment is the development config template for RPC services
const RPCConfigProduction = `` /* 483-byte string literal not displayed */
RPCConfigProduction is the production config template for RPC services
const RPCConfigTest = `` /* 128-byte string literal not displayed */
RPCConfigTest is the test config template for RPC services
const RateLimitingMiddlewareTemplate = `` /* 1251-byte string literal not displayed */
const SystemdTemplate = `` /* 430-byte string literal not displayed */
Variables ¶
var ErrDeploymentTemplateNotFound = fmt.Errorf("deployment template not found")
var ErrTemplateNotFound = fmt.Errorf("template not found")
Functions ¶
func ExecuteTemplate ¶
ExecuteTemplate executes a template with the given parameters
func GetConfigTemplate ¶
GetConfigTemplate returns the appropriate config template
func ListTemplates ¶
ListTemplates returns available templates by type
Types ¶
type APISpec ¶
type APISpec struct {
ServiceName string
Types []TypeDef
Endpoints []EndpointDef
}
type ConfigTemplate ¶
type ConfigTemplate struct {
Type string // "api" or "rpc"
Environment string // "development", "production", "test"
Content string
}
ConfigTemplate represents a configuration template
type EndpointDef ¶
type Template ¶
type Template struct {
Name string
Type string // "middleware", "error_handler", "deployment"
Description string
Content string
Parameters []TemplateParameter
}
Template represents a code template
func GetDeploymentTemplate ¶
func GetErrorHandlerTemplate ¶
func GetMiddlewareTemplate ¶
GetMiddlewareTemplate returns a middleware template by name
func GetTemplate ¶
GetTemplate returns a template by name and type