master v1.7
bel 2020-01-26 17:46:50 +00:00
parent f582410c40
commit d73cbe9e0c
4 changed files with 95 additions and 8 deletions

View File

@ -1,8 +1,11 @@
FROM frolvlad/alpine-glibc:alpine-3.9_glibc-2.29
RUN apk update && apk add --no-cache ca-certificates git
FROM golang:1.13-alpine as certs
RUN apk update && apk add --no-cache ca-certificates
FROM busybox:glibc
RUN mkdir -p /var/log
WORKDIR /main
COPY --from=certs /etc/ssl/certs /etc/ssl/certs
COPY . .
@ -10,3 +13,4 @@ ENV GOPATH=""
ENV MNT="/mnt/"
ENTRYPOINT ["/main/exec-notes-server"]
CMD []

49
server/gzipresponsewriter.go Executable file
View File

@ -0,0 +1,49 @@
package server
import (
"compress/gzip"
"fmt"
"net/http"
"strings"
)
type GZipResponseWriter struct {
w http.ResponseWriter
gz *gzip.Writer
}
func NewGZipResponseWriter(w http.ResponseWriter) *GZipResponseWriter {
w.Header().Add("Content-Type", "text/html")
w.Header().Add("Content-Encoding", "gzip")
return &GZipResponseWriter{
w: w,
gz: gzip.NewWriter(w),
}
}
func (gz *GZipResponseWriter) Header() http.Header {
return gz.w.Header()
}
func (gz *GZipResponseWriter) WriteHeader(statusCode int) {
gz.w.WriteHeader(statusCode)
}
func (gz *GZipResponseWriter) Write(b []byte) (int, error) {
return gz.gz.Write(b)
}
func (gz *GZipResponseWriter) Close() error {
return gz.gz.Close()
}
func (s *Server) gzip(h http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if encoding, _ := r.Header["Accept-Encoding"]; strings.Contains(fmt.Sprint(encoding), "gzip") {
gz := NewGZipResponseWriter(w)
defer gz.Close()
w = gz
}
h(w, r)
}
}

View File

@ -0,0 +1,34 @@
package server
import (
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
)
func TestGZipResponseWriter(t *testing.T) {
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
gz := NewGZipResponseWriter(w)
defer gz.Close()
fmt.Fprintln(gz, "Hello world")
}))
defer s.Close()
req := httptest.NewRequest("GET", s.URL, nil)
req.RequestURI = ""
resp, err := http.DefaultClient.Do(req)
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
t.Fatal(resp.StatusCode)
}
if b, err := ioutil.ReadAll(resp.Body); err != nil {
t.Fatal(err)
} else if s := string(b); s != "Hello world\n" {
t.Fatalf("%q", s)
}
}

View File

@ -18,27 +18,27 @@ func (s *Server) Routes() error {
},
{
path: fmt.Sprintf("notes/%s%s", wildcard, wildcard),
handler: s.authenticate(s.notes),
handler: s.gzip(s.authenticate(s.notes)),
},
{
path: fmt.Sprintf("edit/%s%s", wildcard, wildcard),
handler: s.authenticate(s.edit),
handler: s.gzip(s.authenticate(s.edit)),
},
{
path: fmt.Sprintf("delete/%s%s", wildcard, wildcard),
handler: s.authenticate(s.delete),
handler: s.gzip(s.authenticate(s.delete)),
},
{
path: fmt.Sprintf("submit/%s%s", wildcard, wildcard),
handler: s.authenticate(s.submit),
handler: s.gzip(s.authenticate(s.submit)),
},
{
path: fmt.Sprintf("create/%s%s", wildcard, wildcard),
handler: s.authenticate(s.create),
handler: s.gzip(s.authenticate(s.create)),
},
{
path: fmt.Sprintf("search"),
handler: s.authenticate(s.search),
handler: s.gzip(s.authenticate(s.search)),
},
}