static

package
v0.0.0-...-1a1ce70 Latest Latest
Warning

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

Go to latest
Published: Mar 29, 2024 License: MIT, MIT Imports: 8 Imported by: 0

README

Gin Static Middleware

Static middleware, support both local files and embed filesystem.

Copied from gin-contrib/static and soulteary/gin-static

Quick Start

Download and Import

Download and install it:

go get gitverse.ru/andoma/gin-contrib/static

Import it in your code:

import "gitverse.ru/andoma/gin-contrib/static"

Example

See the example

Serve Local Files
package main

import (
	"log"

	"gitverse.ru/andoma/gin-contrib/static"
	"gitverse.ru/andoma/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"

	"gitverse.ru/andoma/gin-contrib/static"
	"gitverse.ru/andoma/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

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

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

func ServeRoot

func ServeRoot(urlPrefix, root string) gin.HandlerFunc

Types

type Override

type Override func(*gin.Context) bool

type ServeFileSystem

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

func EmbedFolder

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

Jump to

Keyboard shortcuts

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