Skip to content

Commit 2ff059a

Browse files
committed
shift to file-based cache, leveraging existing metas indexer
1 parent e4879a3 commit 2ff059a

10 files changed

Lines changed: 727 additions & 381 deletions

File tree

hack/demo/graphql-demo-server/main.go

Lines changed: 41 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,63 @@
11
package main
22

33
import (
4+
"context"
5+
"encoding/json"
46
"fmt"
5-
"io/fs"
67
"net/http"
7-
"net/url"
88
"os"
99
"os/signal"
1010
"syscall"
1111
"testing/fstest"
12+
"time"
1213

13-
"github.com/operator-framework/operator-controller/internal/catalogd/server"
14-
"github.com/operator-framework/operator-controller/internal/catalogd/service"
15-
)
16-
17-
// demoCatalogStore implements server.CatalogStore backed by an in-memory FS
18-
type demoCatalogStore struct {
19-
catalogs map[string]fs.FS
20-
}
21-
22-
func (s *demoCatalogStore) GetCatalogData(catalog string) (*os.File, os.FileInfo, error) {
23-
return nil, nil, fmt.Errorf("not implemented for demo")
24-
}
14+
"github.com/graphql-go/graphql"
2515

26-
func (s *demoCatalogStore) GetCatalogFS(catalog string) (fs.FS, error) {
27-
catFS, ok := s.catalogs[catalog]
28-
if !ok {
29-
return nil, fs.ErrNotExist
30-
}
31-
return catFS, nil
32-
}
33-
34-
func (s *demoCatalogStore) GetIndex(catalog string) (server.Index, error) {
35-
return nil, fmt.Errorf("not implemented for demo")
36-
}
16+
gql "github.com/operator-framework/operator-controller/internal/catalogd/graphql"
17+
)
3718

3819
func main() {
3920
addr := ":9376"
4021
if v := os.Getenv("PORT"); v != "" {
4122
addr = ":" + v
4223
}
4324

44-
store := &demoCatalogStore{
45-
catalogs: map[string]fs.FS{
46-
"example-catalog": buildCatalog(),
47-
},
25+
catalogFS := buildCatalog()
26+
dynamicSchema, err := gql.LoadAndSummarizeCatalogDynamic(catalogFS)
27+
if err != nil {
28+
fmt.Fprintf(os.Stderr, "failed to build schema: %v\n", err)
29+
os.Exit(1)
4830
}
49-
50-
graphqlSvc := service.NewCachedGraphQLService()
51-
rootURL, _ := url.Parse("/catalogs/")
52-
handlers := server.NewCatalogHandlers(
53-
store,
54-
graphqlSvc,
55-
rootURL,
56-
server.MetasHandlerDisabled,
57-
server.GraphQLQueriesEnabled,
58-
)
31+
gql.PrintCatalogSummary(dynamicSchema)
5932

6033
mux := http.NewServeMux()
61-
mux.Handle("/catalogs/", handlers.Handler())
34+
mux.HandleFunc("POST /catalogs/{catalog}/api/v1/graphql", func(w http.ResponseWriter, r *http.Request) {
35+
r.Body = http.MaxBytesReader(w, r.Body, 1<<20)
36+
37+
var params struct {
38+
Query string `json:"query"`
39+
}
40+
if err := json.NewDecoder(r.Body).Decode(&params); err != nil {
41+
http.Error(w, "Invalid request body", http.StatusBadRequest)
42+
return
43+
}
44+
if params.Query == "" {
45+
http.Error(w, "Query cannot be empty", http.StatusBadRequest)
46+
return
47+
}
48+
49+
ctx, cancel := context.WithTimeout(r.Context(), 5*time.Second)
50+
defer cancel()
51+
52+
result := graphql.Do(graphql.Params{
53+
Schema: dynamicSchema.Schema,
54+
RequestString: params.Query,
55+
Context: ctx,
56+
})
57+
58+
w.Header().Set("Content-Type", "application/json")
59+
json.NewEncoder(w).Encode(result) //nolint:errcheck
60+
})
6261

6362
fmt.Fprintf(os.Stderr, "GraphQL demo server listening on http://localhost%s\n", addr)
6463
fmt.Fprintf(os.Stderr, "Endpoint: POST http://localhost%s/catalogs/example-catalog/api/v1/graphql\n", addr)
@@ -78,16 +77,15 @@ func main() {
7877
fmt.Fprintln(os.Stderr, "\nShutting down.")
7978
}
8079

81-
func buildCatalog() fs.FS {
82-
return fstest.MapFS{
80+
func buildCatalog() *fstest.MapFS {
81+
return &fstest.MapFS{
8382
"catalog.json": &fstest.MapFile{
8483
Data: []byte(catalogJSON),
8584
},
8685
}
8786
}
8887

8988
// catalogJSON contains sample FBC data for demonstration purposes.
90-
// Each line is a separate JSON object (JSONL format parsed by WalkMetasFS).
9189
const catalogJSON = `{"schema":"olm.package","name":"database-operator","defaultChannel":"stable","description":"An operator for managing database instances."}
9290
{"schema":"olm.package","name":"logging-operator","defaultChannel":"stable","description":"Logging operator for collecting and forwarding application logs."}
9391
{"schema":"olm.package","name":"messaging-operator","defaultChannel":"stable","description":"Messaging broker operator based on Apache Kafka."}

0 commit comments

Comments
 (0)