-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathservice.go
108 lines (99 loc) · 3.35 KB
/
service.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package service
import (
"fmt"
"net/http"
"github.com/NYTimes/gizmo/server"
"github.com/NYTimes/gziphandler"
"github.com/fsouza/ctxlogger"
"github.com/gorilla/handlers"
"github.com/sirupsen/logrus"
"github.com/video-dev/video-transcoding-api/v2/config"
"github.com/video-dev/video-transcoding-api/v2/db"
"github.com/video-dev/video-transcoding-api/v2/db/redis"
"github.com/video-dev/video-transcoding-api/v2/swagger"
)
// TranscodingService will implement server.JSONService and handle all requests
// to the server.
type TranscodingService struct {
config *config.Config
db db.Repository
logger *logrus.Logger
}
// NewTranscodingService will instantiate a JSONService
// with the given configuration.
func NewTranscodingService(cfg *config.Config, logger *logrus.Logger) (*TranscodingService, error) {
dbRepo, err := redis.NewRepository(cfg)
if err != nil {
return nil, fmt.Errorf("error initializing Redis client: %s", err)
}
return &TranscodingService{config: cfg, db: dbRepo, logger: logger}, nil
}
// Prefix returns the string prefix used for all endpoints within
// this service.
func (s *TranscodingService) Prefix() string {
return ""
}
// Middleware provides an http.Handler hook wrapped around all requests.
// In this implementation, we're using a GzipHandler middleware to
// compress our responses.
func (s *TranscodingService) Middleware(h http.Handler) http.Handler {
logMiddleware := ctxlogger.ContextLogger(s.logger)
h = logMiddleware(h)
if s.config.Server.HTTPAccessLog == nil {
h = handlers.LoggingHandler(s.logger.Writer(), h)
}
return gziphandler.GzipHandler(server.CORSHandler(h, ""))
}
// JSONMiddleware provides a JSONEndpoint hook wrapped around all requests.
func (s *TranscodingService) JSONMiddleware(j server.JSONEndpoint) server.JSONEndpoint {
return func(r *http.Request) (int, interface{}, error) {
status, res, err := j(r)
if err != nil {
return swagger.NewErrorResponse(err).WithStatus(status).Result()
}
return status, res, nil
}
}
// JSONEndpoints is a listing of all endpoints available in the JSONService.
func (s *TranscodingService) JSONEndpoints() map[string]map[string]server.JSONEndpoint {
return map[string]map[string]server.JSONEndpoint{
"/jobs": {
"POST": swagger.HandlerToJSONEndpoint(s.newTranscodeJob),
},
"/jobs/{jobId}": {
"GET": swagger.HandlerToJSONEndpoint(s.getTranscodeJob),
},
"/jobs/{jobId}/cancel": {
"POST": swagger.HandlerToJSONEndpoint(s.cancelTranscodeJob),
},
"/presets": {
"POST": swagger.HandlerToJSONEndpoint(s.newPreset),
},
"/presets/{name}": {
"DELETE": swagger.HandlerToJSONEndpoint(s.deletePreset),
},
"/presetmaps": {
"POST": swagger.HandlerToJSONEndpoint(s.newPresetMap),
"GET": swagger.HandlerToJSONEndpoint(s.listPresetMaps),
},
"/presetmaps/{name}": {
"GET": swagger.HandlerToJSONEndpoint(s.getPresetMap),
"PUT": swagger.HandlerToJSONEndpoint(s.updatePresetMap),
"DELETE": swagger.HandlerToJSONEndpoint(s.deletePresetMap),
},
"/providers": {
"GET": swagger.HandlerToJSONEndpoint(s.listProviders),
},
"/providers/{name}": {
"GET": swagger.HandlerToJSONEndpoint(s.getProvider),
},
}
}
// Endpoints is a list of all non-json endpoints.
func (s *TranscodingService) Endpoints() map[string]map[string]http.HandlerFunc {
return map[string]map[string]http.HandlerFunc{
"/swagger.json": {
"GET": s.swaggerManifest,
},
}
}