Created
December 10, 2009 04:13
-
-
Save divoxx/253106 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"; "log"; "strings";) | |
/** | |
* A service running in an endpoint. | |
*/ | |
type Service struct { | |
responseBody string; | |
} | |
/** | |
* Application server | |
*/ | |
type Server struct { | |
address string; | |
} | |
/** | |
* Creates a new server defaulting to port 8080 | |
*/ | |
func NewServer() (*Server) { | |
return &Server{address: ":8080"}; | |
} | |
/** | |
* Server method that responds the HTTP requests. | |
*/ | |
func (service *Service) ServeHTTP(conn *http.Conn, req *http.Request) { | |
log.Stdoutf("%s %s", req.Method, req.RawURL); | |
conn.SetHeader("Content-Type", "text/plain; charset=utf-8"); | |
conn.Write(strings.Bytes(service.responseBody)); | |
} | |
/** | |
* Runs the server | |
*/ | |
func (server *Server) run() { | |
err := http.ListenAndServe(server.address, nil); | |
if err != nil { | |
log.Exit("ListenAndServe:", err); | |
} | |
} | |
func main() { | |
server := NewServer(); | |
http.Handle("/", &Service{responseBody: "At Home!"}); | |
http.Handle("/hello_world", &Service{responseBody: "Hello World!"}); | |
server.run() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment