mirror of
https://github.com/anyproto/anytype-heart.git
synced 2025-06-08 05:47:07 +09:00
GO-5589: Fix embed openapi spec
This commit is contained in:
parent
948f9e7781
commit
060457fe7e
5 changed files with 19 additions and 23 deletions
|
@ -23,7 +23,7 @@ const (
|
|||
|
||||
// NewRouter builds and returns a *gin.Engine with all routes configured.
|
||||
|
||||
func (s *Server) NewRouter(mw apicore.ClientCommands, eventService apicore.EventService) *gin.Engine {
|
||||
func (s *Server) NewRouter(mw apicore.ClientCommands, eventService apicore.EventService, openapiYAML []byte, openapiJSON []byte) *gin.Engine {
|
||||
isDebug := os.Getenv("ANYTYPE_API_DEBUG") == "1"
|
||||
if !isDebug {
|
||||
gin.SetMode(gin.ReleaseMode)
|
||||
|
@ -55,21 +55,11 @@ func (s *Server) NewRouter(mw apicore.ClientCommands, eventService apicore.Event
|
|||
})
|
||||
|
||||
router.GET("/docs/openapi.yaml", func(c *gin.Context) {
|
||||
data, err := os.ReadFile("./core/api/docs/openapi.yaml")
|
||||
if err != nil {
|
||||
c.String(http.StatusInternalServerError, "Failed to read OpenAPI spec")
|
||||
return
|
||||
}
|
||||
c.Data(http.StatusOK, "application/x-yaml", data)
|
||||
c.Data(http.StatusOK, "application/x-yaml", openapiYAML)
|
||||
})
|
||||
|
||||
router.GET("/docs/openapi.json", func(c *gin.Context) {
|
||||
data, err := os.ReadFile("./core/api/docs/openapi.json")
|
||||
if err != nil {
|
||||
c.String(http.StatusInternalServerError, "Failed to read OpenAPI spec")
|
||||
return
|
||||
}
|
||||
c.Data(http.StatusOK, "application/json", data)
|
||||
c.Data(http.StatusOK, "application/json", openapiJSON)
|
||||
})
|
||||
|
||||
// Auth routes (no authentication required)
|
||||
|
|
|
@ -16,7 +16,7 @@ func TestRouter_Unauthenticated(t *testing.T) {
|
|||
t.Run("GET /v1/spaces without auth returns 401", func(t *testing.T) {
|
||||
// given
|
||||
fx := newFixture(t)
|
||||
engine := fx.NewRouter(fx.mwMock, &fx.eventService)
|
||||
engine := fx.NewRouter(fx.mwMock, &fx.eventService, []byte{}, []byte{})
|
||||
w := httptest.NewRecorder()
|
||||
req := httptest.NewRequest("GET", "/v1/spaces", nil)
|
||||
|
||||
|
@ -32,7 +32,7 @@ func TestRouter_AuthRoute(t *testing.T) {
|
|||
t.Run("POST /v1/auth/token is accessible without auth", func(t *testing.T) {
|
||||
// given
|
||||
fx := newFixture(t)
|
||||
engine := fx.NewRouter(fx.mwMock, &fx.eventService)
|
||||
engine := fx.NewRouter(fx.mwMock, &fx.eventService, []byte{}, []byte{})
|
||||
w := httptest.NewRecorder()
|
||||
req := httptest.NewRequest("POST", "/v1/auth/token", nil)
|
||||
|
||||
|
@ -48,7 +48,7 @@ func TestRouter_MetadataHeader(t *testing.T) {
|
|||
t.Run("Response includes Anytype-Version header", func(t *testing.T) {
|
||||
// given
|
||||
fx := newFixture(t)
|
||||
engine := fx.NewRouter(fx.mwMock, &fx.eventService)
|
||||
engine := fx.NewRouter(fx.mwMock, &fx.eventService, []byte{}, []byte{})
|
||||
fx.KeyToToken = map[string]ApiSessionEntry{"validKey": {Token: "dummyToken", AppName: "dummyApp"}}
|
||||
fx.mwMock.On("ObjectSearch", mock.Anything, mock.Anything).
|
||||
Return(&pb.RpcObjectSearchResponse{
|
||||
|
|
|
@ -24,16 +24,15 @@ type Server struct {
|
|||
KeyToToken map[string]ApiSessionEntry // appKey -> token
|
||||
}
|
||||
|
||||
|
||||
// NewServer constructs a new Server with the default config and sets up the routes.
|
||||
func NewServer(mw apicore.ClientCommands, accountService apicore.AccountService, eventService apicore.EventService) *Server {
|
||||
func NewServer(mw apicore.ClientCommands, accountService apicore.AccountService, eventService apicore.EventService, openapiYAML []byte, openapiJSON []byte) *Server {
|
||||
gatewayUrl, techSpaceId, err := getAccountInfo(accountService)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
s := &Server{service: service.NewService(mw, gatewayUrl, techSpaceId)}
|
||||
s.engine = s.NewRouter(mw, eventService)
|
||||
s.engine = s.NewRouter(mw, eventService, openapiYAML, openapiJSON)
|
||||
s.KeyToToken = make(map[string]ApiSessionEntry)
|
||||
|
||||
return s
|
||||
|
|
|
@ -18,8 +18,8 @@ const (
|
|||
|
||||
type fixture struct {
|
||||
*Server
|
||||
eventService mock_apicore.MockEventService
|
||||
mwMock *mock_apicore.MockClientCommands
|
||||
eventService mock_apicore.MockEventService
|
||||
mwMock *mock_apicore.MockClientCommands
|
||||
}
|
||||
|
||||
func newFixture(t *testing.T) *fixture {
|
||||
|
@ -30,7 +30,7 @@ func newFixture(t *testing.T) *fixture {
|
|||
GatewayUrl: mockedGatewayUrl,
|
||||
TechSpaceId: mockedTechSpaceId,
|
||||
}, nil).Once()
|
||||
server := NewServer(mwMock, accountService, eventService)
|
||||
server := NewServer(mwMock, accountService, eventService, []byte{}, []byte{})
|
||||
|
||||
return &fixture{
|
||||
Server: server,
|
||||
|
|
|
@ -2,6 +2,7 @@ package api
|
|||
|
||||
import (
|
||||
"context"
|
||||
_ "embed"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
@ -24,6 +25,12 @@ const (
|
|||
|
||||
var (
|
||||
mwSrv apicore.ClientCommands
|
||||
|
||||
//go:embed docs/openapi.yaml
|
||||
openapiYAML []byte
|
||||
|
||||
//go:embed docs/openapi.json
|
||||
openapiJSON []byte
|
||||
)
|
||||
|
||||
type Service interface {
|
||||
|
@ -88,7 +95,7 @@ func (s *apiService) runServer() {
|
|||
return
|
||||
}
|
||||
|
||||
s.srv = server.NewServer(s.mw, s.accountService, s.eventService)
|
||||
s.srv = server.NewServer(s.mw, s.accountService, s.eventService, openapiJSON, openapiYAML)
|
||||
|
||||
s.httpSrv = &http.Server{
|
||||
Addr: s.listenAddr,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue