frontend

package
v1.14.0 Latest Latest
Warning

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

Go to latest
Published: Jun 22, 2025 License: MIT Imports: 8 Imported by: 0

README

frontend

frontend is a library for serving static files in a Gin web application. It supports local static files and embedding static files in binary.


Example of use

Local static files

package main

import (
	"github.com/gin-gonic/gin"
	"github.com/go-dev-frame/sponge/pkg/gin/frontend"
)

func main() {
	r := gin.Default()
	f := frontend.New("dist",
		// frontend.With404ToHome(),
	)
	err := f.SetRouter(r)
	if err != nil {
		panic(err)
	}
	err = r.Run(":8080")
	panic(err)
}
Embedding static files in binary
package main

import (
	"embed"
	"github.com/gin-gonic/gin"
	"github.com/go-dev-frame/sponge/pkg/gin/frontend"
)

//go:embed dist
var staticFS embed.FS

func main() {
	r := gin.Default()
	f := frontend.New("dist",
		frontend.WithEmbedFS(staticFS),
		//frontend.WithHandleContent(func(content []byte) []byte {
		//	return bytes.ReplaceAll(content, []byte("http://localhost:24631/api/v1"), []byte("http://192.168.3.37:24631/api/v1"))
		//}, "appConfig.js"),
		// frontend.With404ToHome(),
	)
	err := f.SetRouter(r)
	if err != nil {
		panic(err)
	}
	err = r.Run(":8080")
	panic(err)
}

Documentation

Overview

Package frontend is a library for serving static files in a Gin web application. It supports local static files and embedding static files in binary.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FrontEnd

type FrontEnd struct {
	// contains filtered or unexported fields
}

FrontEnd is the frontend router configuration

func New

func New(sourceDir string, opts ...Option) *FrontEnd

New create a new frontend, default use local static file, you can use WithEmbedFS to use embed.FS.

func (*FrontEnd) SetRouter

func (f *FrontEnd) SetRouter(r *gin.Engine) error

SetRouter set frontend router

type Option added in v1.12.8

type Option func(*options)

Option set the jwt options.

func With404ToHome added in v1.12.8

func With404ToHome() Option

With404ToHome set 404 to home page

func WithEmbedFS added in v1.12.8

func WithEmbedFS(efs embed.FS) Option

WithEmbedFS set embedFS to use embed.FS static resources.

func WithHandleContent added in v1.12.8

func WithHandleContent(fn func(content []byte) []byte, files ...string) Option

WithHandleContent set function to handle content and specified files

Jump to

Keyboard shortcuts

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