Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

zed serve: Add -defaultfmt flag #4379

Merged
merged 2 commits into from
Feb 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions api/mime.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,31 +69,31 @@ func MediaTypeToFormat(s string, dflt string) (string, error) {
return "", &ErrUnsupportedMimeType{typ}
}

func FormatToMediaType(format string) string {
func FormatToMediaType(format string) (string, error) {
switch format {
case "arrows":
return MediaTypeArrowStream
return MediaTypeArrowStream, nil
case "csv":
return MediaTypeCSV
return MediaTypeCSV, nil
case "json":
return MediaTypeJSON
return MediaTypeJSON, nil
case "line":
return MediaTypeLine
return MediaTypeLine, nil
case "ndjson":
return MediaTypeNDJSON
return MediaTypeNDJSON, nil
case "parquet":
return MediaTypeParquet
return MediaTypeParquet, nil
case "vng":
return MediaTypeVNG
return MediaTypeVNG, nil
case "zeek":
return MediaTypeZeek
return MediaTypeZeek, nil
case "zjson":
return MediaTypeZJSON
return MediaTypeZJSON, nil
case "zng":
return MediaTypeZNG
return MediaTypeZNG, nil
case "zson":
return MediaTypeZSON
return MediaTypeZSON, nil
default:
panic(fmt.Sprintf("unknown format type: %s", format))
return "", fmt.Errorf("unknown format type: %s", format)
}
}
1 change: 1 addition & 0 deletions cmd/zed/serve/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func New(parent charm.Command, f *flag.FlagSet) (charm.Command, error) {
c.conf.CORSAllowedOrigins = append(c.conf.CORSAllowedOrigins, s)
return nil
})
f.StringVar(&c.conf.DefaultResponseFormat, "defaultfmt", service.DefaultZedFormat, "default response format")
f.StringVar(&c.listenAddr, "l", ":9867", "[addr]:port to listen on")
f.StringVar(&c.portFile, "portfile", "", "write listen port to file")
f.StringVar(&c.rootContentFile, "rootcontentfile", "", "file to serve for GET /")
Expand Down
21 changes: 14 additions & 7 deletions service/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,13 @@ const indexPage = `
</html>`

type Config struct {
Auth AuthConfig
CORSAllowedOrigins []string
Root *storage.URI
RootContent io.ReadSeeker
Version string
Logger *zap.Logger
Auth AuthConfig
CORSAllowedOrigins []string
DefaultResponseFormat string
Root *storage.URI
RootContent io.ReadSeeker
Version string
Logger *zap.Logger
}

type Core struct {
Expand All @@ -67,6 +68,12 @@ type Core struct {
}

func NewCore(ctx context.Context, conf Config) (*Core, error) {
if conf.DefaultResponseFormat == "" {
conf.DefaultResponseFormat = DefaultZedFormat
}
if _, err := api.FormatToMediaType(conf.DefaultResponseFormat); err != nil {
return nil, fmt.Errorf("invalid default response format: %w", err)
}
if conf.Logger == nil {
conf.Logger = zap.NewNop()
}
Expand Down Expand Up @@ -178,7 +185,7 @@ func (c *Core) addAPIServerRoutes() {

func (c *Core) handler(f func(*Core, *ResponseWriter, *Request)) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if res, req, ok := newRequest(w, r, c.logger); ok {
if res, req, ok := newRequest(w, r, c); ok {
f(c, res, req)
}
})
Expand Down
20 changes: 14 additions & 6 deletions service/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ type Request struct {
Logger *zap.Logger
}

func newRequest(w http.ResponseWriter, r *http.Request, logger *zap.Logger) (*ResponseWriter, *Request, bool) {
logger = logger.With(zap.String("request_id", api.RequestIDFromContext(r.Context())))
func newRequest(w http.ResponseWriter, r *http.Request, c *Core) (*ResponseWriter, *Request, bool) {
logger := c.logger.With(zap.String("request_id", api.RequestIDFromContext(r.Context())))
req := &Request{
Request: r,
Logger: logger,
Expand All @@ -51,7 +51,7 @@ func newRequest(w http.ResponseWriter, r *http.Request, logger *zap.Logger) (*Re
ss = []string{""}
}
for _, mime := range ss {
format, err := api.MediaTypeToFormat(mime, DefaultZedFormat)
format, err := api.MediaTypeToFormat(mime, c.conf.DefaultResponseFormat)
if err != nil {
continue
}
Expand Down Expand Up @@ -222,8 +222,12 @@ func (w *ResponseWriter) ContentType() string {

func (w *ResponseWriter) ZioWriter() zio.WriteCloser {
if w.zw == nil {
w.Header().Set("Content-Type", api.FormatToMediaType(w.Format))
var err error
typ, err := api.FormatToMediaType(w.Format)
if err != nil {
w.Error(err)
return nil
}
w.Header().Set("Content-Type", typ)
w.zw, err = anyio.NewWriter(zio.NopCloser(w), anyio.WriterOpts{Format: w.Format})
if err != nil {
w.Error(err)
Expand All @@ -234,7 +238,11 @@ func (w *ResponseWriter) ZioWriter() zio.WriteCloser {
}
func (w *ResponseWriter) Write(b []byte) (int, error) {
if atomic.CompareAndSwapInt32(&w.written, 0, 1) {
w.Header().Set("Content-Type", api.FormatToMediaType(w.Format))
typ, err := api.FormatToMediaType(w.Format)
if err != nil {
return 0, err
}
w.Header().Set("Content-Type", typ)
}
return w.ResponseWriter.Write(b)
}
Expand Down
15 changes: 15 additions & 0 deletions service/ztests/default-format.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
script: |
export LAKE_EXTRA_FLAGS='-defaultfmt=ndjson'
source service.sh
zed create -q test
zed use -q test
echo '{x: 1(uint64)}' | zed load -q -
curl -d '{"query": "from test"}' $ZED_LAKE/query

inputs:
- name: service.sh

outputs:
- name: stdout
data: |
{"x":1}