Skip to content

Commit

Permalink
added memcache layer to the webserver.
Browse files Browse the repository at this point in the history
also includes a refactoring of the web server code, splitting into
multiple go files. also the way the gzip handling is done has changed,
with Writer implementations for gzip and cache handling, which end up
being passed to the handlers.

Fixes #43.
  • Loading branch information
rochaporto committed Feb 5, 2015
1 parent 7cfcba5 commit 024f2e4
Show file tree
Hide file tree
Showing 10 changed files with 503 additions and 153 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ language: go
go:
- tip
sudo: false
services:
- memcached
before_install:
- go get -v github.com/golang/lint/golint
- go get github.com/mattn/goveralls
Expand Down
5 changes: 3 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,9 @@ type SoaringWeb struct {

// Web holds all config information for the web server.
type Web struct {
Port int
Static string
Port int
Static string
Memcache string
}

// Welt2000 holds all config information for the welt2000 plugin.
Expand Down
3 changes: 3 additions & 0 deletions ezgliding.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ port=8000
# location of static files (html, css, js, ...)
#static=web/static

# memcached server location (when set caching gets enabled)
memcache=localhost:11211

[fusiontables]
# key for the fusion tables REST queries.
# Check https://developers.google.com/fusiontables/docs/v1/using#auth for details.
Expand Down
79 changes: 79 additions & 0 deletions web/cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Copyright 2014 The ezgliding Authors.
//
// This file is part of ezgliding.
//
// ezgliding is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// ezgliding is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with ezgliding. If not, see <http://www.gnu.org/licenses/>.
//
// Author: Ricardo Rocha <rocha.porto@gmail.com>

// Package web provides a web server implementation, serving ezgliding data.
package web

import (
"bytes"
"net/http"
"strings"

"github.com/bradfitz/gomemcache/memcache"
)

const (
// DefaultExpiration is the default item expiration time (in seconds)
DefaultExpiration = 120
)

// CacheItem holds an Item to be stored in the cache.
type CacheItem struct {
s *bytes.Buffer
hs []http.Header
}

// CacheResponseWriter holds a bytes buffer (writer) and a http ResponseWriter.
type CacheResponseWriter struct {
http.ResponseWriter
b *bytes.Buffer
m *memcache.Client
r *http.Request
}

// NewCacheResponseWriter instantiates a new instance.
func NewCacheResponseWriter(w http.ResponseWriter, m *memcache.Client,
r *http.Request) *CacheResponseWriter {
b := new(bytes.Buffer)
return &CacheResponseWriter{
ResponseWriter: w, b: b, m: m, r: r,
}
}

// Writer is the io.Writer implementation.
func (cw CacheResponseWriter) Write(b []byte) (int, error) {
cw.b.Write(b)
return cw.ResponseWriter.Write(b)
}

// Close flushes the written buffer to memcache.
func (cw CacheResponseWriter) Close() error {
// TODO(ricardo): as Close() is called in defer, might be a good idea to log here
// but need to find a way to test failure
return cw.m.Set(&memcache.Item{
Key: cw.requestKey(cw.r), Value: cw.b.Bytes(),
Flags: 0, Expiration: DefaultExpiration,
})
}

// requestKey returns a string with the memcache key for the given request.
func (cw CacheResponseWriter) requestKey(r *http.Request) string {
return r.URL.String() + strings.ToLower(r.Header.Get("Accept")) + strings.ToLower(r.Header.Get("Accept-Encoding"))

}
143 changes: 143 additions & 0 deletions web/cache_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
// Copyright 2014 The ezgliding Authors.
//
// This file is part of ezgliding.
//
// ezgliding is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// ezgliding is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with ezgliding. If not, see <http://www.gnu.org/licenses/>.
//
// Author: Ricardo Rocha <rocha.porto@gmail.com>

package web

import (
"encoding/json"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
"time"

"github.com/bradfitz/gomemcache/memcache"
"github.com/rochaporto/ezgliding/common"
"github.com/rochaporto/ezgliding/context"
"github.com/rochaporto/ezgliding/mock"
"github.com/rochaporto/ezgliding/util"
)

func TestMemcacheBadServer(t *testing.T) {
srv := Server{}
ctx := context.Context{}
ctx.Config.Web.Memcache = "nonexisting:10000"
ctx.Config.Web.Static = "/tmp"
err := srv.Init(ctx)
if err == nil {
t.Errorf("expected error got success")
return
} else if err != memcache.ErrNoServers {
t.Errorf("unexpected error type :: %v", err)
return
}
}

func TestMemcache(t *testing.T) {
airfields := []common.Airfield{
common.Airfield{ID: "MockID", ShortName: "MockShortName", Name: "MockName",
Region: "FR", ICAO: "AAAA", Flags: 0, Catalog: 11, Length: 1000, Elevation: 2000,
Runway: "32R", Frequency: 123.45, Latitude: 32.533, Longitude: 100.376},
}
// get the expected geojson to compare at the end
geojson, _ := util.Struct2GeoJSON([]interface{}{
airfields[0],
})
expected, _ := json.MarshalIndent(geojson, "", "\t")

// using a mock object to return airfields
mock := &mock.Mock{
GetAirfieldF: func(regions []string, updatedSince time.Time) ([]common.Airfield, error) {
return airfields, nil
},
}

// use a local memcached server
ctx := context.Context{Airfield: mock, Waypoint: mock}
ctx.Config.Web.Static = "/tmp"
ctx.Config.Web.Memcache = "localhost:11211"
srv := Server{}
err := srv.Init(ctx)
if err != nil {
t.Errorf("failed to init server with memcache :: %v", err)
return
}

resp := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/airfield/", nil)
req.Header.Set("Accept", "application/json")
srv.mux.ServeHTTP(resp, req)
r, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Errorf("failed to read response :: %v", err)
return
}

// first we compare the reply with the expected json
if string(r) != string(expected) {
t.Errorf("reply expected %v but got %v", string(expected), string(r))
return
}

// then we compare the value in memcache with the expected json
mclient := memcache.New(ctx.Config.Web.Memcache)
a, err := mclient.Get(NewCacheResponseWriter(nil, nil, nil).requestKey(req))
if err != nil {
t.Errorf("failed to contact memcache :: %v", err)
return
}
if string(a.Value) != string(expected) {
t.Errorf("memcache expected %v but got %v", string(expected), string(a.Value))
return
}
}

type RequestKeyTest struct {
h map[string][]string
u string
r string
}

var requestKeyTests = []RequestKeyTest{
{
map[string][]string{},
"/airfield/",
"/airfield/",
},
{
map[string][]string{
"Accept": []string{"application/json"},
},
"/airfield/",
"/airfield/application/json",
},
}

func TestRequestKey(t *testing.T) {
c := NewCacheResponseWriter(nil, nil, nil)
for _, test := range requestKeyTests {
req, _ := http.NewRequest("GET", test.u, nil)
req.Header = http.Header(test.h)
r := c.requestKey(req)
if r != test.r {
t.Errorf("expected %v but got %v", test.r, r)
continue
}
}
}
52 changes: 52 additions & 0 deletions web/gzip.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright 2014 The ezgliding Authors.
//
// This file is part of ezgliding.
//
// ezgliding is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// ezgliding is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with ezgliding. If not, see <http://www.gnu.org/licenses/>.
//
// Author: Ricardo Rocha <rocha.porto@gmail.com>

package web

import (
"compress/gzip"
"io"
"net/http"
)

// GzipResponseWriter gzips the data passed to Writer.
type GzipResponseWriter struct {
io.Writer
http.ResponseWriter
gz *gzip.Writer
}

// NewGzipResponseWriter creates a new Writer, which gzips data passed to h.
func NewGzipResponseWriter(w http.ResponseWriter) *GzipResponseWriter {
w.Header().Set("Content-Encoding", "gzip")
gz := gzip.NewWriter(w)
return &GzipResponseWriter{
Writer: gz, ResponseWriter: w, gz: gz,
}
}

// Write writes the given bytes (io.Writer).
func (gw GzipResponseWriter) Write(b []byte) (int, error) {
return gw.Writer.Write(b)
}

// Close closes the gzip writer.
func (gw GzipResponseWriter) Close() error {
return gw.gz.Close()
}
Loading

0 comments on commit 024f2e4

Please sign in to comment.