Skip to content

Commit

Permalink
Respond to CLI -j flag for Mist compatibility (#9)
Browse files Browse the repository at this point in the history
* Return Mist-parseable info for -j flag

* Add Git SHA
  • Loading branch information
thomshutt authored Aug 5, 2022
1 parent e052bfb commit 5d1a1ca
Show file tree
Hide file tree
Showing 9 changed files with 231 additions and 17 deletions.
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
GO_BUILD_DIR?=build/

ldflags := -X 'github.com/livepeer/catalyst-api/config.Version=$(shell git rev-parse HEAD)'

.PHONY: all
all: build-server

.PHONY: build-server
build-server:
go build -ldflags="$(GO_LDFLAG_VERSION)" -o "$(GO_BUILD_DIR)catalyst-api" cmd/http-server/http-server.go
go build -ldflags="$(ldflags)" -o "$(GO_BUILD_DIR)catalyst-api" cmd/http-server/http-server.go
19 changes: 14 additions & 5 deletions cmd/http-server/http-server.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,36 @@ import (
"net/http"

"github.com/julienschmidt/httprouter"
"github.com/livepeer/catalyst-api/config"
"github.com/livepeer/catalyst-api/handlers"
"github.com/livepeer/catalyst-api/middleware"
"github.com/livepeer/livepeer-data/pkg/mistconnector"
)

func main() {
port := flag.Int("port", 4949, "Port to listen on")
mistJson := flag.Bool("j", false, "Print application info as JSON. Used by Mist to present flags in its UI.")
flag.Parse()

if *mistJson {
mistconnector.PrintMistConfigJson("mist-api-connector", "Sidecar for connecting Mist with Catalyst API", "Mist API Connector", config.Version, flag.CommandLine)
return
}

listen := fmt.Sprintf("localhost:%d", *port)
router := StartDMSAPIRouter()
router := StartCatalystAPIRouter()

log.Println("Starting DMS API server listening on", listen)
log.Println("Starting Catalyst API version", config.Version, "listening on", listen)
err := http.ListenAndServe(listen, router)
log.Fatal(err)

}

func StartDMSAPIRouter() *httprouter.Router {
func StartCatalystAPIRouter() *httprouter.Router {
router := httprouter.New()

router.GET("/ok", middleware.IsAuthorized(handlers.DMSAPIHandlers.Ok()))
router.POST("/api/vod", middleware.IsAuthorized(handlers.DMSAPIHandlers.UploadVOD()))
router.GET("/ok", middleware.IsAuthorized(handlers.CatalystAPIHandlers.Ok()))
router.POST("/api/vod", middleware.IsAuthorized(handlers.CatalystAPIHandlers.UploadVOD()))

return router
}
2 changes: 1 addition & 1 deletion cmd/http-server/http-server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

func TestInitServer(t *testing.T) {
require := require.New(t)
router := StartDMSAPIRouter()
router := StartCatalystAPIRouter()

handle, _, _ := router.Lookup("GET", "/ok")
require.NotNil(handle)
Expand Down
3 changes: 3 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package config

var Version string
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/kr/pretty v0.3.0 // indirect
github.com/livepeer/livepeer-data v0.4.19 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f // indirect
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
Expand Down
199 changes: 199 additions & 0 deletions go.sum

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@ import (
"github.com/xeipuuv/gojsonschema"
)

type DMSAPIHandlersCollection struct{}
type CatalystAPIHandlersCollection struct{}

var DMSAPIHandlers = DMSAPIHandlersCollection{}
var CatalystAPIHandlers = CatalystAPIHandlersCollection{}

func (d *DMSAPIHandlersCollection) Ok() httprouter.Handle {
func (d *CatalystAPIHandlersCollection) Ok() httprouter.Handle {
return func(w http.ResponseWriter, req *http.Request, _ httprouter.Params) {
io.WriteString(w, "OK")
}
}

func (d *DMSAPIHandlersCollection) UploadVOD() httprouter.Handle {
func (d *CatalystAPIHandlersCollection) UploadVOD() httprouter.Handle {
schemaLoader := gojsonschema.NewStringLoader(`{
"type": "object",
"properties": {
Expand Down
8 changes: 4 additions & 4 deletions handlers/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func TestOKHandler(t *testing.T) {
router := httprouter.New()
req, _ := http.NewRequest("GET", "/ok", nil)
rr := httptest.NewRecorder()
router.GET("/ok", DMSAPIHandlers.Ok())
router.GET("/ok", CatalystAPIHandlers.Ok())
router.ServeHTTP(rr, req)

require.Equal(rr.Body.String(), "OK")
Expand Down Expand Up @@ -46,7 +46,7 @@ func TestSuccessfulVODUploadHandler(t *testing.T) {
req.Header.Set("Content-Type", "application/json")

rr := httptest.NewRecorder()
router.POST("/api/vod", DMSAPIHandlers.UploadVOD())
router.POST("/api/vod", CatalystAPIHandlers.UploadVOD())
router.ServeHTTP(rr, req)

require.Equal(rr.Result().StatusCode, 200)
Expand Down Expand Up @@ -106,7 +106,7 @@ func TestInvalidPayloadVODUploadHandler(t *testing.T) {

router := httprouter.New()

router.POST("/api/vod", DMSAPIHandlers.UploadVOD())
router.POST("/api/vod", CatalystAPIHandlers.UploadVOD())
for _, payload := range badRequests {
req, _ := http.NewRequest("POST", "/api/vod", bytes.NewBuffer(payload))
req.Header.Set("Content-Type", "application/json")
Expand Down Expand Up @@ -136,7 +136,7 @@ func TestWrongContentTypeVODUploadHandler(t *testing.T) {
req.Header.Set("Content-Type", "json")

rr := httptest.NewRecorder()
router.POST("/api/vod", DMSAPIHandlers.UploadVOD())
router.POST("/api/vod", CatalystAPIHandlers.UploadVOD())
router.ServeHTTP(rr, req)

require.Equal(rr.Result().StatusCode, 415)
Expand Down
4 changes: 2 additions & 2 deletions middleware/middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func TestNoAuthHeader(t *testing.T) {
router := httprouter.New()
req, _ := http.NewRequest("GET", "/ok", nil)
rr := httptest.NewRecorder()
router.GET("/ok", IsAuthorized(handlers.DMSAPIHandlers.Ok()))
router.GET("/ok", IsAuthorized(handlers.CatalystAPIHandlers.Ok()))
router.ServeHTTP(rr, req)

require.Equal(rr.Code, 401, "should return 401")
Expand All @@ -31,7 +31,7 @@ func TestWrongKey(t *testing.T) {
req.Header.Set("Authorization", "Bearer gibberish")

rr := httptest.NewRecorder()
router.GET("/ok", IsAuthorized(handlers.DMSAPIHandlers.Ok()))
router.GET("/ok", IsAuthorized(handlers.CatalystAPIHandlers.Ok()))
router.ServeHTTP(rr, req)

require.Equal(rr.Code, 401, "should return 401")
Expand Down

0 comments on commit 5d1a1ca

Please sign in to comment.