Skip to content

Commit

Permalink
chore: split pkg for crypto, system
Browse files Browse the repository at this point in the history
  • Loading branch information
soulteary committed May 31, 2022
1 parent be45c0e commit a7b988d
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 43 deletions.
12 changes: 6 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ package main

import (
"bytes"
"context"
"crypto/md5" //#nosec
"context" //#nosec
"embed"
"encoding/json"
"fmt"
Expand All @@ -24,7 +23,9 @@ import (
"github.com/gin-contrib/gzip"
"github.com/gin-gonic/gin"
"github.com/soulteary/hosts-blackhole/internal/logger"
provider "github.com/soulteary/hosts-blackhole/pkg/provider"
"github.com/soulteary/hosts-blackhole/pkg/crypto"
"github.com/soulteary/hosts-blackhole/pkg/provider"
"github.com/soulteary/hosts-blackhole/pkg/system"
flag "github.com/spf13/pflag"
)

Expand Down Expand Up @@ -176,7 +177,7 @@ func main() {
if err != nil {
log.Fatal("程序无法创建缓存数据: ", err)
}
provider.ManualGC()
system.ManualGC()
}

c.Redirect(302, ROUTE_LIST)
Expand Down Expand Up @@ -248,8 +249,7 @@ func main() {
// https://github.com/gin-gonic/gin/issues/1222
func optimizeResourceCacheTime() gin.HandlerFunc {
data := []byte(time.Now().String())
/* #nosec */
etag := fmt.Sprintf("W/%x", md5.Sum(data))
etag := crypto.ETag(data)
return func(c *gin.Context) {
if strings.HasPrefix(c.Request.RequestURI, ROUTE_HOMEPAGE) ||
strings.HasPrefix(c.Request.RequestURI, ROUTE_FAVICON) {
Expand Down
18 changes: 18 additions & 0 deletions pkg/crypto/md5.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package crypto

import (
"crypto/md5" //#nosec
"fmt"
)

func Md5(str string) string {
data := []byte(str)
/* #nosec */
has := md5.Sum(data)
return fmt.Sprintf("%x", has)
}

func ETag(data []byte) string {
/* #nosec */
return fmt.Sprintf("W/%x", md5.Sum(data))
}
40 changes: 3 additions & 37 deletions pkg/provider/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,16 @@ package provider

import (
"bufio"
"crypto/md5" //#nosec
"fmt"
"log"
"os"
"path/filepath"
"runtime"
"runtime/debug"
"sort"
"strconv"
"strings"
"time"

"github.com/soulteary/hosts-blackhole/internal/logger"
"github.com/soulteary/hosts-blackhole/pkg/crypto"
)

type Lines struct {
Expand All @@ -38,13 +35,6 @@ const baseDir = "./"
var cacheKey = ""
var cacheHash = ""

func calcMd5(str string) string {
data := []byte(str)
/* #nosec */
has := md5.Sum(data)
return fmt.Sprintf("%x", has)
}

func CacheHash(files []string, update bool) (string, string) {
sort.Strings(files)
key := ""
Expand All @@ -57,8 +47,8 @@ func CacheHash(files []string, update bool) (string, string) {
}
}

key = calcMd5(key)
val = calcMd5(val)
key = crypto.Md5(key)
val = crypto.Md5(val)

if update {
cacheKey = key
Expand Down Expand Up @@ -193,27 +183,3 @@ func Purge(files []string) (mixed []string, success bool) {

return mixed, true
}

func ManualGC() {
log := logger.GetLogger()

log.Info("Runtime Information:")

runtime.GC()
debug.FreeOSMemory()

var m runtime.MemStats
runtime.ReadMemStats(&m)

log.Infof(" MEM Alloc = %10v MB", toMB(m.Alloc))
log.Infof(" MEM HeapAlloc = %10v MB", toMB(m.HeapAlloc))
log.Infof(" MEM Sys = %10v MB", toMB(m.Sys))
log.Infof(" MEM NumGC = %10v", m.NumGC)
log.Infof(" RUN NumCPU = %10d", runtime.NumCPU())
log.Infof(" RUN NumGoroutine = %10d", runtime.NumGoroutine())
}

func toMB(b uint64) uint64 {
const bytesInKB = 1024
return b / bytesInKB / bytesInKB
}
32 changes: 32 additions & 0 deletions pkg/system/gc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package system

import (
"runtime"
"runtime/debug"

"github.com/soulteary/hosts-blackhole/internal/logger"
)

func ManualGC() {
log := logger.GetLogger()

log.Info("Runtime Information:")

runtime.GC()
debug.FreeOSMemory()

var m runtime.MemStats
runtime.ReadMemStats(&m)

log.Infof(" MEM Alloc = %10v MB", toMB(m.Alloc))
log.Infof(" MEM HeapAlloc = %10v MB", toMB(m.HeapAlloc))
log.Infof(" MEM Sys = %10v MB", toMB(m.Sys))
log.Infof(" MEM NumGC = %10v", m.NumGC)
log.Infof(" RUN NumCPU = %10d", runtime.NumCPU())
log.Infof(" RUN NumGoroutine = %10d", runtime.NumGoroutine())
}

func toMB(b uint64) uint64 {
const bytesInKB = 1024
return b / bytesInKB / bytesInKB
}

0 comments on commit a7b988d

Please sign in to comment.