Skip to content

Commit

Permalink
fix: mime type for javascript files
Browse files Browse the repository at this point in the history
  • Loading branch information
ItsNotGoodName committed Jan 25, 2022
1 parent 8eaa315 commit 0631a8e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 14 deletions.
15 changes: 15 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Launch Package",
"type": "go",
"request": "launch",
"mode": "debug",
"program": "${workspaceRoot}"
}
]
}
34 changes: 20 additions & 14 deletions router/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,38 @@ package router
import (
"io/fs"
"log"
"mime"
"net/http"

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

func init() {
mime.AddExtensionType(".js", "application/javascript")
}

// handleFS adds GET handlers for all files and folders using the given filesystem.
func handleFS(r *gin.Engine, fS fs.FS) {
httpFS := http.FS(fS)
dirHandler := handleDir(httpFS)

if files, err := fs.ReadDir(fS, "."); err == nil {
for _, f := range files {
name := f.Name()
if f.IsDir() {
r.GET("/"+name+"/*"+name, dirHandler)
} else if name == "index.html" {
indexHandler := handleIndex(httpFS)
r.GET("/", indexHandler)
r.GET("/index.html", indexHandler)
} else {
r.GET("/"+name, dirHandler)
}
}
} else {
files, err := fs.ReadDir(fS, ".")
if err != nil {
log.Fatal("router.handleFS:", err)
}

for _, f := range files {
name := f.Name()
if f.IsDir() {
r.GET("/"+name+"/*"+name, dirHandler)
} else if name == "index.html" {
indexHandler := handleIndex(httpFS)
r.GET("/", indexHandler)
r.GET("/index.html", indexHandler)
} else {
r.GET("/"+name, dirHandler)
}
}
}

func handleDir(httpFS http.FileSystem) gin.HandlerFunc {
Expand Down

0 comments on commit 0631a8e

Please sign in to comment.