github.com/greenboxal/deis@v1.12.1/logger/weblog/server.go (about)

     1  package weblog
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"net/http"
     7  
     8  	"github.com/deis/deis/logger/syslogish"
     9  )
    10  
    11  // Server implements a simple HTTP server that handles GET and DELETE requests for application
    12  // logs.  These actions are accomplished by delegating to a syslogish.Server, which will broker
    13  // communication between its underlying storage.Adapter and this weblog server.
    14  type Server struct {
    15  	listening bool
    16  	bindHost  string
    17  	bindPort  int
    18  	handler   *requestHandler
    19  }
    20  
    21  // NewServer returns a pointer to a new Server instance.
    22  func NewServer(bindHost string, bindPort int, syslogishServer *syslogish.Server) (*Server, error) {
    23  	return &Server{
    24  		bindHost: bindHost,
    25  		bindPort: bindPort,
    26  		handler:  &requestHandler{syslogishServer: syslogishServer},
    27  	}, nil
    28  }
    29  
    30  // Listen starts the server's main loop.
    31  func (s *Server) Listen() {
    32  	// Should only ever be called once
    33  	if !s.listening {
    34  		s.listening = true
    35  		go s.listen()
    36  		log.Println("weblog server running")
    37  	}
    38  }
    39  
    40  func (s *Server) listen() {
    41  	http.Handle("/", s.handler)
    42  	if err := http.ListenAndServe(fmt.Sprintf("%s:%d", s.bindHost, s.bindPort), nil); err != nil {
    43  		log.Fatal("weblog server stopped", err)
    44  	}
    45  }