-
Notifications
You must be signed in to change notification settings - Fork 0
/
serve_static_files.go
65 lines (52 loc) · 1.61 KB
/
serve_static_files.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// generated code - do not edit
package static
import (
"embed"
"fmt"
"io/fs"
"net/http"
"os"
// this package contains ...
"github.com/fullstack-lang/gong/test"
"github.com/gin-contrib/cors"
"github.com/gin-contrib/static"
"github.com/gin-gonic/gin"
)
func ServeStaticFiles(logGINFlag bool) (r *gin.Engine) {
// setup controlers
if !logGINFlag {
myfile, _ := os.Create("/tmp/server.log")
gin.DefaultWriter = myfile
}
r = gin.Default()
config := cors.DefaultConfig()
config.AllowOrigins = []string{"http://localhost:8080", "http://localhost:4200"} // Allow requests from localhost:8080 and localhost:4200
config.AllowMethods = []string{"GET", "POST", "PUT", "DELETE"} // Allow specific HTTP methods
config.AllowHeaders = []string{"Origin", "Content-Type", "Authorization"} // Allow specific headers
r.Use(cors.New(config))
// insertion point for serving the static file
// provide the static route for the angular pages
r.Use(static.Serve("/", EmbedFolder(test.NgDistNg, "ng-github.com-fullstack-lang-gong-test/dist/ng-github.com-fullstack-lang-gong-test/browser")))
r.NoRoute(func(c *gin.Context) {
fmt.Println(c.Request.URL.Path, "doesn't exists, redirect on /")
c.Redirect(http.StatusMovedPermanently, "/")
c.Abort()
})
return
}
type embedFileSystem struct {
http.FileSystem
}
func (e embedFileSystem) Exists(prefix string, path string) bool {
_, err := e.Open(path)
return err == nil
}
func EmbedFolder(fsEmbed embed.FS, targetPath string) static.ServeFileSystem {
fsys, err := fs.Sub(fsEmbed, targetPath)
if err != nil {
panic(err)
}
return embedFileSystem{
FileSystem: http.FS(fsys),
}
}