gin middlewares, just like nginx try-file function

Gin Middleware TryFile

This project is to solve the problem that the gin framework processes the dynamic routing file in the front-end compilation file and imitates nginx try_file function

本项目是解决gin框架处理前端编译文件中动态路由文件,模仿nginx try_file功能

location /images/ {
    root /opt/html/;
    try_files $uri   index.html; 
}

Usage

func RegisterTryFile(router *gin.Engine, hfs http.FileSystem, staticFileDir ...string)

Parameter analysis

  • router *gin.Engine
  • hfs http.FileSystem
    • http.FS
    • http.Dir
    • http.FS(embed.FS)
  • staticFileDir ...string
    • only one word as default,which means gin router and static path
    • include two word,first is gin router,second is static path, default try file is index.html
    • include three word,first is gin router,second is static path,three is try file name eg: index.html

Demo

package main

import (
	"embed"
	"net/http"

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

//go:embed dist
var distFile embed.FS

func main() {
	r := gin.Default()
	// As Default, Gin Router equal StaticPath
	tryfile.RegisterTryFile(r, http.FS(distFile), "/dist")

	// Custom Gin Router and StaticPath
	// tryfile.RegisterTryFile(r, http.FS(distFile), "/static", "/dist")

	// Fully custom parameters
	// tryfile.RegisterTryFile(r, http.FS(distFile), "/static", "/dist/custom", "index.html")

	// As you wish
	r.Any("/try/file/*any", tryfile.WrapHandler("/dist", http.FS(distFile)))
	r.Run()
}

Verification results

➜  gin-tryfile git:(main) ✗ curl http://127.0.0.1:8080/dist/this/is/not/exist
<html>
    <body>
        <h3>Hello Gin-TryFile</h3>
    </body>
</html>
➜  gin-tryfile git:(main) ✗ curl http://127.0.0.1:8080/try/file/this/is/not/exist
<html>
    <body>
        <h3>Hello Gin-TryFile</h3>
    </body>
</html>
Owner
Full-Stack Developer/Senior operation and maintenance Development Engineer
null
Similar Resources

REST API written in GO with PostgreSQL and Nginx Proxy + Certbot Let's Encrypt HTTPS certificates + Graphical Frontend in PHP. Deployed via docker-compose.

SSOA-PT REST APP Services Backend: REST API in Go Database: PostgreSQL Web Proxy: Nginx Let's Encrypt HTTPS certificates with certbot Frontend: PHP Ap

Mar 19, 2022

GoCondor is a golang web framework with an MVC like architecture, it's based on Gin framework

GoCondor is a golang web framework with an MVC like architecture, it's based on Gin framework

GoCondor is a golang web framework with an MVC like architecture, it's based on Gin framework, it features a simple organized directory structure for your next project with a pleasant development experience, made for developing modern APIs and microservices.

Dec 29, 2022

Goshort - Just another simple url shortener to use with curl

qnd.be Just another simple url shortener to use with curl. Kinda like 0x0.st. Ju

Jan 29, 2022

RSS master is a RSS subscription function aggregation tool, You can use it easily!

🇨🇳 中文 rsm ✨ RSS master[rsm] is a RSS subscription function aggregation tool, You can use it easily! How to start? 🚁 Start rsm with rsm run -c,--cfg

Oct 18, 2022

:construction: Closed. A website and user system (Gin/Backbone).

A website and user system starter. Implemented with gin and Backbone. Gowall is port of Drywall Go Node.js Repository here Drywall Site Gowall Drywall

Jul 6, 2020

Example golang using gin framework everything you need, i create this tutorial special for beginner.

Golang Gin Framework Fundamental Example golang using gin framework everything you need, i create this tutorial special for beginner. Feature Containe

Dec 16, 2022

gin api blog

Gin项目 介绍 以下是基于gin开发的项目接口,将持续更新,有兴趣请star,本项目包含mysql,redis,elasticsearch,mongo,rabbitmq,kafka,jaeger,单机限流,分布式限流,sentry, jwt,请求参数验证,发送邮件,图片上传,httpclient用

Oct 14, 2022

gin api的脚手架

基于gin的api脚手架 0、支持指定配置文件启动 1、不破坏gin的原有特性,基本不影响原有gin的运行速度 2、wire负责管理依赖注入 3、集成常用组件gorm/zap/viper/go-redis等 4、方便集成第三放组件 5、可扩展解耦方便,只需要替换data层数据源即可(biz负责定义r

Aug 12, 2022

前端使用Vue3、CompositionAPI、typescript、vite、element plus,后端使用Go + Gin + Gorm,实现的后端管理系统,支持菜单、页面按钮及后端API的权限管控。

前端使用Vue3、CompositionAPI、typescript、vite、element plus,后端使用Go + Gin + Gorm,实现的后端管理系统,支持菜单、页面按钮及后端API的权限管控。

本系统使用最新的 Vue3 及相关技术栈,实现了菜单及页面按钮的权限管控,基于 Casbin 实现了后端 API 接口的管控,不进行过度的封装,代码简洁易懂,方便二次开发及当成后端管理平台脚手架使用,数据库暂时仅支持 postgres 。 演示站点:http://fdevops.com:8088 文

Apr 1, 2022
Nada is a JS runtime, just like Nodejs. The difference is that Nada allows JS developers to easily achieve millions of concurrent applications.

Nada is a JS runtime, just like Nodejs. The difference is that Nada allows JS developers to easily achieve millions of concurrent applications. It also adds some new enhancements to THE JS syntax (types, interfaces, generics) that fundamentally address JS's perennial complaints.

Jul 11, 2022
Go (Golang) API REST with Gin FrameworkGo (Golang) API REST with Gin Framework

go-rest-api-aml-service Go (Golang) API REST with Gin Framework 1. Project Description Build REST APIs to support AML service with the support of exte

Nov 21, 2021
Gin-boilerplate - This repository contains boilerplate code of a REST service using Gin (golang) framework.

Boilerplate REST service using Gin web framework (golang) Introduction This repository contains a boilerplate REST API service using Gin web framework

Apr 28, 2022
Gin-easy-todo - golang 의 RESTful API 프레임워크 gin 을 활용해 아주 간단한 Todo 애플리케이션을 만들어보자.
Gin-easy-todo - golang 의 RESTful API 프레임워크 gin 을 활용해 아주 간단한 Todo 애플리케이션을 만들어보자.

목차 1. 요약 2. 목표 3. API 목록 4. 프로젝트 구조 5. 패키지 별 기능과 관계 6. 사전 작업 6.1. DB, Table 생성 6.2. 모듈 생성 6.3. 패키지 다운로드 7. Gin 작성 7.1. 데이터베이스 설정 7.2. 테이블, 스키마 정의 7.3.

Jan 2, 2022
Go-service-gin - Simple Web api application developed in Golang and Gin

Simple Web api application developed in Golang and Gin Initial Tutorial URL http

Jan 4, 2022
Go-gin-ddd-cqrs - Clean api rest with Go, Gin and GORM
Go-gin-ddd-cqrs - Clean api rest with Go, Gin and GORM

GOLANG API REST Clean api rest with Go, Gin and GORM. Clean Architecture with DD

Oct 21, 2022
Go-gin-mongo-api - A backend RESTful API built using golang, gin and mongoDB

go-gin-mongo-API This is a RESTful backend API which is developed using the gola

Jul 19, 2022
Gin-boilerplate - Gin boilerplate preconfigured with basic rest api features

Gin Boilerplate Build apis with gin faster with this template Features Validatio

Jun 24, 2022
This is a web project in golang, where we will try to use multiple concepts
This is a web project in golang, where we will try to use multiple concepts

This is a web project in golang, where we will try to use multiple concepts ##List of all the dependencies we have used in this project Name GoCommand

Feb 10, 2022
Compose sample application Go server with an Nginx proxy and a Postgres database

Compose sample application Go server with an Nginx proxy and a Postgres database Project

Apr 7, 2022