zoukankan      html  css  js  c++  java
  • Golang Web Server on Windows Service

    https://github.com/Technololist/golang-http-windows-service/blob/master/websvc.go

    golang.orgxsys@v0.0.0-20210124154548-22da62e12c0cwindowssvcexamplemain.go


    golang.orgxsys@v0.0.0-20210124154548-22da62e12c0cwindowssvcexamplemain.go

    func usage(errmsg string) {
    fmt.Fprintf(os.Stderr,
    "%s "+
    "usage: %s <command> "+
    " where <command> is one of "+
    " install, remove, debug, start, stop, pause or continue. ",
    errmsg, os.Args[0])
    os.Exit(2)
    }

    func main() {
    const svcName = "myservice"

    inService, err := svc.IsWindowsService()
    if err != nil {
    log.Fatalf("failed to determine if we are running in service: %v", err)
    }
    if inService {
    runService(svcName, false)
    return
    }

    if len(os.Args) < 2 {
    usage("no command specified")
    }

    cmd := strings.ToLower(os.Args[1])
    switch cmd {
    case "debug":
    runService(svcName, true)
    return
    case "install":
    err = installService(svcName, "my service")
    case "remove":
    err = removeService(svcName)
    case "start":
    err = startService(svcName)
    case "stop":
    err = controlService(svcName, svc.Stop, svc.Stopped)
    case "pause":
    err = controlService(svcName, svc.Pause, svc.Paused)
    case "continue":
    err = controlService(svcName, svc.Continue, svc.Running)
    default:
    usage(fmt.Sprintf("invalid command %s", cmd))
    }
    if err != nil {
    log.Fatalf("failed to %s %s: %v", cmd, svcName, err)
    }
    return
    }


    golang.orgxsys@v0.0.0-20210124154548-22da62e12c0cwindowssvcexampleinstall.go

    func exePath() (string, error) {
    prog := os.Args[0]
    p, err := filepath.Abs(prog)
    if err != nil {
    return "", err
    }
    fi, err := os.Stat(p)
    if err == nil {
    if !fi.Mode().IsDir() {
    return p, nil
    }
    err = fmt.Errorf("%s is directory", p)
    }
    if filepath.Ext(p) == "" {
    p += ".exe"
    fi, err := os.Stat(p)
    if err == nil {
    if !fi.Mode().IsDir() {
    return p, nil
    }
    err = fmt.Errorf("%s is directory", p)
    }
    }
    return "", err
    }

    func installService(name, desc string) error {
    exepath, err := exePath()
    if err != nil {
    return err
    }
    m, err := mgr.Connect()
    if err != nil {
    return err
    }
    defer m.Disconnect()
    s, err := m.OpenService(name)
    if err == nil {
    s.Close()
    return fmt.Errorf("service %s already exists", name)
    }
    s, err = m.CreateService(name, exepath, mgr.Config{DisplayName: desc}, "is", "auto-started")
    if err != nil {
    return err
    }
    defer s.Close()
    err = eventlog.InstallAsEventCreate(name, eventlog.Error|eventlog.Warning|eventlog.Info)
    if err != nil {
    s.Delete()
    return fmt.Errorf("SetupEventLogSource() failed: %s", err)
    }
    return nil
    }

    func removeService(name string) error {
    m, err := mgr.Connect()
    if err != nil {
    return err
    }
    defer m.Disconnect()
    s, err := m.OpenService(name)
    if err != nil {
    return fmt.Errorf("service %s is not installed", name)
    }
    defer s.Close()
    err = s.Delete()
    if err != nil {
    return err
    }
    err = eventlog.Remove(name)
    if err != nil {
    return fmt.Errorf("RemoveEventLogSource() failed: %s", err)
    }
    return nil
    }


    C:/Users/xiaole.feng/go/pkg/mod/golang.org/x/sys@v0.0.0-20210124154548-22da62e12c0c/windows/svc/example/manage.go:17


    func startService(name string) error {
    m, err := mgr.Connect()
    if err != nil {
    return err
    }
    defer m.Disconnect()
    s, err := m.OpenService(name)
    if err != nil {
    return fmt.Errorf("could not access service: %v", err)
    }
    defer s.Close()
    err = s.Start("is", "manual-started")
    if err != nil {
    return fmt.Errorf("could not start service: %v", err)
    }
    return nil
    }

    func controlService(name string, c svc.Cmd, to svc.State) error {
    m, err := mgr.Connect()
    if err != nil {
    return err
    }
    defer m.Disconnect()
    s, err := m.OpenService(name)
    if err != nil {
    return fmt.Errorf("could not access service: %v", err)
    }
    defer s.Close()
    status, err := s.Control(c)
    if err != nil {
    return fmt.Errorf("could not send control=%d: %v", c, err)
    }
    timeout := time.Now().Add(10 * time.Second)
    for status.State != to {
    if timeout.Before(time.Now()) {
    return fmt.Errorf("timeout waiting for service to go to state=%d", to)
    }
    time.Sleep(300 * time.Millisecond)
    status, err = s.Query()
    if err != nil {
    return fmt.Errorf("could not retrieve service status: %v", err)
    }
    }
    return nil
    }

  • 相关阅读:
    Spring整合SpringDataJpa配置文件头
    SpringDataJpa全部依赖
    Spring data jpa persistence .xml 配置文件
    最新为Phpstorm配置xdebug 进行断点调试
    如何根据不同的浏览器写不同的css样式达到兼容
    关于ecshop的那些故事
    安装xampp 后 发现 apache 启动不起来
    关于在biweb 中安装完成后 首页上方报错问题的解决
    phpstormn 中 xdebug 的详细配置2
    关于phpstorm中安装配置xdeug
  • 原文地址:https://www.cnblogs.com/rsapaper/p/15525709.html
Copyright © 2011-2022 走看看