mirror of
https://github.com/yanislav-igonin/micrach
synced 2025-01-03 03:58:45 +03:00
feat: add css renamer for production (#8)
This commit is contained in:
parent
86bd536f42
commit
cf00c7a3fa
104
build/css-renamer.go
Normal file
104
build/css-renamer.go
Normal file
@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
5
main.go
5
main.go
@ -12,6 +12,7 @@ import (
|
|||||||
mgin "github.com/ulule/limiter/v3/drivers/middleware/gin"
|
mgin "github.com/ulule/limiter/v3/drivers/middleware/gin"
|
||||||
memory "github.com/ulule/limiter/v3/drivers/store/memory"
|
memory "github.com/ulule/limiter/v3/drivers/store/memory"
|
||||||
|
|
||||||
|
Build "micrach/build"
|
||||||
Config "micrach/config"
|
Config "micrach/config"
|
||||||
Controllers "micrach/controllers"
|
Controllers "micrach/controllers"
|
||||||
Db "micrach/db"
|
Db "micrach/db"
|
||||||
@ -29,6 +30,10 @@ func main() {
|
|||||||
Repositories.Seed()
|
Repositories.Seed()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if Config.App.Env == "release" {
|
||||||
|
Build.RenameCss()
|
||||||
|
}
|
||||||
|
|
||||||
err := Utils.CreateUploadsFolder()
|
err := Utils.CreateUploadsFolder()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panicln(err)
|
log.Panicln(err)
|
||||||
|
Loading…
Reference in New Issue
Block a user