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