Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into rendering-experimen…
Browse files Browse the repository at this point in the history
…t-squash
  • Loading branch information
nielsole committed Jul 23, 2023
2 parents b6c4ca5 + dfcfe50 commit 8a3a6e5
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 22 deletions.
16 changes: 8 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func readInt(file *os.File) (uint32, error) {
return binary.LittleEndian.Uint32(b), nil
}

func readPngTile(metatile_path string, metatile_offset uint32) (*os.File, *io.SectionReader, error) {
func readTile(metatile_path string, metatile_offset uint32) (*os.File, *io.SectionReader, error) {
file, err := os.Open(metatile_path)
if err != nil {
return nil, nil, err
Expand All @@ -93,8 +93,8 @@ func readPngTile(metatile_path string, metatile_offset uint32) (*os.File, *io.Se
return file, io.NewSectionReader(file, int64(tile_offset), int64(tile_length)), nil
}

func writeTileResponse(writer http.ResponseWriter, req *http.Request, metatile_path string, metatile_offset uint32, modTime time.Time) error {
file, pngReader, err := readPngTile(metatile_path, metatile_offset)
func writeTileResponse(writer http.ResponseWriter, req *http.Request, metatile_path string, metatile_offset uint32, modTime time.Time, ext string) error {
file, tileReader, err := readTile(metatile_path, metatile_offset)
if file != nil {
defer file.Close()
}
Expand All @@ -108,18 +108,18 @@ func writeTileResponse(writer http.ResponseWriter, req *http.Request, metatile_p
return nil
}
writer.Header().Add("Cache-Control", "no-cache")
http.ServeContent(writer, req, "file.png", modTime, pngReader)
http.ServeContent(writer, req, "file."+ext, modTime, tileReader)
return nil
}

func handleRequest(resp http.ResponseWriter, req *http.Request, data_dir, map_name, renderd_socket string, renderd_timeout time.Duration, tile_expiration time.Duration) {
z, x, y, err := utils.ParsePath(req.URL.Path)
z, x, y, ext, err := utils.ParsePath(req.URL.Path)
if err != nil {
resp.WriteHeader(http.StatusBadRequest)
resp.Write([]byte(err.Error()))
return
}
resp.Header().Add("Content-Type", "image/png")
resp.Header().Add("Content-Type", "image/"+ext)
metatile_path, metatile_offset := findPath(data_dir, map_name, z, x, y)
fileInfo, statErr := os.Stat(metatile_path)
if statErr != nil {
Expand Down Expand Up @@ -153,8 +153,8 @@ func handleRequest(resp http.ResponseWriter, req *http.Request, data_dir, map_na
}
}
modTime := fileInfo.ModTime()
errPng := writeTileResponse(resp, req, metatile_path, metatile_offset, modTime)
if errPng != nil {
errTile := writeTileResponse(resp, req, metatile_path, metatile_offset, modTime, ext)
if errTile != nil {
resp.WriteHeader(http.StatusInternalServerError)
}
}
Expand Down
22 changes: 16 additions & 6 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ import (
"github.com/nielsole/go_tile/utils"
)

func TestParse(t *testing.T) {
z, x, y, ext, err := utils.ParsePath("/tile/4/3/2.webp")
if err != nil {
t.Error(err)
}
if z != 4 || x != 3 || y != 2 || ext != "webp" {
t.Fail()
}
}

func TestParseError(t *testing.T) {
invalid_paths := []string{
"/tile/4/3/2",
Expand All @@ -24,7 +34,7 @@ func TestParseError(t *testing.T) {
"/tile/abc/3/2.png",
}
for _, path := range invalid_paths {
_, _, _, err := utils.ParsePath(path)
_, _, _, _, err := utils.ParsePath(path)
if err == nil {
t.Errorf("expected error for path %s", path)
}
Expand All @@ -38,13 +48,13 @@ func TestParseError(t *testing.T) {
// 22 microsecond and 13microsecond avg. response time is really nothing worth optimizing
func BenchmarkPngRead(b *testing.B) {
for i := 0; i < b.N; i++ {
readPngTile("mock_data/0.meta", 0)
readTile("mock_data/0.meta", 0)
}
}

func BenchmarkParsePath(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _, _, err := utils.ParsePath("/tile/4/3/2.png")
_, _, _, _, err := utils.ParsePath("/tile/4/3/2.png")
if err != nil {
b.Error(err)
}
Expand All @@ -59,7 +69,7 @@ func TestWriteTileResponse(t *testing.T) {

// Call writeTileResponse function
modTime := time.Now()
err := writeTileResponse(w, req, "mock_data/0.meta", 0, modTime)
err := writeTileResponse(w, req, "mock_data/0.meta", 0, modTime, "png")
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -92,7 +102,7 @@ func TestWriteTileResponse(t *testing.T) {
func TestWriteTileResponse404(t *testing.T) {
req := httptest.NewRequest("GET", "http://example.com/", bytes.NewReader([]byte{}))
resp := httptest.ResponseRecorder{}
if err := writeTileResponse(&resp, req, "mock_data/404.meta", 0, time.Now()); err != nil {
if err := writeTileResponse(&resp, req, "mock_data/404.meta", 0, time.Now(), "png"); err != nil {
t.Error(err)
}
if resp.Code != 404 {
Expand All @@ -103,7 +113,7 @@ func TestWriteTileResponse404(t *testing.T) {
func TestWriteTileResponseOutOfBounds(t *testing.T) {
req := httptest.NewRequest("GET", "http://example.com/", bytes.NewReader([]byte{}))
resp := httptest.ResponseRecorder{}
if err := writeTileResponse(&resp, req, "mock_data/0.meta", 65, time.Now()); err != nil {
if err := writeTileResponse(&resp, req, "mock_data/0.meta", 65, time.Now(), "png"); err != nil {
t.Error(err)
}
if resp.Code != 500 {
Expand Down
5 changes: 4 additions & 1 deletion renderer/experimental-renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,10 @@ func Mmap(path string) (*[]byte, *os.File, error) {
}

func HandleRenderRequest(w http.ResponseWriter, r *http.Request, duration time.Duration, data *Data, maxTreeDepth uint32, mmapData *[]byte) {
z, x, y, err := utils.ParsePath(r.URL.Path)
z, x, y, ext, err := utils.ParsePath(r.URL.Path)
if ext != "png" {
http.Error(w, "Only png is supported", http.StatusBadRequest)
}
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
Expand Down
6 changes: 4 additions & 2 deletions renderer/experimental-renderer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ func BenchmarkServeEmptyTile(b *testing.B) {
//
// c03af9bccb2499f3f0291c8e8aca22f141f06600
//
// 2456439762 ns/op
// 2456439762 ns/op
// 2536369085 ns/op
func BenchmarkServeFullTile(b *testing.B) {
b.StopTimer()
pathTile := "/tile/11/1081/661.png"
Expand Down Expand Up @@ -145,7 +146,8 @@ func BenchmarkServeFullTile(b *testing.B) {
//
// c03af9bccb2499f3f0291c8e8aca22f141f06600
//
// 4617123956 ns/op
// 4617123956 ns/op
// 101012328 ns/op
func BenchmarkServeFullTileZ3(b *testing.B) {
b.StopTimer()
pathTile := "/tile/3/4/2.png"
Expand Down
12 changes: 7 additions & 5 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import (
"strconv"
)

func ParsePath(path string) (z, x, y uint32, err error) {
matcher := regexp.MustCompile(`^/tile/([0-9]+)/([0-9]+)/([0-9]+).png$`)
matches := matcher.FindStringSubmatch(path)
if len(matches) != 4 {
return 0, 0, 0, errors.New("could not match path")
var _matcher = regexp.MustCompile(`^/tile/([0-9]+)/([0-9]+)/([0-9]+).(png|webp)$`)

func ParsePath(path string) (z, x, y uint32, ext string, err error) {
matches := _matcher.FindStringSubmatch(path)
if len(matches) != 5 {
return 0, 0, 0, "", errors.New("could not match path")
}
zInt, err := strconv.Atoi(matches[1])
if err != nil {
Expand All @@ -24,6 +25,7 @@ func ParsePath(path string) (z, x, y uint32, err error) {
if err != nil {
return
}
ext = matches[4]
z = uint32(zInt)
x = uint32(xInt)
y = uint32(yInt)
Expand Down

0 comments on commit 8a3a6e5

Please sign in to comment.