Skip to content

Commit

Permalink
fix: Some Logic Error
Browse files Browse the repository at this point in the history
  • Loading branch information
takumimas committed Nov 22, 2024
1 parent 3f0a300 commit 474b624
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 3 deletions.
10 changes: 8 additions & 2 deletions gcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type StaticServerMiddlewareInterface interface {
}

// NewGCSStaticMiddleware initializes and returns a new instance of FilesStore with the provided GCSStaticConfig.
func (s *FilesStore) NewGCSStaticMiddleware(config GCSStaticConfig) StaticServerMiddlewareInterface {
func NewGCSStaticMiddleware(config GCSStaticConfig) StaticServerMiddlewareInterface {
return &FilesStore{
config: config,
}
Expand All @@ -48,6 +48,12 @@ func (s *FilesStore) ServerHeader(next echo.HandlerFunc) echo.HandlerFunc {
filePath := s.filePath(c)
file, contentType, err := s.getFile(filePath)
if err != nil {
if s.config.IsSPA {
IndexFile, IndexFileType, Err := s.getFile("index.html")
if Err == nil {
return c.Blob(http.StatusOK, IndexFileType, IndexFile)
}
}
return c.NoContent(http.StatusNotFound)
}

Expand All @@ -72,7 +78,7 @@ func (s *FilesStore) filePath(ctx echo.Context) string {
path = path + "/index.html"
}
if base == "." {
path = "/index.html"
path = "index.html"
}
}
return path
Expand Down
65 changes: 64 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,67 @@

---

This Echo middleware provides a static file store powered by Google Cloud Storage as its backend. In detail, it facilitates the efficient delivery of static files—such as HTML, CSS, JavaScript, or image files—stored in Google Cloud Storage directly to end users. By leveraging this middleware, you can host static content on GCS without embedding these files directly within your application code, enabling seamless access to the content via HTTP requests.
**This is a testing phase and not suitable for production use.**

This Echo middleware provides a static file store powered by Google Cloud Storage as its backend. In detail, it facilitates the efficient delivery of static files—such as HTML, CSS, JavaScript, or image files—stored in Google Cloud Storage directly to end users. By leveraging this middleware, you can host static content on GCS without embedding these files directly within your application code, enabling seamless access to the content via HTTP requests.

---

# Usage

## Installation
```
go get github.com/takumimas/echo-gcs-middleware
```

## Example

```go
func main() {
ctx := context.Background()

gcsClient, err := storage.NewClient(ctx)
if err != nil {
log.Fatalf("Failed to create GCS client: %v", err)
}
defer func() {
if err := gcsClient.Close(); err != nil {
log.Printf("Failed to close GCS client: %v", err)
}
}()

e := echo.New()

// Configure GCS middleware
gcsConfig := gcsmiddleware.GCSStaticConfig{
Client: gcsClient,
BucketName: "gcs-echo-test",
IgnorePath: nil, // Specify paths to ignore if any
IsSPA: true, // Set to true if serving a Single Page Application
RootPath: "/", // Set the root path
}

// Create the GCS middleware
gcsMiddleware := gcsmiddleware.NewGCSStaticMiddleware(gcsConfig)

// Register the middleware with Echo
e.Use(gcsMiddleware.ServerHeader)

port := ":1323"
log.Printf("Starting server on port %s...", port)
if err := e.Start(port); err != nil {
log.Fatalf("Failed to start the server: %v", err)
}
}
```

**IsSPA:**

When IsSPA is set to true, any 404 errors will automatically redirect to index.html. This is useful for Single Page Applications (SPAs) where routing is handled client-side and all non-static paths should serve the main entry point (index.html).

**RootPath:**

If you set RootPath to a specific value, such as /app/, it adjusts the base path from which files are served. For example, when RootPath is set to /app/, a request to /app/ will serve the file located at app/index.html.



0 comments on commit 474b624

Please sign in to comment.