Browse Source

MinifyHtmlCode, SessionStart

Volodymyr Tkach 2 years ago
parent
commit
34e1eddb2e
4 changed files with 65 additions and 0 deletions
  1. 1 0
      go.mod
  2. 2 0
      go.sum
  3. 30 0
      utils/http/helpers/helpers.go
  4. 32 0
      utils/http/helpers/helpers_test.go

+ 1 - 0
go.mod

@@ -5,6 +5,7 @@ go 1.18
 require (
 	github.com/onsi/ginkgo v1.16.5
 	github.com/onsi/gomega v1.19.0
+	github.com/vladimirok5959/golang-server-sessions v1.0.9
 )
 
 require (

+ 2 - 0
go.sum

@@ -31,6 +31,8 @@ github.com/onsi/gomega v1.19.0/go.mod h1:LY+I3pBVzYsTBU1AnDwOSxaYi9WoWiqgwooUqq9
 github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
 github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
 github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
+github.com/vladimirok5959/golang-server-sessions v1.0.9 h1:oGj3jfpIo5PtWojeGDexDtTmaaZtSY/vH2PcwZIqWNI=
+github.com/vladimirok5959/golang-server-sessions v1.0.9/go.mod h1:w0JRthleTg5D4lY32iczfuUfKCXpbgUiLXHhIUFyY4o=
 github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
 golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
 golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=

+ 30 - 0
utils/http/helpers/helpers.go

@@ -0,0 +1,30 @@
+package helpers
+
+import (
+	"errors"
+	"log"
+	"net/http"
+	"os"
+	"regexp"
+
+	"github.com/vladimirok5959/golang-server-sessions/session"
+)
+
+var mHtml = regexp.MustCompile(`>[\n\t\r\s]+<`)
+var mHtmlLeft = regexp.MustCompile(`>[\n\t\r\s]+`)
+var mHtmlRight = regexp.MustCompile(`[\n\t\r\s]+<`)
+
+func MinifyHtmlCode(str string) string {
+	str = mHtml.ReplaceAllString(str, "><")
+	str = mHtmlLeft.ReplaceAllString(str, ">")
+	str = mHtmlRight.ReplaceAllString(str, "<")
+	return str
+}
+
+func SessionStart(w http.ResponseWriter, r *http.Request) (*session.Session, error) {
+	sess, err := session.New(w, r, "/tmp")
+	if err != nil && !errors.Is(err, os.ErrNotExist) {
+		log.Printf("%s\n", err.Error())
+	}
+	return sess, err
+}

+ 32 - 0
utils/http/helpers/helpers_test.go

@@ -0,0 +1,32 @@
+package helpers_test
+
+import (
+	"testing"
+
+	. "github.com/onsi/ginkgo"
+	. "github.com/onsi/gomega"
+	"github.com/vladimirok5959/golang-utils/utils/http/helpers"
+)
+
+var _ = Describe("helpers", func() {
+	Context("MinifyHtmlCode", func() {
+		It("minify Html code", func() {
+			Expect(helpers.MinifyHtmlCode(`
+				<!doctype html>
+				<html lang="uk">
+					<head>
+						<meta charset="utf-8" />
+					</head>
+					<body>
+						Index
+					</body>
+				</html>
+			`)).To(Equal(`<!doctype html><html lang="uk"><head><meta charset="utf-8" /></head><body>Index</body></html>`))
+		})
+	})
+})
+
+func TestSuite(t *testing.T) {
+	RegisterFailHandler(Fail)
+	RunSpecs(t, "helpers")
+}