1
2

More than 1 year has passed since last update.

MBTiles ベースのベクトルタイルサーバを systemd でサービス化する

Last updated at Posted at 2022-02-03

MBTiles ベースのベクトルタイルサーバを systemd でサービス化した際の作業の記録です。使用したのは Raspberry Pi OS です。

social preview image

MBTiles ベースのベクトルタイルサーバ

require './constants'
require 'sqlite3'
require 'sinatra'

DB = SQLite3::Database.new(PATH)

def get_tile(z, x, y)
  tile_row = 2 ** z.to_i - y.to_i - 1
  tile = DB.execute <<-EOS
SELECT tile_data FROM tiles WHERE \
zoom_level=#{z} AND tile_column=#{x} AND tile_row=#{tile_row}
  EOS
  tile[0] ? tile[0][0] : 404
end

get '/zxy/:z/:x/:y.pbf' do |z, x, y|
  content_type 'application/vnd.mapbox-vector-tile'
  response.headers['Content-Encoding'] = 'gzip'
  get_tile(z, x, y)
end

このプログラムを rake host で呼べるようにしています。

systemd でサービス化するのに実行したコマンド

sudo systemctl edit --force --full optgeo.kagami
sudo systemctl start optgeo.kagami
sudo systemctl status optgeo.kagami
sudo systemctl enable optgeo.kagami

上記第一行を実行すると、エディタが立ち上がるので、次のように入力しています。

[Unit]
Description = optgeo.kagami
After = basic.target

[Service]
Type = simple
ExecStart = bash -c 'cd /mnt/hdd/kagami; rake host'
Restart = always

[Install]
WantedBy = multi-user.target

sudo systemctl enable optgeo.kagami を実施した段階で、サービス化されるので、その後はリブートしたら自動的にベクトルタイルサーバが立ち上がることになります。

追記: nginx でリバースプロキシする

Raspberry Pi OS で nginx 使っている場合には、たとえば /etc/nginx/sites-available/default あたりで次の設定をして、sudo nginx -s reload すれば OK です。

location, proxy_pass が作業のキーワードになります。

server {
  listen 443 ssl http2;
  server_name ...;
  root ...;
  ssl_certificate /etc/letsencrypt/live/.../fullchain.pem;
  ssl_certificate_key /etc/letsencrypt/live/.../privkey.pem;
  ssl_dhparam dhparam.pem;
  add_header Strict-Transport-Security "max-age=15768000; includeSubdomains";
  add_header Access-Control-Allow-Origin * always;
  location /kagami/zxy {
    proxy_pass http://localhost:8002/zxy;
  }
}

情報源

ありがとうございます。

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2