Skip to content

Commit

Permalink
Making more configurable. Gracefully handle non-existing socket
Browse files Browse the repository at this point in the history
  • Loading branch information
nielsole committed May 15, 2022
1 parent bb8d9f1 commit 25349f7
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Currently supported features:
### With a renderd backend

* Currently there are no prebuilt binaries included here, so you need to build from source (`go build .`)
* go_tile is hardcoded to use `/var/run/renderd/renderd.sock` right now
* You need to have a working renderd setup

### With prerendered Tiles

Expand All @@ -36,11 +36,15 @@ Now you can view your map at <http://localhost:8080/>. Tiles are served at <http
If you prefer to run the binary directly you have the following options:

```
Usage of /tmp/go-build004735986/b001/exe/osm-tileserver:
Usage of /tmp/go-build2340306719/b001/exe/osm-tileserver:
-data string
Path to directory containing tiles (default "./data")
-port string
Listening port (default ":8080")
-renderd-timeout int
time in seconds to wait for renderd before returning an error to the client. Set negative to disable (default 60)
-socket string
Path to renderd socket. Set to '' to disable rendering (default "/var/run/renderd/renderd.sock")
-static string
Path to static file directory (default "./static/")
```
Expand Down
13 changes: 10 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func parsePath(path string) (z, x, y uint32, err error) {
return
}

func handleRequest(resp http.ResponseWriter, req *http.Request, data_dir *string) {
func handleRequest(resp http.ResponseWriter, req *http.Request, data_dir *string, renderd_sock_path string, renderd_timeout time.Duration) {
z, x, y, err := parsePath(req.URL.Path)
if err != nil {
resp.WriteHeader(http.StatusBadRequest)
Expand All @@ -135,7 +135,11 @@ func handleRequest(resp http.ResponseWriter, req *http.Request, data_dir *string
fileInfo, statErr := os.Stat(metatile_path)
if statErr != nil {
if errors.Is(statErr, os.ErrNotExist) {
renderErr := requestRender(x, y, z)
if len(renderd_sock_path) == 0 {
resp.WriteHeader(http.StatusNotFound)
return
}
renderErr := requestRender(x, y, z, renderd_sock_path, renderd_timeout)
if renderErr != nil {
fmt.Printf("Could not generate tile for coordinates %d, %d, %d (x,y,z). '%s'\n", x, y, z, renderErr)
// Not returning as we are hoping and praying that rendering did nonetheless produce a file
Expand Down Expand Up @@ -164,14 +168,17 @@ func main() {
listen_port := flag.String("port", ":8080", "Listening port")
data_dir := flag.String("data", "./data", "Path to directory containing tiles")
static_dir := flag.String("static", "./static/", "Path to static file directory")
renderd_sock_path := flag.String("socket", "/var/run/renderd/renderd.sock", "Path to renderd socket. Set to '' to disable rendering")
renderd_timeout := flag.Int("renderd-timeout", 60, "time in seconds to wait for renderd before returning an error to the client. Set negative to disable")
var renderd_timeout_duration time.Duration = time.Duration(*renderd_timeout) * time.Second
flag.Parse()
http.HandleFunc("/tile/", func(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
w.WriteHeader(http.StatusMethodNotAllowed) // TODO return 4xx wrong method
w.Write([]byte("Only GET requests allowed"))
return
}
handleRequest(w, r, data_dir)
handleRequest(w, r, data_dir, *renderd_sock_path, renderd_timeout_duration)
})
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
http.FileServer(http.Dir(*static_dir)).ServeHTTP(w, r)
Expand Down
10 changes: 6 additions & 4 deletions renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ type Request struct {
Map [44]byte
}

func requestRender(x, y, z uint32) error {
c, err := net.Dial("unix", "/var/run/renderd/renderd.sock")
func requestRender(x, y, z uint32, renderd_sock_path string, renderd_timeout time.Duration) error {
c, err := net.Dial("unix", renderd_sock_path)
if err != nil {
panic(err)
return err
}
defer c.Close()
c.SetDeadline(time.Now().Add(30 * time.Second))
if renderd_timeout > 0 {
c.SetDeadline(time.Now().Add(renderd_timeout))
}
request := Request{
Version: 3,
CmdPriority: 5,
Expand Down

0 comments on commit 25349f7

Please sign in to comment.