-
Notifications
You must be signed in to change notification settings - Fork 262
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f10b5c5
commit cd5b92e
Showing
6 changed files
with
350 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
# Serving Static Files using GoFr | ||
|
||
Often, we are required to serve static content such as a default profile image, a favicon, or a background image for our | ||
web application. We want to have a mechanism to serve that static content without the hassle of implementing it from scratch. | ||
|
||
GoFr provides a default mechanism where if a `static` folder is available in the directory of the application, | ||
it automatically provides an endpoint with `/static/<filename>`, here filename refers to the file we want to get static content to be served. | ||
|
||
Example project structure: | ||
|
||
```dotenv | ||
project_folder | ||
| | ||
|---configs | ||
| .env | ||
|---static | ||
| img1.jpeg | ||
| img2.png | ||
| img3.jpeg | ||
| main.go | ||
| main_test.go | ||
``` | ||
|
||
main.go code: | ||
|
||
```go | ||
package main | ||
|
||
import "gofr.dev/pkg/gofr" | ||
|
||
func main(){ | ||
app := gofr.New() | ||
app.Run() | ||
} | ||
``` | ||
|
||
Additionally, if we want to serve more static endpoints, we have a dedicated function called `AddStaticFiles()` | ||
which takes 2 parameters `endpoint` and the `filepath` of the static folder which we want to serve. | ||
|
||
Example project structure: | ||
|
||
```dotenv | ||
project_folder | ||
| | ||
|---configs | ||
| .env | ||
|---static | ||
| img1.jpeg | ||
| img2.png | ||
| img3.jpeg | ||
|---public | ||
| |---css | ||
| | main.css | ||
| |---js | ||
| | main.js | ||
| | index.html | ||
| main.go | ||
| main_test.go | ||
``` | ||
|
||
main.go file: | ||
|
||
```go | ||
package main | ||
|
||
import "gofr.dev/pkg/gofr" | ||
|
||
func main(){ | ||
app := gofr.New() | ||
app.AddStaticFiles("public", "./public") | ||
app.Run() | ||
} | ||
``` | ||
|
||
In the above example, both endpoints `/public` and `/static` are available for the app to render the static content. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.