zdpgo_cmd

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: May 1, 2022 License: MIT Imports: 6 Imported by: 2

README

zdpgo_cmd

使用Golang开发命令行工具的框架,基于corba二次开发

版本历史

  • v0.1.0 2022/4/24 新增:后台服务
  • v0.1.1 2022/5/1 新增:删除后台服务

使用示例

后台服务

示例代码

package main

import (
	"embed"
	"fmt"
	"github.com/zhangdapeng520/zdpgo_cmd/libs/daemon"
	"html/template"
	"net/http"
	"os"
)

//go:embed templates
var fs embed.FS

func helloHandler(w http.ResponseWriter, r *http.Request) {
	// 使用嵌入的文件系统,能够直接打包到二进制文件
	t, err := template.ParseFS(fs, "templates/index.html")

	if err != nil {
		panic(err)
	}

	// 解析模板
	t.Execute(w, nil)
}

func main() {
	logFile := "daemon.log"

	fmt.Println("启动服务:http://localhost:8888")

	// 启动一个子进程后主程序退出
	daemon.Background(logFile, true)

	// 以下代码只有子程序会执行
	fmt.Println(os.Getpid(), "启动服务...")

	// 启动服务
	server := &http.Server{Addr: ":8888"}
	http.HandleFunc("/", helloHandler)
	server.ListenAndServe()

	// 服务退出
	fmt.Println(os.Getpid(), "服务退出")
}

运行方式,以windows为例:

go build
./background_server.exe

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cmd

type Cmd struct {
	Config *Config
	Shell  *zdpgo_shell.Shell
	Env    *zdpgo_env.Env
	Log    *zdpgo_log.Log
}

func New

func New() *Cmd

func NewWithConfig

func NewWithConfig(config Config) *Cmd

func (*Cmd) ExitBackground

func (c *Cmd) ExitBackground() error

func (*Cmd) RunWithBackGround

func (c *Cmd) RunWithBackGround(runFunc func()) error

type Config

type Config struct {
	Debug       bool   `json:"debug" yaml:"debug" env:"debug"`
	LogFileName string `json:"log_file_name" yaml:"log_file_name" env:"log_file_name"`
	PidName     string `json:"pid_name" yaml:"pid_name" env:"pid_name"`
	EnvFileName string `json:"env_file_name" yaml:"env_file_name" env:"env_file_name"`
}

Directories

Path Synopsis
examples
zdptool command
libs
pflag
Package pflag is a drop-in replacement for Go's flag package, implementing POSIX/GNU-style --flags.
Package pflag is a drop-in replacement for Go's flag package, implementing POSIX/GNU-style --flags.

Jump to

Keyboard shortcuts

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