2 Commits
v1.7 ... v1.8

Author SHA1 Message Date
bel
563eb7bb61 change textarea cursor 2020-01-26 18:41:12 +00:00
bel
b7f13bf33d extract gzip to own package 2020-01-26 18:08:20 +00:00
4 changed files with 17 additions and 84 deletions

View File

@@ -28,7 +28,7 @@ func editFile(p filetree.Path) string {
return fmt.Sprintf(` return fmt.Sprintf(`
<form action="/submit/%s" method="post" style="width:100%%; height: 90%%"> <form action="/submit/%s" method="post" style="width:100%%; height: 90%%">
<table style="width:100%%; height: 90%%"> <table style="width:100%%; height: 90%%">
<textarea name="content" style="width:100%%; min-height:90%%">%s</textarea> <textarea name="content" style="width:100%%; min-height:90%%; cursor:crosshair;">%s</textarea>
</table> </table>
<button type="submit">Submit</button> <button type="submit">Submit</button>
</form> </form>

View File

@@ -1,49 +0,0 @@
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

@@ -1,34 +0,0 @@
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

@@ -2,8 +2,10 @@ package server
import ( import (
"fmt" "fmt"
"local/gziphttp"
"local/router" "local/router"
"net/http" "net/http"
"path/filepath"
) )
func (s *Server) Routes() error { func (s *Server) Routes() error {
@@ -54,3 +56,17 @@ func (s *Server) root(w http.ResponseWriter, r *http.Request) {
r.URL.Path = "/notes" r.URL.Path = "/notes"
http.Redirect(w, r, r.URL.String(), http.StatusPermanentRedirect) http.Redirect(w, r, r.URL.String(), http.StatusPermanentRedirect)
} }
func (s *Server) gzip(h http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if gziphttp.Can(r) {
gz := gziphttp.New(w)
defer gz.Close()
w = gz
}
if filepath.Ext(r.URL.Path) == ".css" {
w.Header().Set("Content-Type", "text/css; charset=utf-8")
}
h(w, r)
}
}