Skip to content

proposal: embed: embed and expose also media/mime type #66121

@mitar

Description

@mitar

Proposal Details

I think this is a common use case: we use embed to embed some files at compile time and then we serve them at runtime using HTTP. But to serve files at runtime, one has to also know the media/mime type of the file, to set Content-Type correctly. One way to do that is to use standard mime.TypeByExtension on the embedded filenames. This works well if filenames have known file extensions and developer can generally predict the outcome if they notice a file extension is missing or if the file extension is an obscure one.

But what is hard to observe by a developer is that mime.TypeByExtension requires system installation of a corresponding database. If that database is missing, then behavior on their local machine might differ from the production machine. Even worse, the behavior might be different if the database is different (e.g., locally one uses Ubuntu and production uses Alpine).

To make production deployment work well, we use static builds. But sadly, because of the dependency on this database, even static build can behave differently.

Proposal

I propose that embedded files with embed.FS detect the media/mime type at compile time and expose it in some way to the developer (I am open to how exactly this is implemented). The critical point here is that this is done at compile where developer has easier time controlling the environment, so that the behavior is the same wherever the binary is later on deployed/ran.

Alternative proposal

Maybe there should be a way to embed currently available mime/media type database into the binary at compile time.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Status

    Incoming

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions