templates

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 1, 2025 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

View Source
const APIConfigDevelopment = `` /* 138-byte string literal not displayed */

APIConfigDevelopment is the development config template for API services

View Source
const APIConfigProduction = `` /* 387-byte string literal not displayed */

APIConfigProduction is the production config template for API services

View Source
const APIConfigTest = `` /* 144-byte string literal not displayed */

APIConfigTest is the test config template for API services

View Source
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}}}
`
View Source
const AuthMiddlewareTemplate = `` /* 1146-byte string literal not displayed */
View Source
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",
		}
	}
}
`
View Source
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,
		}
	}
}
`
View Source
const DockerfileTemplate = `` /* 394-byte string literal not displayed */
View Source
const KubernetesTemplate = `` /* 1301-byte string literal not displayed */
View Source
const LoggingMiddlewareTemplate = `` /* 1080-byte string literal not displayed */
View Source
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

View Source
const RPCConfigProduction = `` /* 483-byte string literal not displayed */

RPCConfigProduction is the production config template for RPC services

View Source
const RPCConfigTest = `` /* 128-byte string literal not displayed */

RPCConfigTest is the test config template for RPC services

View Source
const RateLimitingMiddlewareTemplate = `` /* 1251-byte string literal not displayed */
View Source
const SystemdTemplate = `` /* 430-byte string literal not displayed */

Variables

View Source
var ErrDeploymentTemplateNotFound = fmt.Errorf("deployment template not found")
View Source
var ErrTemplateNotFound = fmt.Errorf("template not found")

Functions

func ExecuteTemplate

func ExecuteTemplate(tmpl *Template, params map[string]interface{}) (string, error)

ExecuteTemplate executes a template with the given parameters

func GetConfigTemplate

func GetConfigTemplate(serviceType, environment string) string

GetConfigTemplate returns the appropriate config template

func ListTemplates

func ListTemplates(templateType string) []string

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 EndpointDef struct {
	Handler  string
	Method   string
	Path     string
	Request  string
	Response string
}

type FieldDef

type FieldDef struct {
	Name    string
	Type    string
	JsonTag string
	Comment string
}

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 GetDeploymentTemplate(name string) (*Template, error)

func GetErrorHandlerTemplate

func GetErrorHandlerTemplate(name string) (*Template, error)

func GetMiddlewareTemplate

func GetMiddlewareTemplate(name string) (*Template, error)

GetMiddlewareTemplate returns a middleware template by name

func GetTemplate

func GetTemplate(templateType, templateName string) (*Template, error)

GetTemplate returns a template by name and type

type TemplateParameter

type TemplateParameter struct {
	Name        string
	Type        string // "string", "int", "bool"
	Description string
	Required    bool
	Default     interface{}
}

TemplateParameter defines a parameter for template execution

type TypeDef

type TypeDef struct {
	Name   string
	Fields []FieldDef
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL