ngxtpl

package module
v0.0.0-...-40bc1a3 Latest Latest
Warning

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

Go to latest
Published: Jan 30, 2024 License: MIT Imports: 38 Imported by: 0

README

ngxtpl

golang nginx template.

  1. build: make install
  2. create a demo config file ngxtpl --demo.
  3. for http get/post mocking go get github.com/bingoohuang/httplive/..., httplive -d testdata/httplive.db

启动时,向 nacos 注册自己

  1. 在配置文件 ngxtpl.hcl 中添加 nacos 配置,参考 demo

    nacos {
      configFile = "/some/path/nacos.yaml"
    }
    
  2. 创建 nacos 配置文件 /some/path/nacos.yaml,示例 demo

支持 redis-cluster

redis {
  addr = "localhost:6379" # 多个地址以逗号分隔
  password = ""
  cluster = true # 支持 redis-cluster 的开关
  db = 0
  # servicesKey = "services"
  servicesKey = "__gateway_redis__ upstreams"
  # 如果是hash的,servicesKey = "hashKey field"
  resultKey = "__gateway_redis__ upstreams_result"
}

image

image

redis

  1. edit the created demo.hcl config file, see demo redis confi, demo redis config for set_real_ip_from
  2. put some data into redis.
    • redis-cli -x hset __gateway_redis__ upstreams < testdata/upstreams.json
    • redis-cli -x set upstreams < testdata/upstreams.json
    • redis-cli -x set realIps < testdata/set_real_ip_from.json
  3. run ngxtpl -c demo.hcl, or ngxtpl -c demo.hcl -c demo_realIps.hcl

mysql

  1. start MySQL docker run -d --name mysql -e MYSQL_USER=user -e MYSQL_DATABASE=mydb -e MYSQL_PASSWORD=pass -e MYSQL_ROOT_PASSWORD=root -p 33306:3306 mysql:5.7
  2. prepare tables MYSQL_PWD=root mysql -h hostname -P 33306 -u user mydb < testdata/mysql.sql
  3. prepare config file, see demo mysql config

nacos

详见

reference

  1. slides HCL: A human-friendly language for developers and operators

docker-compose testing env

redis install
  1. docker pull redis:6.0.10-alpine3.13
  2. https://phoenixnap.com/kb/docker-redis https://hub.docker.com/_/redis
  3. docker run --name redis -p 6379:6379 -d redis:6.0.10-alpine3.13 docker exec -it redis redis-cli
  4. Another Redis DeskTop Manager

openresty install

  1. https://hub.docker.com/r/openresty/openresty
  2. docker pull openresty/openresty:1.19.3.1-2-alpine
  3. docker run --name openresty -v $PWD/testdata/nginx:/etc/nginx/conf.d -p 8080:8080 -p 8081:8081 -p 8082:8082 -p 8083:8083 -d openresty/openresty:1.19.3.1-2-alpine
$ docker-compose exec openresty sh
/ # ps -ef|grep nginx
    1 root      0:00 {openresty} nginx: master process /usr/local/openresty/bin/openresty -g daemon off;
    7 nobody    0:00 {openresty} nginx: worker process
   15 root      0:00 grep nginx
/ # /usr/local/openresty/bin/openresty -t
nginx: the configuration file /usr/local/openresty/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/openresty/nginx/conf/nginx.conf test is successful
/ # /usr/local/openresty/bin/openresty -s reload
2021/02/07 09:47:47 [notice] 17#17: signal process started
/ # echo $?
0
/ # 

ngxtpl install

  1. docker build -f Dockerfile -t openresty/ngxtpl .

docker compose

  1. docker-compose up
  2. set tpl (demo) and services(demo) in redis by docker.hcl config.
  3. change services data in redis and run curl to check results:
🕙[ 00:13:06 ] ❯ for n in {1..8}; do curl 127.0.0.1:8090/service1/abc;print; done
8081 welcome you!
{"status":"success","result":"8082 nginx json"}
8083 welcome you!
8084 welcome you!
8081 welcome you!
{"status":"success","result":"8082 nginx json"}
8083 welcome you!
8084 welcome you!

daemon service on linux

/app/ngxtpl install -c /etc/app/docker.hcl; /app/ngxtpl start

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrCfg = errors.New("Errors in the config file")

ErrCfg defines the error type of bad config.

View Source
var InitAssets embed.FS

InitAssets gives the initial assets for the program initialization.

Functions

func ConvertToMapInterface

func ConvertToMapInterface(v map[string]string) map[string]interface{}

ConvertToMapInterface converts map[string]string to map[string]interface{}.

func ConvertToMapInterfaceSlice

func ConvertToMapInterfaceSlice(m []map[string]string) []map[string]interface{}

ConvertToMapInterfaceSlice convert slice of map[string]interface{} to slice of map[string]interface{}.

func DefaultTo

func DefaultTo(a, b string) string

DefaultTo test a, return b if a is empty.

func FormatFloat

func FormatFloat(num float64, precision int) string

FormatFloat format float with specified precision.

func GetRegisterParam

func GetRegisterParam(config Config, c config_client.IConfigClient) (registerParam string, err error)

func HTTPGet

func HTTPGet(addr string) ([]byte, error)

HTTPGet execute HTTP GET request.

func HTTPGetStr

func HTTPGetStr(addr string) (string, error)

HTTPGetStr execute HTTP GET request.

func HTTPInvoke

func HTTPInvoke(method, addr string, body []byte) ([]byte, error)

HTTPInvoke execute HTTP method request.

func HTTPPost

func HTTPPost(addr string, body []byte) ([]byte, error)

HTTPPost execute HTTP POST request.

func HasBrace

func HasBrace(s string, head, tail string) bool

HasBrace tests whether the string s begins with head and ends with tail.

func HasPrefix

func HasPrefix(s string, prefix ...string) bool

HasPrefix tests whether the string s begins with any of prefix.

func If

func If(condition bool, a, b string) string

If tests condition to return a or b.

func IsHTTPAddress

func IsHTTPAddress(s string) bool

IsHTTPAddress tests whether the string s starts with http:// or https://.

func IsJSONBytes

func IsJSONBytes(b []byte) bool

IsJSONBytes tests bytes b is in JSON format.

func JSONDecode

func JSONDecode(v string) (interface{}, error)

JSONDecode decodes JSON string v to map[string]interface.

func MapInt

func MapInt(m map[string]interface{}, key string, defaultValue int) int

MapInt returns the int value associated with given key in the map.

func MapStr

func MapStr(m map[string]interface{}, key, defaultValue string) string

MapStr returns the string value associated with given key in the map.

func ParseConfig

func ParseConfig(configYamlFileName string, v any) error

func PflagParse

func PflagParse(f *pflag.FlagSet, args []string)

PflagParse parses the plag and check unknown args.

func Pick1

func Pick1[T any](a T, _ ...any) T

func ReadFile

func ReadFile(filename string) []byte

ReadFile reads the file content of file with name filename. or panic if error happens.

func ReadFileE

func ReadFileE(filename string) ([]byte, error)

ReadFileE reads the file content of file with name filename.

func ReadFileStrE

func ReadFileStrE(filename string) (string, error)

ReadFileStrE reads the file content of file with name filename.

func SetupSingals

func SetupSingals(sig ...os.Signal) context.Context

SetupSingals setups the signal.

func SetupSingalsWithContext

func SetupSingalsWithContext(parent context.Context, sig ...os.Signal) context.Context

SetupSingalsWithContext setups the signal.

func Sh

func Sh(bash string) (*cmd.Cmd, cmd.Status)

Sh executes a bash scripts.

func SliceContains

func SliceContains(ss []string, sub string) bool

SliceContains test if any element in slice contains sub.

func Split2

func Split2(s, sep string) (string, string)

Split2 splits the s with sep and return the trimmed string pair.

func TemplateEval

func TemplateEval(s string, data interface{}) (string, error)

TemplateEval execute a template s with data.

func ZeroTo

func ZeroTo(a, b int) int

ZeroTo return a == 0 ? b : a.

Types

type Cfg

type Cfg struct {
	Redis *Redis `hcl:"redis"`
	Mysql *Mysql `hcl:"mysql"`
	Nacos *Nacos `hcl:"nacos"`

	Tpl Tpl `hcl:"tpl"`
	// contains filtered or unexported fields
}

Cfg represents the root structure of the config.

func (*Cfg) Parse

func (c *Cfg) Parse() error

Parse parses the config.

func (Cfg) Run

func (c Cfg) Run()

Run runs the config.

type Cfgs

type Cfgs []Cfg

Cfgs is alias for slice of Cfg.

func DecodeCfgFiles

func DecodeCfgFiles(cfgFiles []string) (cfgs Cfgs)

DecodeCfgFiles decodes the config files to Cfg structs.

func (Cfgs) Run

func (c Cfgs) Run()

Run runs the configs.

type CommandResult

type CommandResult struct {
	ExecError error  `json:"execError"`
	Stdout    string `json:"stdout"`
	Stderr    string `json:"stderr"`
	ExitCode  int    `json:"exitCode"`
}

CommandResult is the result of command execution.

type Config

type Config struct {
	ServerConfigs         []constant.ServerConfig
	ClientConfig          constant.ClientConfig
	RegisterParam         *vo.ConfigParam
	RegisterInstanceParam *vo.RegisterInstanceParam
}

type DataSource

type DataSource interface {
	Read() (interface{}, error)
}

DataSource defines the data source interface for reading the data.

type HTTPSource

type HTTPSource struct {
	Address string
}

HTTPSource defines the data source interface for reading the data from http.

func (HTTPSource) Read

func (h HTTPSource) Read() (interface{}, error)

type KeyReader

type KeyReader interface {
	Get(key string) (string, error)
}

KeyReader read by key.

type KeyWriter

type KeyWriter interface {
	Write(key, value string) error
}

KeyWriter writes key and value.

type Mysql

type Mysql struct {
	DataSourceName string            `hcl:"dataSourceName"`
	DataKey        string            `hcl:"dataKey"`
	DataSQL        string            `hcl:"dataSql"`
	Sqls           map[string]string `hcl:"sqls"`
	KVSql          string            `hcl:"kvSql"`
}

Mysql represents the structure of mysql config.

func (Mysql) Get

func (t Mysql) Get(key string) (string, error)

Get gets the value of key from mysql.

func (*Mysql) Parse

func (t *Mysql) Parse() (DataSource, error)

Parse parses the mysql config.

func (Mysql) Read

func (t Mysql) Read() (interface{}, error)

type Nacos

type Nacos struct {
	ConfigFile   string       `hcl:"configFile"`
	ServiceParam ServiceParam `hcl:"serviceParam"`
	// contains filtered or unexported fields
}

Nacos represents the structure of Nacos config.

func (Nacos) Get

func (n Nacos) Get(key string) (string, error)

Get gets the value of key from mysql.

func (*Nacos) Parse

func (n *Nacos) Parse() (DataSource, error)

Parse parses the nacos config.

func (Nacos) Read

func (n Nacos) Read() (interface{}, error)

type Redis

type Redis struct {
	Addr        string `hcl:"addr"`
	Password    string `hcl:"password"`
	ServicesKey string `hcl:"servicesKey"`
	ResultKey   string `hcl:"resultKey"`
	Db          int    `hcl:"db"`
	Cluster     bool   `hcl:"cluster"`
}

Redis represents the structure of redis config.

func (Redis) Get

func (r Redis) Get(key string) (string, error)

Get gets the value of key from redis.

func (*Redis) Parse

func (r *Redis) Parse() (DataSource, error)

Parse parses the redis config.

func (Redis) Read

func (r Redis) Read() (interface{}, error)

Read reads the value.

func (Redis) Write

func (r Redis) Write(key, value string) (err error)

Write writes key and it's value to redis.

func (Redis) WriteResult

func (r Redis) WriteResult(result Result) error

WriteResult writes error.

type Result

type Result struct {
	Time       string `json:"time"`
	Old        string `json:"old,omitempty"`
	New        string `json:"new,omitempty"`
	Error      string `json:"error,omitempty"`
	StatusCode int    `json:"statusCode"`
}

Result ...

type ResultWriter

type ResultWriter interface {
	WriteResult(Result) error
}

ResultWriter writes error.

type ServiceParam

type ServiceParam struct {
	ServiceName string   `hcl:"ServiceName"` // required
	GroupName   string   `hcl:"GroupName"`   // optional,default:DEFAULT_GROUP
	Clusters    []string `hcl:"Clusters"`    // optional,default:DEFAULT
}

ServiceParam map config for service discovery.

type Tpl

type Tpl struct {
	DataSource  string `hcl:"dataSource"`
	Interval    string `hcl:"interval"`
	TplSource   string `hcl:"tplSource"`
	Destination string `hcl:"destination"`
	// 执行命令(包括测试命令)失败时,写入导致失败的内容
	FailedDestination string `hcl:"failDestination"`
	// 测试命令
	TestCommand string `hcl:"testCommand"`
	// 测试命令执行结果检查,例如有OK字眼,不配置,则只检测测试命令的执行状态
	TestCommandCheck string `hcl:"testCommandCheck"`

	Command string `hcl:"command"`
	// 结果检查,例如有OK字眼,不配置,则只检测测试命令的执行状态ß
	CommandCheck string `hcl:"commandCheck"`

	Perms int `hcl:"perms"`
	// contains filtered or unexported fields
}

Tpl represents a tpl config section.

func (*Tpl) Execute

func (t *Tpl) Execute(data interface{}, ds DataSource, cfgName string, result *Result) error

Execute executes the template.

func (*Tpl) Parse

func (t *Tpl) Parse() error

Parse parses and validates the template.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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