GoFrame Framework

repository·master·Indexed 12 days ago

https://github.com/gogf/gf

A Go development framework providing a comprehensive set of tools and libraries. It includes the gf CLI tool for project initialization, code generation, and building, as well as various configuration adapters for centralized sources such as Apollo, Consul, Nacos, and Kubernetes ConfigMaps (kubecm) via the gcfg.Adapter interface.

Tokens
127.5K
Snippets
450
Records
553
Agent score
96%

What's inside GoFrame

  1. Understand RSA key formats (PKCS#1 vs PKCS#8/PKIX)

    master

    The package supports two primary formats. Note that encryption uses public keys, and for modern formats, public keys use the PKIX (X.509) standard, not PKCS#8.

    FormatPrivate Key PEM HeaderPublic Key PEM Header
    PKCS#1-----BEGIN RSA PRIVATE KEY----------BEGIN RSA PUBLIC KEY-----
    PKCS#8/PKIX-----BEGIN PRIVATE KEY----------BEGIN PUBLIC KEY-----

    Important Note on PKCS#8/PKIX: When using modern keys, a "PKCS#8 key pair" actually consists of a PKCS#8 formatted private key and a PKIX/SubjectPublicKeyInfo formatted public key. The function EncryptPKIX is the correct way to encrypt using these modern public keys.

  2. Install the gf CLI tool

    master

    You can install the gf CLI tool using pre-compiled binaries or via go install.

    Using Pre-Compiled Binaries (Mac & Linux)

    Run the following command to download, make executable, install, and clean up the temporary binary:

    wget -O gf https://github.com/gogf/gf/releases/latest/download/gf_$(go env GOOS)_$(go env GOARCH) && chmod +x gf && ./gf install -y && rm ./gf

    Note for Zsh users: If you encounter conflicts between gf and git fetch, you may need to resolve the alias by running alias gf=gf.

    Using go install

    To install the latest version or a specific version (must be >= v2.5.5):

    # Latest version
    go install github.com/gogf/gf/cmd/gf/v2@latest
    
    # Specific version
    go install github.com/gogf/gf/cmd/gf/v2@v2.5.5

    Windows Installation

    Manually download the binary from the releases page, execute it in the command line, and follow the provided instructions.

    wget -O gf https://github.com/gogf/gf/releases/latest/download/gf_$(go env GOOS)_$(go env GOARCH) && chmod +x gf && ./gf install -y && rm ./gf
  3. Install the GoFrame Etcd Registry

    master

    To use etcd for service registration and discovery management in GoFrame, install the registry package using go get or by adding it to your go.mod file.

    go get -u -v github.com/gogf/gf/contrib/registry/etcd/v2

    Or in your go.mod:

    require github.com/gogf/gf/contrib/registry/etcd/v2 latest
  4. Install GoFrame Polaris Registry

    master

    To use PolarisMesh for service registration, discovery management, and heartbeat reporting in GoFrame, install the Polaris registry contribution package.

    Requirements:

    • Golang version >= 1.20

    Installation via CLI:

    go get -u -v github.com/gogf/gf/contrib/registry/polaris/v2

    Installation via go.mod:

    require github.com/gogf/gf/contrib/registry/polaris/v2 latest
    go get -u -v github.com/gogf/gf/contrib/registry/polaris/v2
  5. Import the kubecm boot package in main

    master

    To ensure the kubecm adapter is registered before the application starts, import your custom boot package using a blank identifier (_) at the very top of your main.go imports.

    package main
    
    import (
    	_ "github.com/gogf/gf/example/config/kubecm/boot_in_pod"
    
    	"github.com/gogf/gf/v2/frame/g"
    	"github.com/gogf/gf/v2/os/gctx"
    )
    
    func main() {
    	var ctx = gctx.GetInitCtx()
    
    	// Available checks.
    	g.Dump(g.Cfg().Available(ctx))
    
    	// All key-value configurations.
    	g.Dump(g.Cfg().Data(ctx))
    
    	// Retrieve certain value by key.
    	g.Dump(g.Cfg().MustGet(ctx, "server.address"))
    }
  6. Configure Nacos as the global configuration adapter

    master

    To use Nacos configuration globally, create a custom boot package. This package should use an init() function to initialize the Nacos client and set it as the default adapter for the GoFrame configuration instance (g.Cfg()) before any other packages boot.

    You need to provide:

    • constant.ServerConfig: The Nacos server IP and Port.
    • constant.ClientConfig: Local cache and log directories.
    • vo.ConfigParam: The DataId and Group for your configuration.
    package boot
    
    import (
    	"github.com/gogf/gf/contrib/config/nacos/v2"
    	"github.com/gogf/gf/v2/frame/g"
    	"github.com/gogf/gf/v2/os/gctx"
    	"github.com/nacos-group/nacos-sdk-go/v2/common/constant"
    	"github.com/nacos-group/nacos-sdk-go/v2/vo"
    )
    
    func init() {
    	var (
    		cx          = gctx.GetInitCtx()
    		serverConfig = constant.ServerConfig{
    			IpAddr: "localhost",
    			Port:   8848,
    		}
    		clientConfig = constant.ClientConfig{
    			CacheDir: "/tmp/nacos",
    			LogDir:   "/tmp/nacos",
    		}
    		configParam = vo.ConfigParam{
    			DataId: "config.toml",
    			Group:  "test",
    		}
    	)
    	// Create anacosClient that implements gcfg.Adapter.
    	adapter, err := nacos.New(ctx, nacos.Config{
    		ServerConfigs: []constant.ServerConfig{serverConfig},
    		ClientConfig:  clientConfig,
    		ConfigParam:   configParam,
    	})
    	if err != nil {
    		g.Log().Fatalf(ctx, `%+v`, err)
    	}
    	// Change the adapter of default configuration instance.
    	g.Cfg().SetAdapter(adapter)
    }
  7. Install the Zookeeper registry for GoFrame

    master

    To use Zookeeper for service registration and discovery management in GoFrame, install the zookeeper/v2 contribution package using go get or by adding it to your go.mod file.

    go get -u -v github.com/gogf/gf/contrib/registry/zookeeper/v2

    Or in your go.mod:

    require github.com/gogf/gf/contrib/registry/zookeeper/v2 latest
  8. Install otelmetric for GoFrame

    master

    To use OpenTelemetry metrics with GoFrame, install the otelmetric contribution package using go get or by adding it to your go.mod file.

    go get -u -v github.com/gogf/gf/contrib/metric/otelmetric/v2

    Or in your go.mod:

    require github.com/gogf/gf/contrib/metric/otelmetric/v2 latest
  9. Install the GoFrame File Registry

    master

    To use the file registry for service registration and discovery management, install the package using go get or add it to your go.mod file.

    Using go get:

    go get -u -v github.com/gogf/gf/contrib/registry/file/v2

    Using go.mod:

    require github.com/gogf/gf/contrib/registry/file/v2 latest
    go get -u -v github.com/gogf/gf/contrib/registry/file/v2
  10. Install the GoFrame Consul Registry

    master

    To use Consul for service registration and discovery in your GoFrame project, install the consul/v2 contribution package using go get or by adding it to your go.mod file.

    go get -u github.com/gogf/gf/contrib/registry/consul/v2

    Or via go.mod:

    require github.com/gogf/gf/contrib/registry/consul/v2 latest