Skip to content

Instantly share code, notes, and snippets.

@divoxx
Created December 10, 2009 04:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save divoxx/253106 to your computer and use it in GitHub Desktop.
Save divoxx/253106 to your computer and use it in GitHub Desktop.
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