Skip to content

Instantly share code, notes, and snippets.

@jpic
Created April 28, 2010 14:40
Show Gist options
  • Save jpic/382227 to your computer and use it in GitHub Desktop.
Save jpic/382227 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"http"
"regexp"
/*"io"*/
"log"
"fcgi"
)
type Root struct {
Url string
Root string
}
type Site struct {
HostRegexp string
Roots []Root
FcgiDialers []fcgi.Dialer
FcgiHandler http.Handler
}
type Configuration struct {
Address string
Sites []Site
}
var configuration = Configuration{
"0.0.0.0:80",
[]Site{
Site{
"(chocspace\\.com)|(localhost)",
[]Root{
Root{
"/site_media",
"/srv/music/site_media",
},
},
[]fcgi.Dialer{
fcgi.NewDialer("tcp", ":9001"),
},
nil,
},
},
}
func (cfg *Configuration) ServeHTTP(c *http.Conn, req *http.Request) {
for _, site := range cfg.Sites {
match, _ := regexp.MatchString( site.HostRegexp, req.Host )
if match {
fmt.Println("Serving")
site.FcgiHandler.ServeHTTP(c, req)
fmt.Println("Served")
return
}
}
}
func main() {
println("serving on", configuration.Address)
// prepare handlers
for _, site := range configuration.Sites {
handler, err := fcgi.Handler(site.FcgiDialers)
site.FcgiHandler = handler
if err != nil {
log.Exit("fcgi.Handler:", err)
}
}
http.Handle("/", &configuration)
err := http.ListenAndServe(configuration.Address, nil)
if err != nil {
log.Exit("ListenAndServe:", err)
}
return
}
/*
Error:
serving on 0.0.0.0:80
Serving
panic: runtime error: invalid memory address or nil pointer dereference
panic PC=0xb74c6dcc
runtime.panic+0x97 /home/jpic/src/go/src/pkg/runtime/proc.c:1011
runtime.panic(0x0, 0x80d1b60)
panicstring+0x5e /home/jpic/src/go/src/pkg/runtime/runtime.c:83
panicstring(0x80d1b60, 0xb7799a98)
sigpanic+0x8f /home/jpic/src/go/src/pkg/runtime/linux/thread.c:285
sigpanic()
main.*Configuration·ServeHTTP+0x183 /home/jpic/src/goserver/serve.go:53
main.*Configuration·ServeHTTP(0x0, 0xb779f180, 0xb770a150, 0x8)
http.*ServeMux·ServeHTTP+0x207 /home/jpic/src/go/src/pkg/http/server.go:508
http.*ServeMux·ServeHTTP(0x80e64a4, 0xb779f180, 0xb770a150, 0x1)
http.*Conn·serve+0x73 /home/jpic/src/go/src/pkg/http/server.go:289
http.*Conn·serve(0xb76a6818, 0xb779f180)
goexit /home/jpic/src/go/src/pkg/runtime/proc.c:145
goexit()
goroutine 3 [4]:
gosched+0x66 /home/jpic/src/go/src/pkg/runtime/proc.c:541
gosched()
chanrecv+0x14b /home/jpic/src/go/src/pkg/runtime/chan.c:355
chanrecv(0xb76b2430, 0xb76feb40, 0x1, 0x808e1ed)
runtime.chanrecv1+0x37 /home/jpic/src/go/src/pkg/runtime/chan.c:429
runtime.chanrecv1(0xb76b2400, 0xb76b6c84)
net.*pollServer·WaitRead+0x4d /home/jpic/src/go/src/pkg/net/fd.go:272
net.*pollServer·WaitRead(0xb76b2400, 0xb76b3f00, 0xb)
net.*netFD·Read+0x19b /home/jpic/src/go/src/pkg/net/fd.go:380
net.*netFD·Read(0xb76fe740, 0xb76b3f00, 0x1, 0x8, 0x0, ...)
net.*TCPConn·Read+0x70 /home/jpic/src/go/src/pkg/net/tcpsock.go:92
net.*TCPConn·Read(0xb76b3f00, 0xb7799940, 0x8, 0x8, 0x0, ...)
fcgi.*lockReadWriteCloser·Read+0x50 /home/jpic/src/goserver/fcgi/listener.go:558
fcgi.*lockReadWriteCloser·Read(0xb76a6900, 0xb7799940, 0x8, 0x8, 0x0, ...)
io.ReadAtLeast+0x91 /home/jpic/src/go/src/pkg/io/io.go:171
io.ReadAtLeast(0xb76fe580, 0xb7799940, 0x8, 0x8, 0x0, ...)
io.ReadFull+0x4b /home/jpic/src/go/src/pkg/io/io.go:191
io.ReadFull(0xb76feaa0, 0xb76fe580, 0xb7799940, 0x8, 0x8, ...)
encoding/binary.Read+0x1fe /home/jpic/src/go/src/pkg/encoding/binary/binary.go:138
encoding/binary.Read(0xb76feaa0, 0xb76fe580, 0xb7799940, 0x8, 0x8, ...)
fcgi.*wsConn·readAllPackets+0xb2 /home/jpic/src/goserver/fcgi/handler.go:191
fcgi.*wsConn·readAllPackets(0xb76feaa0, 0xb76fe580)
goexit /home/jpic/src/go/src/pkg/runtime/proc.c:145
goexit()
goroutine 2 [3]:
runtime.entersyscall+0x5d /home/jpic/src/go/src/pkg/runtime/proc.c:562
runtime.entersyscall()
syscall.Syscall6+0x5 /home/jpic/src/go/src/pkg/syscall/asm_linux_386.s:40
syscall.Syscall6()
syscall.EpollWait+0x71 /home/jpic/src/go/src/pkg/syscall/zsyscall_linux_386.go:132
syscall.EpollWait(0x100, 0x6, 0xb779c900, 0x1, 0xffffffff, ...)
net.*pollster·WaitFD+0x111 /home/jpic/src/go/src/pkg/net/fd_linux.go:116
net.*pollster·WaitFD(0x6, 0xb779c900, 0x1, 0x1, 0xffffffff, ...)
net.*pollServer·Run+0xc9 /home/jpic/src/go/src/pkg/net/fd.go:232
net.*pollServer·Run(0xb76a68a8, 0x0)
goexit /home/jpic/src/go/src/pkg/runtime/proc.c:145
goexit()
goroutine 1 [4]:
gosched+0x66 /home/jpic/src/go/src/pkg/runtime/proc.c:541
gosched()
chanrecv+0x14b /home/jpic/src/go/src/pkg/runtime/chan.c:355
chanrecv(0xb779ec70, 0xb76fe560, 0x1, 0x808e1ed)
runtime.chanrecv1+0x37 /home/jpic/src/go/src/pkg/runtime/chan.c:429
runtime.chanrecv1(0xb779ec40, 0xb76b5cd4)
net.*pollServer·WaitRead+0x4d /home/jpic/src/go/src/pkg/net/fd.go:272
net.*pollServer·WaitRead(0xb779ec40, 0xb76b3180, 0x0)
net.*netFD·accept+0x2c2 /home/jpic/src/go/src/pkg/net/fd.go:508
net.*netFD·accept(0xb76fe740, 0xb76b3180, 0x0, 0x0, 0xb, ...)
net.*TCPListener·AcceptTCP+0x53 /home/jpic/src/go/src/pkg/net/tcpsock.go:253
net.*TCPListener·AcceptTCP(0xb76b3180, 0x809509c, 0x0, 0x0)
net.*TCPListener·Accept+0x36 /home/jpic/src/go/src/pkg/net/tcpsock.go:263
net.*TCPListener·Accept(0xb77998e8, 0xb76b5e64, 0x0, 0x0, 0xb77999e0, ...)
http.Serve+0x6b /home/jpic/src/go/src/pkg/http/server.go:551
http.Serve(0xb77998e8, 0x0, 0x0, 0x0, 0x0, ...)
http.ListenAndServe+0x84 /home/jpic/src/go/src/pkg/http/server.go:596
http.ListenAndServe(0xb76fe820, 0xb77998e8, 0xb76fe800, 0xb76a6818, 0xb76fe820, ...)
main.main+0x24e /home/jpic/src/goserver/serve.go:75
main.main()
mainstart+0xf /home/jpic/src/go/src/pkg/runtime/386/asm.s:83
mainstart()
goexit /home/jpic/src/go/src/pkg/runtime/proc.c:145
goexit()
The fastcgi server should be running:
(music)natacha newproj # netstat -a | grep 9001
tcp 0 0 localhost:9001 *:* LISTEN
tcp 0 0 localhost:40842 localhost:9001 TIME_WAIT
tcp 0 0 localhost:40844 localhost:9001 TIME_WAIT
(music)natacha newproj # ps aux | grep fcgi
root 20173 0.0 0.9 13856 8384 ? S 16:30 0:00 python ./manage.py runfcgi protocol=fcgi host=localhost port=9001
root 20174 0.0 0.8 13856 8064 ? S 16:30 0:00 python ./manage.py runfcgi protocol=fcgi host=localhost port=9001
root 20175 0.0 0.8 13856 7904 ? S 16:30 0:00 python ./manage.py runfcgi protocol=fcgi host=localhost port=9001
root 20176 0.0 0.8 13856 7904 ? S 16:30 0:00 python ./manage.py runfcgi protocol=fcgi host=localhost port=9001
root 20177 0.0 0.8 13856 8064 ? S 16:30 0:00 python ./manage.py runfcgi protocol=fcgi host=localhost port=9001
root 20178 0.0 0.8 13856 8004 ? S 16:30 0:00 python ./manage.py runfcgi protocol=fcgi host=localhost port=9001
root 20362 0.0 0.0 3460 664 pts/12 S+ 16:39 0:00 grep --colour=auto fcgi
*/
@jldailey
Copy link

I'm not 100% sure what's happening but you can see where the error comes from:
main.*Configuration·ServeHTTP(0x0, 0xb779f180, 0xb770a150, 0x8)

That "0x0" in there should be a *http.Conn, but nothing in that stack trace has gotten to the fcgi code yet.

@jpic
Copy link
Author

jpic commented Apr 28, 2010

Thanks again, i'll let you know how it goes when i figure the fix

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment