Skip to content

Commit

Permalink
pr feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
mattnibs committed Feb 14, 2023
1 parent bfc2f88 commit 473d6fd
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 27 deletions.
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)
}
}
2 changes: 1 addition & 1 deletion cmd/zed/serve/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +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.DefaultZedFormat, "defaultfmt", service.DefaultZedFormat, "default zed response format")
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
18 changes: 9 additions & 9 deletions service/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ const indexPage = `
</html>`

type Config struct {
Auth AuthConfig
CORSAllowedOrigins []string
DefaultZedFormat 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 Down Expand Up @@ -77,8 +77,8 @@ func NewCore(ctx context.Context, conf Config) (*Core, error) {
if conf.Version == "" {
conf.Version = "unknown"
}
if conf.DefaultZedFormat == "" {
conf.DefaultZedFormat = DefaultZedFormat
if conf.DefaultResponseFormat == "" {
conf.DefaultResponseFormat = DefaultZedFormat
}

registry := prometheus.NewRegistry()
Expand Down
16 changes: 12 additions & 4 deletions service/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func newRequest(w http.ResponseWriter, r *http.Request, c *Core) (*ResponseWrite
ss = []string{""}
}
for _, mime := range ss {
format, err := api.MediaTypeToFormat(mime, c.conf.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

0 comments on commit 473d6fd

Please sign in to comment.