-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
43 lines (37 loc) · 1.3 KB
/
server.go
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
package server
import (
"net/http"
"os"
"path/filepath"
"github.com/roman-kulish/spatio-temporal-deduplication-example/cmd/example/app/config"
"github.com/roman-kulish/spatio-temporal-deduplication-example/cmd/example/app/dedup"
"github.com/roman-kulish/spatio-temporal-deduplication-example/cmd/example/app/server/response"
)
// HandlerFunc is a wrapped handler functions.
type HandlerFunc func(*dedup.SpatioTemporalFilter, http.ResponseWriter, *http.Request) error
// WithSpatioTemporalFilter wraps handler function into http.HandlerFunc and
// injects dedup.Filter.
func WithSpatioTemporalFilter(f *dedup.SpatioTemporalFilter, fn HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if err := fn(f, w, r); err != nil {
response.SendError(w, err)
}
}
}
// New creates, configures and returns an instance of http.Server.
func New(cfg *config.Server, filter *dedup.SpatioTemporalFilter) (*http.Server, error) {
dir, err := os.Getwd()
if err != nil {
return nil, err
}
dir = filepath.Join(dir, "public")
s := http.Server{
Addr: cfg.Addr,
ReadTimeout: cfg.ReadTimeout,
ReadHeaderTimeout: cfg.ReadHeaderTimeout,
WriteTimeout: cfg.WriteTimeout,
IdleTimeout: cfg.IdleTimeout,
}
s.Handler = initRoutes(dir, filter)
return &s, nil
}