Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Wprowadzenie prostego wersjonowania wersjonowania #131

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion firebase/functions/src/getFileEditDate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ export interface DataResponse {
token: string
data: string
message: string
version: number
}

export async function getFileUpdate(
givenToken: string,
file: string
file: string,
version: number
): Promise<DataResponse> {
const fileBucket = app
.storage()
Expand All @@ -26,6 +28,7 @@ export async function getFileUpdate(
const content = await fileBucket.download()

return {
version,
error: false,
oldToken: givenToken,
token: currentToken,
Expand All @@ -35,6 +38,7 @@ export async function getFileUpdate(
}

return {
version,
error: false,
oldToken: givenToken,
token: currentToken,
Expand Down
8 changes: 7 additions & 1 deletion firebase/functions/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,29 @@ import { getFileUpdate, DataResponse } from './getFileEditDate'

export const mapItems = functions.https.onRequest(async (request, response) => {
const token = request.query.token?.toString() || ''
// parseInt aby nie bawić się w walidacje. `|| 1` aby NaN zastąpić 1
// NIE POZWALAĆ NA WSZYSTKO, bo można wtedy pobrać inne pliki !!!
const version = parseInt(request.query.version?.toString() || '1') || 1

try {
const result = await getFileUpdate(token, 'map_items.json')
const result = await getFileUpdate(token, `map_items_${version}.json`, version)

functions.logger.log({
version,
requestedToken: token,
updateNeeded: result.data.length !== 0,
})

response.send(result)
} catch (e) {
functions.logger.error({
version,
requestedToken: token,
error: e,
})

const error: DataResponse = {
version,
error: true,
oldToken: token,
token: '',
Expand Down
10 changes: 6 additions & 4 deletions firebase/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ npm run serve
```

Endpoint dostępny w:
> http://localhost:5001/kampus-sggw-2021/us-central1/mapItems?token=mGl0bnenMANgd9CUpeg3Pw==
> http://localhost:5001/kampus-sggw-2021/us-central1/mapItems?token=mGl0bnenMANgd9CUpeg3Pw==&version=1

## Deploy do firebase
```
Expand All @@ -46,18 +46,19 @@ firebase deploy

# Użycie

> /mapItems?token=[Token aktualizacji]
> /mapItems?token=[Token aktualizacji]&version=[Wersja]

Przykładowo
> /mapItems?token=mGl0bnenMANgd9CUpeg3Pw==
> /mapItems?token=mGl0bnenMANgd9CUpeg3Pw==&version=1

Na produkcji
> https://us-central1-kampus-sggw-2021.cloudfunctions.net/mapItems?token=mGl0bnenMANgd9CUpeg3Pw==
> https://us-central1-kampus-sggw-2021.cloudfunctions.net/mapItems?token=mGl0bnenMANgd9CUpeg3Pw==&version=1

Zwraca

```
interface DataResponse {
version: number
error: boolean
oldToken: string
token: string
Expand All @@ -66,6 +67,7 @@ interface DataResponse {
}
```

version = Wersja zwracanych danych <br />
error = false - wszystko zadziało ok <br />
oldToken = stary token, taki jak został odczytany <br />
token = aktualny token <br />
Expand Down