|
| 1 | +# Open Map API |
| 2 | + |
| 3 | +En Google Maps API-kompatibel geocoding-, routing- och optimeringstjänst byggd med Node.js, Koa och TypeScript. API:et fungerar som en gateway för öppna kartdata-tjänster som Pelias, OSRM och VROOM. |
| 4 | + |
| 5 | +## 🌟 Funktioner |
| 6 | + |
| 7 | +- **Geocoding & Reverse Geocoding** - Konvertera adresser till koordinater och vice versa |
| 8 | +- **Directions** - Beräkna rutter och turn-by-turn navigering |
| 9 | +- **Distance Matrix** - Beräkna avstånd och restider mellan flera punkter |
| 10 | +- **Places Search** - Sök efter platser med text eller närhetssökning |
| 11 | +- **Autocomplete** - Förslag för adresser och platser |
| 12 | +- **Route Optimization** - VRP (Vehicle Routing Problem) och TSP (Traveling Salesman Problem) |
| 13 | +- **Google Maps API-kompatibel** - Drop-in replacement för många Google Maps API-anrop |
| 14 | + |
| 15 | +## 🏗️ Arkitektur |
| 16 | + |
| 17 | +API:et använder följande öppna tjänster: |
| 18 | +- **[Pelias](https://pelias.io/)** - Geocoding och platssökning |
| 19 | +- **[OSRM](http://project-osrm.org/)** - Routing och avståndsmätning |
| 20 | +- **[VROOM](http://vroom-project.org/)** - Ruttoptimering |
| 21 | + |
| 22 | +## 🚀 Snabbstart |
| 23 | + |
| 24 | +### Lokal utveckling |
| 25 | + |
| 26 | +```bash |
| 27 | +# Klona repositoriet |
| 28 | +git clone https://github.com/iteam/openmapapi.git |
| 29 | +cd openmapapi |
| 30 | + |
| 31 | +# Installera dependencies |
| 32 | +npm install |
| 33 | + |
| 34 | +# Kopiera miljövariabler |
| 35 | +cp .env.example .env |
| 36 | + |
| 37 | +# Starta utvecklingsservern |
| 38 | +npm run dev |
| 39 | +``` |
| 40 | + |
| 41 | +API:et kommer att vara tillgängligt på `http://localhost:3000` |
| 42 | + |
| 43 | +### Docker |
| 44 | + |
| 45 | +```bash |
| 46 | +# Bygg Docker-image |
| 47 | +docker build -t openmaps-api . |
| 48 | + |
| 49 | +# Kör container |
| 50 | +docker run -p 3000:3000 openmaps-api |
| 51 | +``` |
| 52 | + |
| 53 | +## 📚 API-dokumentation |
| 54 | + |
| 55 | +Interaktiv API-dokumentation finns tillgänglig på: |
| 56 | +- **Lokal utveckling**: http://localhost:3000 |
| 57 | +- **Produktion**: https://openmaps.iteam.pub |
| 58 | + |
| 59 | +OpenAPI-specifikation: `/openapi.json` |
| 60 | + |
| 61 | +## 🔧 Användning |
| 62 | + |
| 63 | +### Geocoding |
| 64 | + |
| 65 | +Konvertera en adress till koordinater: |
| 66 | + |
| 67 | +```bash |
| 68 | +curl "https://openmaps.iteam.pub/maps/api/geocode/json?address=Drottninggatan+1,+Stockholm&key=your-api-key" |
| 69 | +``` |
| 70 | + |
| 71 | +Reverse geocoding (koordinater till adress): |
| 72 | + |
| 73 | +```bash |
| 74 | +curl "https://openmaps.iteam.pub/maps/api/geocode/json?latlng=59.3293,18.0686&key=your-api-key" |
| 75 | +``` |
| 76 | + |
| 77 | +### Directions |
| 78 | + |
| 79 | +Få vägbeskrivning mellan två punkter: |
| 80 | + |
| 81 | +```bash |
| 82 | +curl "https://openmaps.iteam.pub/maps/api/directions/json?origin=Stockholm+Central&destination=Gamla+Stan&key=your-api-key" |
| 83 | +``` |
| 84 | + |
| 85 | +Med waypoints och alternativ: |
| 86 | + |
| 87 | +```bash |
| 88 | +curl "https://openmaps.iteam.pub/maps/api/directions/json?origin=Stockholm&destination=Göteborg&waypoints=Jönköping&alternatives=true&key=your-api-key" |
| 89 | +``` |
| 90 | + |
| 91 | +### Distance Matrix |
| 92 | + |
| 93 | +Beräkna avstånd mellan flera punkter: |
| 94 | + |
| 95 | +```bash |
| 96 | +curl "https://openmaps.iteam.pub/maps/api/distancematrix/json?origins=Stockholm|Göteborg&destinations=Malmö|Uppsala&key=your-api-key" |
| 97 | +``` |
| 98 | + |
| 99 | +### Places Search |
| 100 | + |
| 101 | +Textsökning efter platser: |
| 102 | + |
| 103 | +```bash |
| 104 | +curl "https://openmaps.iteam.pub/maps/api/place/textsearch/json?query=restauranger+i+Stockholm&key=your-api-key" |
| 105 | +``` |
| 106 | + |
| 107 | +Närhetssökning: |
| 108 | + |
| 109 | +```bash |
| 110 | +curl "https://openmaps.iteam.pub/maps/api/place/nearbysearch/json?location=59.3293,18.0686&radius=1000&type=restaurant&key=your-api-key" |
| 111 | +``` |
| 112 | + |
| 113 | +Autocomplete: |
| 114 | + |
| 115 | +```bash |
| 116 | +curl "https://openmaps.iteam.pub/maps/api/place/autocomplete/json?input=Drottning&key=your-api-key" |
| 117 | +``` |
| 118 | + |
| 119 | +### Route Optimization |
| 120 | + |
| 121 | +Traveling Salesman Problem (TSP): |
| 122 | + |
| 123 | +```bash |
| 124 | +curl -X POST "https://openmaps.iteam.pub/api/optimization/tsp" \ |
| 125 | + -H "Content-Type: application/json" \ |
| 126 | + -d '{ |
| 127 | + "locations": [[18.0686, 59.3293], [18.0696, 59.3303], [18.0706, 59.3313]], |
| 128 | + "start": 0, |
| 129 | + "profile": "driving" |
| 130 | + }' |
| 131 | +``` |
| 132 | + |
| 133 | +Vehicle Routing Problem (VRP): |
| 134 | + |
| 135 | +```bash |
| 136 | +curl -X POST "https://openmaps.iteam.pub/api/optimization/vrp" \ |
| 137 | + -H "Content-Type: application/json" \ |
| 138 | + -d '{ |
| 139 | + "jobs": [ |
| 140 | + {"id": 1, "location": [18.0686, 59.3293], "delivery": [10]}, |
| 141 | + {"id": 2, "location": [18.0696, 59.3303], "delivery": [15]} |
| 142 | + ], |
| 143 | + "vehicles": [ |
| 144 | + {"id": 1, "start": [18.0676, 59.3283], "capacity": [50]} |
| 145 | + ] |
| 146 | + }' |
| 147 | +``` |
| 148 | + |
| 149 | +## 🌍 JavaScript/TypeScript Exempel |
| 150 | + |
| 151 | +### Fetch API |
| 152 | + |
| 153 | +```javascript |
| 154 | +// Geocoding |
| 155 | +const geocodeResponse = await fetch( |
| 156 | + 'https://openmaps.iteam.pub/maps/api/geocode/json?address=Stockholm&key=your-api-key' |
| 157 | +); |
| 158 | +const geocodeData = await geocodeResponse.json(); |
| 159 | +console.log(geocodeData.results[0].geometry.location); |
| 160 | + |
| 161 | +// Directions |
| 162 | +const directionsResponse = await fetch( |
| 163 | + 'https://openmaps.iteam.pub/maps/api/directions/json?origin=Stockholm&destination=Göteborg&key=your-api-key' |
| 164 | +); |
| 165 | +const directionsData = await directionsResponse.json(); |
| 166 | +console.log(directionsData.routes[0].legs[0].duration.text); |
| 167 | +``` |
| 168 | + |
| 169 | +### Axios |
| 170 | + |
| 171 | +```javascript |
| 172 | +import axios from 'axios'; |
| 173 | + |
| 174 | +const api = axios.create({ |
| 175 | + baseURL: 'https://openmaps.iteam.pub', |
| 176 | + params: { |
| 177 | + key: 'your-api-key' |
| 178 | + } |
| 179 | +}); |
| 180 | + |
| 181 | +// Geocoding |
| 182 | +const geocode = await api.get('/maps/api/geocode/json', { |
| 183 | + params: { address: 'Drottninggatan 1, Stockholm' } |
| 184 | +}); |
| 185 | + |
| 186 | +// Places search |
| 187 | +const places = await api.get('/maps/api/place/textsearch/json', { |
| 188 | + params: { query: 'restauranger i Stockholm' } |
| 189 | +}); |
| 190 | + |
| 191 | +// Route optimization |
| 192 | +const optimization = await api.post('/api/optimization/tsp', { |
| 193 | + locations: [[18.0686, 59.3293], [18.0696, 59.3303]], |
| 194 | + start: 0 |
| 195 | +}); |
| 196 | +``` |
| 197 | + |
| 198 | +## 🔑 API-nycklar |
| 199 | + |
| 200 | +För närvarande accepterar API:et vilken sträng som helst som API-nyckel. I produktion bör du implementera riktig autentisering. |
| 201 | + |
| 202 | +## 🏃♂️ Prestanda |
| 203 | + |
| 204 | +- **Caching**: Implementera Redis för att cacha vanliga förfrågningar |
| 205 | +- **Rate Limiting**: Nginx ingress har rate limiting konfigurerat (100 req/min) |
| 206 | +- **Horizontal Scaling**: HPA konfigurerat för automatisk skalning baserat på CPU/minne |
| 207 | + |
| 208 | +## 🔧 Konfiguration |
| 209 | + |
| 210 | +Miljövariabler: |
| 211 | + |
| 212 | +```bash |
| 213 | +# Server |
| 214 | +PORT=3000 |
| 215 | +NODE_ENV=production |
| 216 | +API_BASE_URL=https://openmaps.iteam.pub |
| 217 | + |
| 218 | +# Externa tjänster |
| 219 | +PELIAS_BASE_URL=https://pelias.telge.iteam.pub/v1 |
| 220 | +OSRM_BASE_URL=https://osrm.telge.iteam.pub |
| 221 | +VROOM_BASE_URL=https://vroom.telge.iteam.pub |
| 222 | +``` |
| 223 | + |
| 224 | +## 🚢 Deployment |
| 225 | + |
| 226 | +### Kubernetes med FluxCD |
| 227 | + |
| 228 | +Projektet är konfigurerat för deployment med FluxCD. Peka FluxCD på detta repository så kommer det automatiskt att deploya: |
| 229 | + |
| 230 | +- Namespace: `openmaps` |
| 231 | +- Deployment med 2 replicas |
| 232 | +- Service på port 80 |
| 233 | +- Ingress med TLS (Let's Encrypt) |
| 234 | +- HPA för autoskalning |
| 235 | +- ConfigMap för miljövariabler |
| 236 | + |
| 237 | +### GitHub Actions |
| 238 | + |
| 239 | +Automatisk CI/CD pipeline som: |
| 240 | +- Bygger och testar koden |
| 241 | +- Skapar Docker-image |
| 242 | +- Pushar till GitHub Container Registry (GHCR) |
| 243 | +- Taggar med branch och SHA |
| 244 | + |
| 245 | +## 🛠️ Utveckling |
| 246 | + |
| 247 | +### Projektstruktur |
| 248 | + |
| 249 | +``` |
| 250 | +src/ |
| 251 | +├── adapters/ # HTTP request/response hantering |
| 252 | +├── config/ # Konfiguration och externa klienter |
| 253 | +├── middleware/ # Request validering och preprocessing |
| 254 | +├── routes/ # Route definitioner och Swagger docs |
| 255 | +├── schemas/ # Zod schemas och TypeScript types |
| 256 | +└── services/ # Affärslogik och dataåtkomst |
| 257 | +``` |
| 258 | + |
| 259 | +### Lägga till nya endpoints |
| 260 | + |
| 261 | +1. Definiera schema i `src/schemas/` |
| 262 | +2. Implementera service i `src/services/` |
| 263 | +3. Skapa adapter i `src/adapters/` |
| 264 | +4. Lägg till routes i `src/routes/` |
| 265 | +5. Registrera schema för OpenAPI-dokumentation |
| 266 | + |
| 267 | +### Kodstil |
| 268 | + |
| 269 | +- **TypeScript** för typsäkerhet |
| 270 | +- **Zod** för runtime-validering |
| 271 | +- **Funktionell programmering** - använd map/reduce istället för loopar |
| 272 | +- **Engelska** för all kod, kommentarer och dokumentation |
| 273 | +- **Centraliserad felhantering** - kasta typade fel, middleware hanterar formatering |
| 274 | + |
| 275 | +## 🧪 Testning |
| 276 | + |
| 277 | +```bash |
| 278 | +# Kör tester (när de implementeras) |
| 279 | +npm test |
| 280 | + |
| 281 | +# Bygg projektet |
| 282 | +npm run build |
| 283 | + |
| 284 | +# Starta produktionsserver |
| 285 | +npm start |
| 286 | +``` |
| 287 | + |
| 288 | +## 📊 Monitoring |
| 289 | + |
| 290 | +- **Health Check**: `/health` endpoint |
| 291 | +- **Metrics**: Kubernetes liveness/readiness probes |
| 292 | +- **Logging**: Strukturerad loggning till stdout |
| 293 | + |
| 294 | +## 🤝 Bidrag |
| 295 | + |
| 296 | +1. Forka repositoriet |
| 297 | +2. Skapa en feature branch |
| 298 | +3. Gör dina ändringar |
| 299 | +4. Lägg till tester |
| 300 | +5. Skicka en pull request |
| 301 | + |
| 302 | +## 📄 Licens |
| 303 | + |
| 304 | +ISC License |
| 305 | + |
| 306 | +## 🔗 Relaterade projekt |
| 307 | + |
| 308 | +- [Pelias Geocoder](https://github.com/pelias/pelias) |
| 309 | +- [OSRM Backend](https://github.com/Project-OSRM/osrm-backend) |
| 310 | +- [VROOM](https://github.com/VROOM-Project/vroom) |
| 311 | + |
| 312 | +## 📞 Support |
| 313 | + |
| 314 | +För frågor och support, skapa en issue i detta repository. |
| 315 | + |
| 316 | +--- |
| 317 | + |
| 318 | +**Byggd med ❤️ av Iteam** |
0 commit comments