listenandserve in a windows service

800 views
Skip to first unread message

jksmi...@gmail.com

unread,
Apr 29, 2014, 1:33:04 PM4/29/14
to golan...@googlegroups.com
Using service library from bitbucket.org/kardianos/service, unable to pass a port string to worker func:

func doWork(port string) {
    slog.Info("Service is running")
    m := martini.Classic()
    m.Get("/", func() string {
        return "Hello world!"
    })

       go http.ListenAndServe(port, m)
    select {}
}

This doWork func is run as a goroutine. Getting rid of the param and doing something like the following (reading a global var for port) also yields no joy:

func doWork() {
    slog.Info("ST Services is running")
    m := martini.Classic()
    m.Get("/", func() string {
        return "Hello world!"
    })
    Utils.GloConfig.RLock()
    portnum := Utils.GloConfig.Port
    Utils.GloConfig.RUnlock()
      go http.ListenAndServe(":"+strconv.Itoa(portnum), m)
      select {}
}

Does anyone have an example of a golang webserver running as a windows service?

Thanks,
James

Frederic Boyer

unread,
Apr 29, 2014, 3:29:36 PM4/29/14
to golan...@googlegroups.com, jksmi...@gmail.com
Hi,

Your approach seems to be fine, copy-paste the following sample code (it is a modified example from the "service" repo): http://play.golang.org/p/xbIw1_6rGQ

I could install/run/remove it from services.msc (and get the "Hello world!") without any issue.

Let me know if I missed something,

Fred

jksmi...@gmail.com

unread,
Apr 29, 2014, 4:46:33 PM4/29/14
to golan...@googlegroups.com, jksmi...@gmail.com
Hi Fred,

Thanks for help. Must be something in my initialization that is killing the service. To get port number from config, if I do something like this (or in the init code somewhere):

    err = s.Run(func() error {
        // start
        Utils.GloConfig.RLock()
        portnum := Utils.GloConfig.Port
        Utils.GloConfig.RUnlock()
        port := ":" + strconv.Itoa(portnum)
        go doWork(port)
        return nil
    }, func() error {
        // stop
        stopWork()
        return nil
    })

then something in the doWork func goes awry.

Frederic Boyer

unread,
Apr 29, 2014, 5:02:04 PM4/29/14
to golan...@googlegroups.com, jksmi...@gmail.com
Is that config fed using a config file? If yes, is your service running under a user that has enough permissions to access this file? 

Anyhow, I would highly suggest that you log the result of the "portnum" and "port" assignment statements to some file or stdout (this would probably require you to "service.exe run") to check that it's actually valid?

Sorry, I'm just throwing ideas out there as it's not easy to help you with the minimal amount of code provided.

Let me know,

Fred

alexru...@gmail.com

unread,
Apr 29, 2014, 9:59:48 PM4/29/14
to golan...@googlegroups.com, jksmi...@gmail.com
You can use NSSM with your Go executable.
http://nssm.cc/

вторник, 29 апреля 2014 г., 20:33:04 UTC+3 пользователь jksmi...@gmail.com написал:

Jesse McNelis

unread,
Apr 29, 2014, 10:37:38 PM4/29/14
to jksmi...@gmail.com, golang-nuts
On Wed, Apr 30, 2014 at 3:33 AM, <jksmi...@gmail.com> wrote:
> Using service library from bitbucket.org/kardianos/service, unable to pass a
> port string to worker func:
>
> func doWork(port string) {
>
> slog.Info("Service is running")
>
> m := martini.Classic()
>
> m.Get("/", func() string {
>
> return "Hello world!"
>
> })
>
> go http.ListenAndServe(port, m)
>
> select {}
>
> }

http.ListenAndServe() returns an error value that you're ignoring,
you'll be very sad when this process just silently exits because the
port string is wrong or the process can't bind to the port specified.
Don't ignore the error.

Benjamin BALET

unread,
Apr 30, 2014, 3:21:46 AM4/30/14
to golan...@googlegroups.com, jksmi...@gmail.com
These two projects work on Windows : 

You'll see how to use bitbucket.org/kardianos/service package in conjunction with bitbucket.org/kardianos/osext to get the executable's path. This will allow you to load the configuration file (in which the port number is defined).

Konstantin Khomoutov

unread,
Apr 30, 2014, 6:54:01 AM4/30/14
to jksmi...@gmail.com, golan...@googlegroups.com
On Tue, 29 Apr 2014 10:33:04 -0700 (PDT)
jksmi...@gmail.com wrote:

> Using service library from bitbucket.org/kardianos/service, unable to
> pass a port string to worker func:
[...]

In addition to other said, I'd try winsvc [1] which is Windows-specific.

1. https://code.google.com/p/winsvc/

Benjamin BALET

unread,
Apr 30, 2014, 7:06:50 AM4/30/14
to Konstantin Khomoutov, jksmi...@gmail.com, golan...@googlegroups.com



--
You received this message because you are subscribed to a topic in the Google Groups "golang-nuts" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/golang-nuts/66RXNMQ1auU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages