Skip to content

Commit

Permalink
refactor(code): improve code quality
Browse files Browse the repository at this point in the history
  • Loading branch information
cg33 committed Aug 14, 2020
1 parent 94d8d52 commit 9e14dc5
Show file tree
Hide file tree
Showing 30 changed files with 76 additions and 172 deletions.
3 changes: 3 additions & 0 deletions adm/compile_assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ var AssetPaths = map[string]string{
}`

err = ioutil.WriteFile(outputPath+"/assets_list.go", []byte(listContent), 0644)
if err != nil {
return
}
err = ioutil.WriteFile(outputPath+"/assets_path.go", []byte(pathsContent), 0644)
if err != nil {
return
Expand Down
24 changes: 11 additions & 13 deletions adm/minify.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,52 +22,48 @@ func cssMinifier(inputDir, outputFile string, hash bool) {
err := removeOutputFile(outputFile)
if err != nil {
log.Panicln("removeOutputFileError", err)
return
}

files, err := getInputFiles(inputDir)
if err != nil {
log.Panicln("getInputFilesError", err)
return
}

notMinifiedString, err := combineFiles(files, inputDir)
if err != nil {
log.Panicln("combineFilesError", err)
return
}

minifiedString, err := makeMini(notMinifiedString, "text/css")
if err != nil {
log.Panicln("doTheMinifyingError", err)
return
}

if hash && filepath.Ext(outputFile) == ".css" {
m5 := md5.New()
m5.Write([]byte(minifiedString))
_, err := m5.Write([]byte(minifiedString))
if err != nil {
log.Panicln("combineFilesError", err)
}
m5res := hex.EncodeToString(m5.Sum(nil))
outputFile = strings.ReplaceAll(outputFile, ".css", "."+m5res[len(m5res)-10:]+".css")
}

err = writeOutputFile(minifiedString, outputFile)
if err != nil {
log.Panicln("writeOutputFileError", err)
return
}
}

func jsMinifier(inputDir, outputFile string, hash bool) {
err := removeOutputFile(outputFile)
if err != nil {
log.Panicln("removeOutputFileError", err)
return
}

files, err := getInputFiles(inputDir)
if err != nil {
log.Panicln("getInputFilesError", err)
return
}

var b bytes.Buffer
Expand All @@ -81,7 +77,7 @@ func jsMinifier(inputDir, outputFile string, hash bool) {
filePath := inputDir + name
fileTxt, err := ioutil.ReadFile(filePath)
if err != nil {
return
checkError(err)
}

fmt.Println("file path", filePath)
Expand All @@ -91,26 +87,28 @@ func jsMinifier(inputDir, outputFile string, hash bool) {

minifiedString, err := m.Bytes("text/javascript", fileTxt)
if err != nil {
return
checkError(err)
}

_, err = b.Write(minifiedString)
if err != nil {
return
checkError(err)
}
}

if hash && filepath.Ext(outputFile) == ".js" {
m5 := md5.New()
m5.Write(b.Bytes())
_, err := m5.Write(b.Bytes())
if err != nil {
checkError(err)
}
m5res := hex.EncodeToString(m5.Sum(nil))
outputFile = strings.ReplaceAll(outputFile, ".js", "."+m5res[len(m5res)-10:]+".js")
}

err = writeOutputFile(b.String(), outputFile)
if err != nil {
checkError(err)
return
}
}

Expand Down
8 changes: 4 additions & 4 deletions adm/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,13 +268,13 @@ var Generators = map[string]table.Generator{
}

configByte, err := json.MarshalIndent(cfg, "", " ")
configByte = bytes.Replace(configByte, []byte(`
configByte = bytes.ReplaceAll(configByte, []byte(`
"logger": {
"encoder": {},
"rotate": {}
},`), []byte{}, -1)
configByte = bytes.Replace(configByte, []byte(`,
"animation": {}`), []byte{}, -1)
},`), []byte{})
configByte = bytes.ReplaceAll(configByte, []byte(`,
"animation": {}`), []byte{})
checkError(err)
checkError(ioutil.WriteFile("./config.json", configByte, 0644))

Expand Down
18 changes: 9 additions & 9 deletions adm/project_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func startServer() {
_ = r.Run(":{{.Port}}")
quit := make(chan os.Signal)
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt)
<-quit
log.Print("closing database connection")
Expand Down Expand Up @@ -115,7 +115,7 @@ func startServer() {
beego.BConfig.Listen.HTTPPort = {{.Port}}
go app.Run()
quit := make(chan os.Signal)
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt)
<-quit
log.Print("closing database connection")
Expand Down Expand Up @@ -179,7 +179,7 @@ func startServer() {
_ = bu.Serve()
}()
quit := make(chan os.Signal)
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt)
<-quit
log.Print("closing database connection")
Expand Down Expand Up @@ -244,7 +244,7 @@ func startServer() {
_ = http.ListenAndServe(":{{.Port}}", r)
}()
quit := make(chan os.Signal)
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt)
<-quit
log.Print("closing database connection")
Expand Down Expand Up @@ -323,7 +323,7 @@ func startServer() {
go e.Logger.Fatal(e.Start(":{{.Port}}"))
quit := make(chan os.Signal)
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt)
<-quit
log.Print("closing database connection")
Expand Down Expand Up @@ -384,7 +384,7 @@ func startServer() {
_ = fasthttp.ListenAndServe(":{{.Port}}", router.Handler)
}()
quit := make(chan os.Signal)
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt)
<-quit
log.Print("closing database connection")
Expand Down Expand Up @@ -443,7 +443,7 @@ func startServer() {
s.SetPort({{.Port}})
go s.Run()
quit := make(chan os.Signal)
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt)
<-quit
log.Print("closing database connection")
Expand Down Expand Up @@ -504,7 +504,7 @@ func startServer() {
_ = http.ListenAndServe(":{{.Port}}", app)
}()
quit := make(chan os.Signal)
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt)
<-quit
log.Print("closing database connection")
Expand Down Expand Up @@ -568,7 +568,7 @@ func startServer() {
_ = app.Run(iris.Addr(":{{.Port}}"))
}()
quit := make(chan os.Signal)
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt)
<-quit
log.Print("closing database connection")
Expand Down
2 changes: 2 additions & 0 deletions adm/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ func downloadTo(url, output string) {

file, err := os.Create(output)

checkError(err)

_, err = io.Copy(file, res.Body)

checkError(err)
Expand Down
9 changes: 0 additions & 9 deletions context/trie.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,6 @@ func (n *node) printChildren() {
}
}

func (n *node) printLeafChildren() {
if len(n.handle) > 0 {
n.print()
}
for _, child := range n.children {
child.printLeafChildren()
}
}

func stringToArr(path string) []string {
var (
paths = make([]string, 0)
Expand Down
2 changes: 1 addition & 1 deletion examples/beego/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func main() {
beego.BConfig.Listen.HTTPPort = 9087
go app.Run()

quit := make(chan os.Signal)
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt)
<-quit
log.Print("closing database connection")
Expand Down
2 changes: 1 addition & 1 deletion examples/buffalo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func main() {
_ = bu.Serve()
}()

quit := make(chan os.Signal)
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt)
<-quit
log.Print("closing database connection")
Expand Down
2 changes: 1 addition & 1 deletion examples/chi/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func main() {
_ = http.ListenAndServe(":3333", r)
}()

quit := make(chan os.Signal)
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt)
<-quit
log.Print("closing database connection")
Expand Down
2 changes: 1 addition & 1 deletion examples/echo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func main() {
// Start server
go e.Logger.Fatal(e.Start(":1323"))

quit := make(chan os.Signal)
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt)
<-quit
log.Print("closing database connection")
Expand Down
2 changes: 1 addition & 1 deletion examples/fasthttp/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func main() {
_ = fasthttp.ListenAndServe(":8897", router.Handler)
}()

quit := make(chan os.Signal)
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt)
<-quit
log.Print("closing database connection")
Expand Down
2 changes: 1 addition & 1 deletion examples/gf/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func main() {
s.SetPort(9033)
go s.Run()

quit := make(chan os.Signal)
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt)
<-quit
log.Print("closing database connection")
Expand Down
2 changes: 1 addition & 1 deletion examples/gin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func main() {
_ = r.Run(":9033")
}()

quit := make(chan os.Signal)
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt)
<-quit
log.Print("closing database connection")
Expand Down
2 changes: 1 addition & 1 deletion examples/gorilla/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func main() {
_ = http.ListenAndServe(":9033", app)
}()

quit := make(chan os.Signal)
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt)
<-quit
log.Print("closing database connection")
Expand Down
2 changes: 1 addition & 1 deletion examples/iris/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func main() {
_ = app.Run(iris.Addr(":8099"))
}()

quit := make(chan os.Signal)
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt)
<-quit
log.Print("closing database connection")
Expand Down
Loading

0 comments on commit 9e14dc5

Please sign in to comment.