How to stop http.ListenAndServe??

2,793 views
Skip to first unread message

nvcnvn

unread,
Apr 17, 2011, 6:15:26 AM4/17/11
to golang-nuts
I'm trying to make a web server with just http pkg, some time I need
stop the terminal to re complie but I must close the window I re type
any thing

In web.go pkg there is a function help stop the web server when I
need. I just can end the process by access localhost/end

How can I do it with just http pkg???

P/s: Golang function list is not.....humm. Like C++, PHP... there
funtion list full with example, comment...that will help someone in
somecase!

I'm just a newbie from programming, and when I read:
###############################################################
func (*ServerConn) Close

func (sc *ServerConn) Close() (c net.Conn, r *bufio.Reader)

Close detaches the ServerConn and returns the underlying connection as
well as the read-side bufio which may have some left over data. Close
may be called before Read has signaled the end of the keep-alive
logic. The user should not call Close while Read or Write is in
progress.
################################################################

I even do not know if this is what I need or how to try it!

Please help me!

Dave Cheney

unread,
Apr 17, 2011, 6:29:36 AM4/17/11
to nvcnvn, golang-nuts
Hi,

You could try something like this to establish a handler function that
will cause your application to exit.

lucky(~/devel) % cat exit.go
package main

import "http"
import "log"

func ExitFunc(response http.ResponseWriter, request *http.Request) {
log.Fatal("/exit handler called")
}

func main() {
http.HandleFunc("/exit", ExitFunc)

server := &http.Server{
Addr: ":12345",
Handler: http.DefaultServeMux,
}
err := server.ListenAndServe()
if err != nil {
log.Fatalf("Error: %s", err.String())
}
}

lucky(~/devel) % 6g exit.go ; 6l exit.6 ; ./6.out &
[1] 46123
lucky(~/devel) % curl -I http://localhost:12345/exit
2011/04/17 20:27:46 /exit handler called
curl: (52) Empty reply from server
[1]+ Exit 1 ./6.out

However, would it be simpler just to issue ctrl-c ?

Cheers

Dave

nvcnvn

unread,
Apr 17, 2011, 6:34:55 AM4/17/11
to golang-nuts
Oh, tanks you so much!

I forgot mention that I'm a newbie in Linux ubuntu too! :)

On Apr 17, 5:29 pm, Dave Cheney <d...@cheney.net> wrote:
> Hi,
>
> You could try something like this to establish a handler function that
> will cause your application to exit.
>
> lucky(~/devel) % cat exit.go
> package main
>
> import "http"
> import "log"
>
> func ExitFunc(response http.ResponseWriter, request *http.Request) {
>   log.Fatal("/exit handler called")
>
> }
>
> func main() {
>         http.HandleFunc("/exit", ExitFunc)
>
>         server := &http.Server{
>                 Addr:         ":12345",
>                 Handler:      http.DefaultServeMux,
>         }
>         err := server.ListenAndServe()
>         if err != nil {
>                 log.Fatalf("Error: %s", err.String())
>         }
>
> }
>
> lucky(~/devel) % 6g exit.go ; 6l exit.6 ; ./6.out &
> [1] 46123
> lucky(~/devel) % curl -Ihttp://localhost:12345/exit
> 2011/04/17 20:27:46 /exit handler called
> curl: (52) Empty reply from server
> [1]+  Exit 1                  ./6.out
>
> However, would it be simpler just to issue ctrl-c ?
>
> Cheers
>
> Dave
>

土星五号

unread,
Apr 17, 2011, 7:21:39 AM4/17/11
to nvcnvn, golang-nuts
control + c

2011/4/17 nvcnvn <nvc...@gmail.com>
Reply all
Reply to author
Forward
0 new messages