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

Added Static File Handler #674

Merged
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
807faf9
Added Static File Handler
KedarisettiSreeVamsi Jun 2, 2024
b449cb4
changes to filepath and gofr file
KedarisettiSreeVamsi Jun 2, 2024
7c136e4
Added documentation and removed the example with small edits
KedarisettiSreeVamsi Jun 3, 2024
749f34a
Merge branch 'development' into staticFileHandler
Umang01-hash Jun 4, 2024
25c7cc2
Made changes to Doc file and removed config line
KedarisettiSreeVamsi Jun 4, 2024
fb04cc6
Merge branch 'staticFileHandler' of https://github.com/KedarisettiSre…
KedarisettiSreeVamsi Jun 4, 2024
6e5a7be
typo fix
KedarisettiSreeVamsi Jun 4, 2024
380aada
Merge branch 'development' into staticFileHandler
Umang01-hash Jun 6, 2024
fbbb2c6
Added test for static handling
KedarisettiSreeVamsi Jun 6, 2024
f4ec872
Merge branch 'staticFileHandler' of https://github.com/KedarisettiSre…
KedarisettiSreeVamsi Jun 6, 2024
761e814
minor changes
KedarisettiSreeVamsi Jun 6, 2024
b7605e6
Changes
KedarisettiSreeVamsi Jun 6, 2024
7105889
Merge branch 'development' into staticFileHandler
Umang01-hash Jun 7, 2024
b7f7cbf
made changes
KedarisettiSreeVamsi Jun 7, 2024
04e00b9
Merge branch 'staticFileHandler' of https://github.com/KedarisettiSre…
KedarisettiSreeVamsi Jun 7, 2024
a2b06f9
code quality checks
KedarisettiSreeVamsi Jun 7, 2024
1d34bcc
rework code quality checks
KedarisettiSreeVamsi Jun 7, 2024
0115e03
code quality clearance
KedarisettiSreeVamsi Jun 7, 2024
070f02a
Merge branch 'development' into staticFileHandler
Umang01-hash Jun 10, 2024
aa5288f
resolve review comments
Umang01-hash Jun 10, 2024
fc6a232
Merge branch 'development' of github.com:gofr-dev/gofr into staticFil…
Umang01-hash Jun 10, 2024
7d97572
Merge branch 'development' into staticFileHandler
aryanmehrotra Jun 10, 2024
4c52caf
refactor tests
aryanmehrotra Jun 10, 2024
e75d5b5
fix docs, tests, logs
srijan-27 Jun 11, 2024
9d8ff4b
Merge branch 'development' into staticFileHandler
srijan-27 Jun 11, 2024
cec9b74
Merge branch 'development' into staticFileHandler
aryanmehrotra Jun 12, 2024
ca1122b
add config to static file handling
KedarisettiSreeVamsi Jun 12, 2024
58ccbc0
update of test
KedarisettiSreeVamsi Jun 12, 2024
105543f
fixing for index file handling
KedarisettiSreeVamsi Jun 12, 2024
438bfa3
updates to documentation and code quality fixes
KedarisettiSreeVamsi Jun 12, 2024
0a5fb78
changes and fixes
KedarisettiSreeVamsi Jun 13, 2024
824b1bc
small typo in documentation
KedarisettiSreeVamsi Jun 13, 2024
8d8af7a
checking for public endpoint
KedarisettiSreeVamsi Jun 13, 2024
492dbab
fix linters
aryanmehrotra Jun 17, 2024
d4ee4f0
Merge branch 'development' into staticFileHandler
vipul-rawat Jun 19, 2024
8f0dd2c
changes as requested
KedarisettiSreeVamsi Jun 24, 2024
5aa6105
conflict check
KedarisettiSreeVamsi Jun 24, 2024
cd82021
resolve merge conflicts
aryanmehrotra Jun 25, 2024
e6bbd59
fix reveiw comments
aryanmehrotra Jun 25, 2024
9b9a6ba
fix issues in rendering file
aryanmehrotra Jun 25, 2024
39597ee
remove unwanted exported methods
aryanmehrotra Jun 25, 2024
d92b6f1
refactor implementatins
aryanmehrotra Jun 25, 2024
b25c4b6
unexport static file config function
aryanmehrotra Jun 25, 2024
886e3ef
remove unwanted files
aryanmehrotra Jun 25, 2024
aa6d989
Merge branch 'development' into staticFileHandler
aryanmehrotra Jun 26, 2024
8054eec
add test for invalid file and custom route
aryanmehrotra Jun 26, 2024
f725bc4
update invalid filepath test
aryanmehrotra Jun 26, 2024
d8be25c
add test and remove unwanted code
aryanmehrotra Jun 26, 2024
a45db0f
resolve linters
aryanmehrotra Jun 26, 2024
683952f
fix test
aryanmehrotra Jun 26, 2024
f2d186e
keep inline
aryanmehrotra Jun 26, 2024
bee9a39
Merge branch 'development' into staticFileHandler
aryanmehrotra Jun 26, 2024
fc4ffe3
fix reveiw comments
aryanmehrotra Jun 26, 2024
ec4be3f
Merge branch 'development' into staticFileHandler
aryanmehrotra Jun 26, 2024
c30629e
Merge branch 'development' into staticFileHandler
srijan-27 Jun 26, 2024
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,6 @@ vendor/
# IDE Cache
.vscode
.DS_Store

# Public Folder
pkg/gofr/public
srijan-27 marked this conversation as resolved.
Show resolved Hide resolved
76 changes: 76 additions & 0 deletions docs/advanced-guide/serving-static-files/page.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# Serving Static Files using GoFr

Often, we require to serve static content be it a default profile image or a static website. We want to have a mechanism to serve those content without having a hassel of implementing it from scratch.
KedarisettiSreeVamsi marked this conversation as resolved.
Show resolved Hide resolved

GoFr provides a default mechanism where if a public folder is available in the directory of the application, it automatically provides an endpoint with `/public/<filename>`, here filename refers to the file we want to get static content to be served.

Example Project folder utilizing public endpoint:

```
srijan-27 marked this conversation as resolved.
Show resolved Hide resolved
project_folder
|
|---config
| .env
|---public
| <img1>.jpeg
KedarisettiSreeVamsi marked this conversation as resolved.
Show resolved Hide resolved
| <img2>.png
| <img3>.jpeg
KedarisettiSreeVamsi marked this conversation as resolved.
Show resolved Hide resolved
| 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.

Providing an example below along with File System Example:

```
project_folder
|
|---config
| .env
|---public
| <img1>.jpeg
| <img2>.png
| <img3>.jpeg
|---static
| |---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("static","./static")
app.Run()
}

```

In the above example, both endpoints `/public` and `/static` are available for the app to render the static content.
28 changes: 28 additions & 0 deletions pkg/gofr/gofr.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -94,9 +95,36 @@ func New() *App {

app.subscriptionManager = newSubscriptionManager(app.container)

// static fileserver
currentWd, _ := os.Getwd()
checkDirectory := filepath.Join(currentWd, "public")
Umang01-hash marked this conversation as resolved.
Show resolved Hide resolved

if _, err := os.Stat(checkDirectory); !os.IsNotExist(err) {
Umang01-hash marked this conversation as resolved.
Show resolved Hide resolved
app.AddStaticFiles("public", checkDirectory)
srijan-27 marked this conversation as resolved.
Show resolved Hide resolved
}

return app
}

func (a *App) AddStaticFiles(endpoint, filePath string) {
a.httpRegistered = true
dupFilePath := ""
if strings.HasPrefix(filePath, "./") {
dupFilePath, _ = os.Getwd()
dupFilePath = filepath.Join(dupFilePath, filePath)
} else {
dupFilePath = filePath
KedarisettiSreeVamsi marked this conversation as resolved.
Show resolved Hide resolved
}

endpoint = "/" + strings.TrimPrefix(endpoint, "/")

if _, err := os.Stat(dupFilePath); err != nil {
a.container.Logger.Errorf("Couldn't register %s static endpoint", endpoint)
return
}
a.httpServer.router.AddStaticFiles(endpoint, dupFilePath)
}

// NewCMD creates a command-line application.
func NewCMD() *App {
app := &App{}
Expand Down
Loading