static

package module
v0.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 6, 2024 License: MIT Imports: 8 Imported by: 4

README

Gin Static Middleware

Run Tests codecov Go Report Card GoDoc

No Security Error, No uncovered function, No obsolete code.

  • Supercharged static middleware: seamlessly support local files and embedded filesystem for an unparalleled lightning-fast experience.
  • Break free from historical burdens and embrace the new era of Go 1.22. With 100% code coverage, enjoy the perfect blend of quality and performance!

Quick Start

Download and Import

Download and install it:

go get github.com/soulteary/gin-static

Import it in your code:

import "github.com/soulteary/gin-static"

Example

See the example

Serve Local Files
package main

import (
	"log"

	static "github.com/soulteary/gin-static"
	"github.com/gin-gonic/gin"
)

func main() {
	r := gin.Default()

	// if Allow DirectoryIndex
	// r.Use(static.Serve("/", static.LocalFile("./public", true)))
	// set prefix
	// r.Use(static.Serve("/static", static.LocalFile("./public", true)))

	r.Use(static.Serve("/", static.LocalFile("./public", false)))

	r.GET("/ping", func(c *gin.Context) {
		c.String(200, "test")
	})

	// Listen and Server in 0.0.0.0:8080
	if err := r.Run(":8080"); err != nil {
		log.Fatal(err)
	}
}
Serve Embed folder
package main

import (
	"embed"
	"fmt"
	"net/http"

	"github.com/gin-gonic/gin"
)

//go:embed public
var EmbedFS embed.FS

func main() {
	r := gin.Default()

	// method 1: use as Gin Router
	// trim embedfs path `public/page`, and use it as url path `/`
	// r.GET("/", static.ServeEmbed("public/page", EmbedFS))

	// method 2: use as middleware
	// trim embedfs path `public/page`, the embedfs path start with `/`
	// r.Use(static.ServeEmbed("public/page", EmbedFS))

	// method 2.1: use as middleware
	// trim embedfs path `public/page`, the embedfs path start with `/public/page`
	// r.Use(static.ServeEmbed("", EmbedFS))

	// method 3: use as manual
	// trim embedfs path `public/page`, the embedfs path start with `/public/page`
	// staticFiles, err := static.EmbedFolder(EmbedFS, "public/page")
	// if err != nil {
	// 	log.Fatalln("initialization of embed folder failed:", err)
	// } else {
	// 	r.Use(static.Serve("/", staticFiles))
	// }

	r.GET("/ping", func(c *gin.Context) {
		c.String(200, "test")
	})

	r.NoRoute(func(c *gin.Context) {
		fmt.Printf("%s doesn't exists, redirect on /\n", c.Request.URL.Path)
		c.Redirect(http.StatusMovedPermanently, "/")
	})

	// Listen and Server in 0.0.0.0:8080
	r.Run(":8080")
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LocalFile

func LocalFile(root string, indexes bool) *localFileSystem

func Serve

func Serve(urlPrefix string, fs ServeFileSystem) gin.HandlerFunc

Serve returns a middleware handler that serves static files in the given directory.

func ServeCached added in v0.1.0

func ServeCached(urlPrefix string, fs ServeFileSystem, cacheAge uint) gin.HandlerFunc

ServeCached returns a middleware handler that similar as Serve but with the Cache-Control Header set as passed in the cacheAge parameter

func ServeEmbed added in v0.0.5

func ServeEmbed(reqPath string, fsEmbed embed.FS) gin.HandlerFunc

func ServeRoot

func ServeRoot(urlPrefix, root string) gin.HandlerFunc

Types

type ServeFileSystem

type ServeFileSystem interface {
	http.FileSystem
	Exists(prefix string, path string) bool
}

func EmbedFolder

func EmbedFolder(fsEmbed embed.FS, reqPath string) (ServeFileSystem, error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL