From cf00c7a3fa5b5611df2a27ba196a5ac2ddbd5294 Mon Sep 17 00:00:00 2001 From: Yanislav Igonin Date: Tue, 11 Jan 2022 11:49:39 +0200 Subject: [PATCH] feat: add css renamer for production (#8) --- build/css-renamer.go | 104 +++++++++++++++++++++++++++++++++++++++++++ main.go | 5 +++ 2 files changed, 109 insertions(+) create mode 100644 build/css-renamer.go diff --git a/build/css-renamer.go b/build/css-renamer.go new file mode 100644 index 0000000..d13c9e1 --- /dev/null +++ b/build/css-renamer.go @@ -0,0 +1,104 @@ +package build + +import ( + "io/ioutil" + "math/rand" + "os" + "strings" +) + +// Returns all html templates paths from ../templates folder. +func getHtmlTemplatePaths() []string { + var paths []string + files, err := ioutil.ReadDir("templates") + if err != nil { + panic(err) + } + for _, file := range files { + paths = append(paths, "templates/"+file.Name()) + } + return paths +} + +// Returns all css files paths in ../static/styles folder. +func getCssFilesPaths() []string { + var paths []string + files, err := ioutil.ReadDir("static/styles") + if err != nil { + panic(err) + } + for _, file := range files { + paths = append(paths, "/static/styles/"+file.Name()) + } + return paths +} + +// Creates map of css file names to their new names. +func createCssMap() map[string]string { + var cssMap = make(map[string]string) + origPaths := getCssFilesPaths() + for _, origPath := range origPaths { + newPath := "/static/styles/" + randomString(10) + ".css" + cssMap[origPath] = newPath + } + + return cssMap +} + +// Renames file by paths. +func renameFile(oldPath, newPath string) { + // rename the file + err := os.Rename(removeFirstChar(oldPath), removeFirstChar(newPath)) + if err != nil { + panic(err) + } +} + +// Removes first character from string. +func removeFirstChar(s string) string { + return s[1:] +} + +// Generates a random string. +func randomString(length int) string { + // create a slice of characters to use + letters := []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") + + // create a new slice to hold the random string + b := make([]rune, length) + + // loop through the length of the string + for i := range b { + // get a random number + b[i] = letters[rand.Intn(len(letters))] + } + + // return the string + return string(b) +} + +// Renames css files to prevent caching old files on production. +func RenameCss() { + cssMap := createCssMap() + htmlTemplatePaths := getHtmlTemplatePaths() + + for origPath, newPath := range cssMap { + renameFile(origPath, newPath) + } + + for _, htmlTemplatePath := range htmlTemplatePaths { + htmlTemplate, err := ioutil.ReadFile(htmlTemplatePath) + if err != nil { + panic(err) + } + // walk through css map and replace all occurencies of css origName with newName + for origPath, newPath := range cssMap { + htmlTemplate = []byte(strings.Replace(string(htmlTemplate), origPath, newPath, -1)) + } + // write html template to file + err = ioutil.WriteFile(htmlTemplatePath, htmlTemplate, 0644) + if err != nil { + panic(err) + } + } +} diff --git a/main.go b/main.go index d943847..4e67f2f 100644 --- a/main.go +++ b/main.go @@ -12,6 +12,7 @@ import ( mgin "github.com/ulule/limiter/v3/drivers/middleware/gin" memory "github.com/ulule/limiter/v3/drivers/store/memory" + Build "micrach/build" Config "micrach/config" Controllers "micrach/controllers" Db "micrach/db" @@ -29,6 +30,10 @@ func main() { Repositories.Seed() } + if Config.App.Env == "release" { + Build.RenameCss() + } + err := Utils.CreateUploadsFolder() if err != nil { log.Panicln(err)