Created
May 1, 2010 21:25
-
-
Save jpic/386670 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"http" | |
"regexp" | |
"strings" | |
"log" | |
"fmt" | |
"fcgi" | |
) | |
type Site struct { | |
HostRegexp *regexp.Regexp | |
Roots []http.FileHandler | |
FcgiDialers []fcgi.Dialer | |
FcgiHandler *http.Handler | |
} | |
type Httpd struct { | |
Address string | |
Sites []Site | |
} | |
var httpd = Httpd{ | |
"0.0.0.0:8000", | |
[]Site{ | |
Site{ | |
regexp.MustCompile("localhost:8000"), | |
[]http.FileHandler{ | |
http.FileHandler{ | |
"/tmp/b", | |
"/b", | |
}, | |
http.FileHandler{ | |
"/tmp/a", | |
"/a", | |
}, | |
}, | |
[]fcgi.Dialer{ | |
fcgi.NewDialer("tcp", "localhost:9001"), | |
}, | |
nil, | |
}, | |
}, | |
} | |
func (self *Httpd) ServeHTTP(c *http.Conn, request *http.Request) { | |
self.log("Access ", request.Host, request.RawURL) | |
for _, site := range self.Sites { | |
match := site.HostRegexp.ExecuteString( request.Host ) | |
if len(match) > 0 { | |
self.log("Site match ", site.HostRegexp) | |
for _, handler := range site.Roots { | |
self.log("Prefix test ", handler.Prefix) | |
if strings.HasPrefix( request.RawURL, handler.Prefix ) { | |
self.log("Prefix match ", handler.Prefix) | |
handler.ServeHTTP(c, request) | |
return | |
} | |
} | |
site.FcgiHandler.ServeHTTP(c, request) | |
} | |
} | |
} | |
func (self *Httpd) log(a ...interface{}) { | |
ret := fmt.Sprint(a) | |
log.Stdoutf("SERVER: %s", ret); | |
return | |
} | |
func (self *Httpd) start() { | |
self.log("Making FcgiHandlers") | |
for _, site := range self.Sites { | |
handler, err := fcgi.Handler(site.FcgiDialers) | |
site.FcgiHandler = &handler | |
if err != nil { | |
log.Exit("fcgi.Handler:", err) | |
} | |
} | |
self.log("Register route") | |
http.Handle("/", &httpd) | |
self.log("Starting goHttpd on ", httpd.Address) | |
err := http.ListenAndServe(httpd.Address, nil) | |
if err != nil { | |
log.Exit("ListenAndServe:", err) | |
} | |
} | |
func main() { | |
httpd.start() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment