Celerity can serve static files and make entire paths public. Although, in most cases, this isn’t advised and static content should be served via a CDN.

When serving files celerity will automatically fingerprint the file type and set the appropriate headers.

Serving single files

To serve a single file scopes have a ServeFile function which will create a route that will serve the file specified at its path. The first parameter is the url path for the route and the second parameter is a local filepath on the system.

svr = celerity.New()
svr.ServeFile("/downloads/mydownload.zip", "/opt/myproject/downloads/project.zip")

Serving entire paths

Celerity can serve entire local paths and all of the files anywhere below it. This can be accomplished by passing a url path and a local path to the ServePath function.

svr = celerity.New()
svr.ServePath("/downloads", "/opt/myproject/downloads")

A file located at /opt/myproject/downloads/v1/project.zip would be available to download at http://domain.com/downloads/v1/project.zip.

Wildcard routes and local files

You can create a catchall route to serve a static file. This is useful for serving a single page application such as a ReactJS app. This can be accomplished by using a wildcard route and the ServeFile function.

svr = celerity.New()

// A few API endpoints
api := svr.Scope("/api")
api.GET("/users", listUsers)
api.POST("/users", newUser)

// Serve files in the public folder such as javascript and css assets.
svr.ServePath("/", "./public")
// Serve index.html for all other routes
svr.ServeFile("*", "./public/index.html")
last modified Wednesday, July 11, 2018
Previous in Guides
Response structure
Control the format that responses are given in or the JSON structure that is produced.
Next in Guides
Websockets, Channels, and Rooms
Using the celerity Channel functionality to use web sockets for real time communication
Celerity is maintained by 5Sigma. Source code is available at Github.