-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathserver.go
More file actions
36 lines (32 loc) · 797 Bytes
/
server.go
File metadata and controls
36 lines (32 loc) · 797 Bytes
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
package elogger
import (
"fmt"
"log"
"net/http"
)
// Server represents a server
type Server struct {
service Service
config *Config
}
// Start start server
func (s *Server) Start() error {
mux := http.NewServeMux()
mux.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
err := s.service.Log(writer, request)
if err != nil {
http.Error(writer, fmt.Sprintf("%v", err), http.StatusInternalServerError)
return
}
})
fmt.Printf("Started test server on port %v\n", s.config.Port)
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%v", s.config.Port), mux))
return nil
}
// NewServer creates a new server with supplied config.
func NewServer(config *Config, service Service) (*Server, error) {
return &Server{
service: service,
config: config,
}, nil
}