web: use proper caching for autoupdate endpoint

This commit is contained in:
Jeffrey Morgan 2023-07-08 16:47:58 -04:00
parent 1e97807808
commit a8dc0c9b5f
4 changed files with 93 additions and 82 deletions

View file

@ -1,44 +1,42 @@
import { NextResponse } from 'next/server' import { NextResponse } from 'next/server'
import semver from 'semver' import semver from 'semver'
import { Octokit } from '@octokit/rest'
import { RequestError } from '@octokit/types'
const octokit = new Octokit()
export async function GET(req: Request) { export async function GET(req: Request) {
const { searchParams } = new URL(req.url) const { searchParams } = new URL(req.url)
const os = searchParams.get('os') || '' const os = searchParams.get('os') || 'darwin'
const version = searchParams.get('version') || '' const version = searchParams.get('version') || '0.0.0'
if (!version) { if (!version) {
return new Response('not found', { status: 404 }) return new Response('not found', { status: 404 })
} }
try { const res = await fetch('https://api.github.com/repos/jmorganca/ollama/releases', { next: { revalidate: 60 } })
const { data } = await octokit.repos.getLatestRelease({ const data = await res.json()
owner: 'jmorganca',
repo: 'ollama', if (data.length === 0) {
}) return new Response('not found', { status: 404 })
}
const latest = data[0]
const assets = latest.assets || []
if (assets.length === 0) {
return new Response('not found', { status: 404 })
}
// todo: get the correct asset for the current arch/os // todo: get the correct asset for the current arch/os
const asset = data.assets.find(a => a.name.toLowerCase().includes(os)) const asset = assets.find((a: any) => a.name.toLowerCase().includes(os) && a.name.toLowerCase().includes('.zip'))
if (!asset) { if (!asset) {
return new Response('not found', { status: 404 }) return new Response('not found', { status: 404 })
} }
if (semver.lt(version, data.tag_name)) { console.log(asset)
if (semver.lt(version, latest.tag_name)) {
return NextResponse.json({ version: data.tag_name, url: asset.browser_download_url }) return NextResponse.json({ version: data.tag_name, url: asset.browser_download_url })
} }
return new Response('up to date', { status: 204 }) return new Response(null, { status: 204 })
} catch (error) {
const e = error as RequestError
if (e.status === 404) {
return new Response('not found', { status: 404 })
}
return new Response('internal server error', { status: 500 })
}
} }

View file

@ -3,16 +3,29 @@ import { redirect } from 'next/navigation'
const octokit = new Octokit() const octokit = new Octokit()
export const revalidate = 60
export default async function Download() { export default async function Download() {
const { data } = await octokit.repos.getLatestRelease({ const res = await fetch('https://api.github.com/repos/jmorganca/ollama/releases', { next: { revalidate: 60 } })
owner: 'jmorganca', const data = await res.json()
repo: 'ollama',
}) if (data.length === 0) {
return new Response('not found', { status: 404 })
}
const latest = data[0]
const assets = latest.assets || []
if (assets.length === 0) {
return new Response('not found', { status: 404 })
}
// todo: get the correct asset for the current arch/os // todo: get the correct asset for the current arch/os
const asset = data.assets.find(a => a.name.toLowerCase().includes('darwin') && a.name.toLowerCase().includes('.zip')) const asset = assets.find(
(a: any) => a.name.toLowerCase().includes('darwin') && a.name.toLowerCase().includes('.zip')
)
if (!asset) {
return new Response('not found', { status: 404 })
}
if (asset) { if (asset) {
redirect(asset.browser_download_url) redirect(asset.browser_download_url)

88
web/package-lock.json generated
View file

@ -17,7 +17,7 @@
"encoding": "^0.1.13", "encoding": "^0.1.13",
"eslint": "8.44.0", "eslint": "8.44.0",
"eslint-config-next": "13.4.7", "eslint-config-next": "13.4.7",
"next": "13.4.7", "next": "13.4.9",
"postcss": "8.4.24", "postcss": "8.4.24",
"react": "18.2.0", "react": "18.2.0",
"react-dom": "18.2.0", "react-dom": "18.2.0",
@ -191,9 +191,9 @@
"integrity": "sha512-XPSJHWmi394fuUuzDnGz1wiKqWfo1yXecHQMRf2l6hztTO+nPru658AyDngaBe7isIxEkRsPR3FZh+s7iVa4Uw==" "integrity": "sha512-XPSJHWmi394fuUuzDnGz1wiKqWfo1yXecHQMRf2l6hztTO+nPru658AyDngaBe7isIxEkRsPR3FZh+s7iVa4Uw=="
}, },
"node_modules/@next/env": { "node_modules/@next/env": {
"version": "13.4.7", "version": "13.4.9",
"resolved": "https://registry.npmjs.org/@next/env/-/env-13.4.7.tgz", "resolved": "https://registry.npmjs.org/@next/env/-/env-13.4.9.tgz",
"integrity": "sha512-ZlbiFulnwiFsW9UV1ku1OvX/oyIPLtMk9p/nnvDSwI0s7vSoZdRtxXNsaO+ZXrLv/pMbXVGq4lL8TbY9iuGmVw==" "integrity": "sha512-vuDRK05BOKfmoBYLNi2cujG2jrYbEod/ubSSyqgmEx9n/W3eZaJQdRNhTfumO+qmq/QTzLurW487n/PM/fHOkw=="
}, },
"node_modules/@next/eslint-plugin-next": { "node_modules/@next/eslint-plugin-next": {
"version": "13.4.7", "version": "13.4.7",
@ -204,9 +204,9 @@
} }
}, },
"node_modules/@next/swc-darwin-arm64": { "node_modules/@next/swc-darwin-arm64": {
"version": "13.4.7", "version": "13.4.9",
"resolved": "https://registry.npmjs.org/@next/swc-darwin-arm64/-/swc-darwin-arm64-13.4.7.tgz", "resolved": "https://registry.npmjs.org/@next/swc-darwin-arm64/-/swc-darwin-arm64-13.4.9.tgz",
"integrity": "sha512-VZTxPv1b59KGiv/pZHTO5Gbsdeoxcj2rU2cqJu03btMhHpn3vwzEK0gUSVC/XW96aeGO67X+cMahhwHzef24/w==", "integrity": "sha512-TVzGHpZoVBk3iDsTOQA/R6MGmFp0+17SWXMEWd6zG30AfuELmSSMe2SdPqxwXU0gbpWkJL1KgfLzy5ReN0crqQ==",
"cpu": [ "cpu": [
"arm64" "arm64"
], ],
@ -219,9 +219,9 @@
} }
}, },
"node_modules/@next/swc-darwin-x64": { "node_modules/@next/swc-darwin-x64": {
"version": "13.4.7", "version": "13.4.9",
"resolved": "https://registry.npmjs.org/@next/swc-darwin-x64/-/swc-darwin-x64-13.4.7.tgz", "resolved": "https://registry.npmjs.org/@next/swc-darwin-x64/-/swc-darwin-x64-13.4.9.tgz",
"integrity": "sha512-gO2bw+2Ymmga+QYujjvDz9955xvYGrWofmxTq7m70b9pDPvl7aDFABJOZ2a8SRCuSNB5mXU8eTOmVVwyp/nAew==", "integrity": "sha512-aSfF1fhv28N2e7vrDZ6zOQ+IIthocfaxuMWGReB5GDriF0caTqtHttAvzOMgJgXQtQx6XhyaJMozLTSEXeNN+A==",
"cpu": [ "cpu": [
"x64" "x64"
], ],
@ -234,9 +234,9 @@
} }
}, },
"node_modules/@next/swc-linux-arm64-gnu": { "node_modules/@next/swc-linux-arm64-gnu": {
"version": "13.4.7", "version": "13.4.9",
"resolved": "https://registry.npmjs.org/@next/swc-linux-arm64-gnu/-/swc-linux-arm64-gnu-13.4.7.tgz", "resolved": "https://registry.npmjs.org/@next/swc-linux-arm64-gnu/-/swc-linux-arm64-gnu-13.4.9.tgz",
"integrity": "sha512-6cqp3vf1eHxjIDhEOc7Mh/s8z1cwc/l5B6ZNkOofmZVyu1zsbEM5Hmx64s12Rd9AYgGoiCz4OJ4M/oRnkE16/Q==", "integrity": "sha512-JhKoX5ECzYoTVyIy/7KykeO4Z2lVKq7HGQqvAH+Ip9UFn1MOJkOnkPRB7v4nmzqAoY+Je05Aj5wNABR1N18DMg==",
"cpu": [ "cpu": [
"arm64" "arm64"
], ],
@ -249,9 +249,9 @@
} }
}, },
"node_modules/@next/swc-linux-arm64-musl": { "node_modules/@next/swc-linux-arm64-musl": {
"version": "13.4.7", "version": "13.4.9",
"resolved": "https://registry.npmjs.org/@next/swc-linux-arm64-musl/-/swc-linux-arm64-musl-13.4.7.tgz", "resolved": "https://registry.npmjs.org/@next/swc-linux-arm64-musl/-/swc-linux-arm64-musl-13.4.9.tgz",
"integrity": "sha512-T1kD2FWOEy5WPidOn1si0rYmWORNch4a/NR52Ghyp4q7KyxOCuiOfZzyhVC5tsLIBDH3+cNdB5DkD9afpNDaOw==", "integrity": "sha512-OOn6zZBIVkm/4j5gkPdGn4yqQt+gmXaLaSjRSO434WplV8vo2YaBNbSHaTM9wJpZTHVDYyjzuIYVEzy9/5RVZw==",
"cpu": [ "cpu": [
"arm64" "arm64"
], ],
@ -264,9 +264,9 @@
} }
}, },
"node_modules/@next/swc-linux-x64-gnu": { "node_modules/@next/swc-linux-x64-gnu": {
"version": "13.4.7", "version": "13.4.9",
"resolved": "https://registry.npmjs.org/@next/swc-linux-x64-gnu/-/swc-linux-x64-gnu-13.4.7.tgz", "resolved": "https://registry.npmjs.org/@next/swc-linux-x64-gnu/-/swc-linux-x64-gnu-13.4.9.tgz",
"integrity": "sha512-zaEC+iEiAHNdhl6fuwl0H0shnTzQoAoJiDYBUze8QTntE/GNPfTYpYboxF5LRYIjBwETUatvE0T64W6SKDipvg==", "integrity": "sha512-iA+fJXFPpW0SwGmx/pivVU+2t4zQHNOOAr5T378PfxPHY6JtjV6/0s1vlAJUdIHeVpX98CLp9k5VuKgxiRHUpg==",
"cpu": [ "cpu": [
"x64" "x64"
], ],
@ -279,9 +279,9 @@
} }
}, },
"node_modules/@next/swc-linux-x64-musl": { "node_modules/@next/swc-linux-x64-musl": {
"version": "13.4.7", "version": "13.4.9",
"resolved": "https://registry.npmjs.org/@next/swc-linux-x64-musl/-/swc-linux-x64-musl-13.4.7.tgz", "resolved": "https://registry.npmjs.org/@next/swc-linux-x64-musl/-/swc-linux-x64-musl-13.4.9.tgz",
"integrity": "sha512-X6r12F8d8SKAtYJqLZBBMIwEqcTRvUdVm+xIq+l6pJqlgT2tNsLLf2i5Cl88xSsIytBICGsCNNHd+siD2fbWBA==", "integrity": "sha512-rlNf2WUtMM+GAQrZ9gMNdSapkVi3koSW3a+dmBVp42lfugWVvnyzca/xJlN48/7AGx8qu62WyO0ya1ikgOxh6A==",
"cpu": [ "cpu": [
"x64" "x64"
], ],
@ -294,9 +294,9 @@
} }
}, },
"node_modules/@next/swc-win32-arm64-msvc": { "node_modules/@next/swc-win32-arm64-msvc": {
"version": "13.4.7", "version": "13.4.9",
"resolved": "https://registry.npmjs.org/@next/swc-win32-arm64-msvc/-/swc-win32-arm64-msvc-13.4.7.tgz", "resolved": "https://registry.npmjs.org/@next/swc-win32-arm64-msvc/-/swc-win32-arm64-msvc-13.4.9.tgz",
"integrity": "sha512-NPnmnV+vEIxnu6SUvjnuaWRglZzw4ox5n/MQTxeUhb5iwVWFedolPFebMNwgrWu4AELwvTdGtWjqof53AiWHcw==", "integrity": "sha512-5T9ybSugXP77nw03vlgKZxD99AFTHaX8eT1ayKYYnGO9nmYhJjRPxcjU5FyYI+TdkQgEpIcH7p/guPLPR0EbKA==",
"cpu": [ "cpu": [
"arm64" "arm64"
], ],
@ -309,9 +309,9 @@
} }
}, },
"node_modules/@next/swc-win32-ia32-msvc": { "node_modules/@next/swc-win32-ia32-msvc": {
"version": "13.4.7", "version": "13.4.9",
"resolved": "https://registry.npmjs.org/@next/swc-win32-ia32-msvc/-/swc-win32-ia32-msvc-13.4.7.tgz", "resolved": "https://registry.npmjs.org/@next/swc-win32-ia32-msvc/-/swc-win32-ia32-msvc-13.4.9.tgz",
"integrity": "sha512-6Hxijm6/a8XqLQpOOf/XuwWRhcuc/g4rBB2oxjgCMuV9Xlr2bLs5+lXyh8w9YbAUMYR3iC9mgOlXbHa79elmXw==", "integrity": "sha512-ojZTCt1lP2ucgpoiFgrFj07uq4CZsq4crVXpLGgQfoFq00jPKRPgesuGPaz8lg1yLfvafkU3Jd1i8snKwYR3LA==",
"cpu": [ "cpu": [
"ia32" "ia32"
], ],
@ -324,9 +324,9 @@
} }
}, },
"node_modules/@next/swc-win32-x64-msvc": { "node_modules/@next/swc-win32-x64-msvc": {
"version": "13.4.7", "version": "13.4.9",
"resolved": "https://registry.npmjs.org/@next/swc-win32-x64-msvc/-/swc-win32-x64-msvc-13.4.7.tgz", "resolved": "https://registry.npmjs.org/@next/swc-win32-x64-msvc/-/swc-win32-x64-msvc-13.4.9.tgz",
"integrity": "sha512-sW9Yt36Db1nXJL+mTr2Wo0y+VkPWeYhygvcHj1FF0srVtV+VoDjxleKtny21QHaG05zdeZnw2fCtf2+dEqgwqA==", "integrity": "sha512-QbT03FXRNdpuL+e9pLnu+XajZdm/TtIXVYY4lA9t+9l0fLZbHXDYEKitAqxrOj37o3Vx5ufxiRAniaIebYDCgw==",
"cpu": [ "cpu": [
"x64" "x64"
], ],
@ -2983,11 +2983,11 @@
"integrity": "sha512-OWND8ei3VtNC9h7V60qff3SVobHr996CTwgxubgyQYEpg290h9J0buyECNNJexkFm5sOajh5G116RYA1c8ZMSw==" "integrity": "sha512-OWND8ei3VtNC9h7V60qff3SVobHr996CTwgxubgyQYEpg290h9J0buyECNNJexkFm5sOajh5G116RYA1c8ZMSw=="
}, },
"node_modules/next": { "node_modules/next": {
"version": "13.4.7", "version": "13.4.9",
"resolved": "https://registry.npmjs.org/next/-/next-13.4.7.tgz", "resolved": "https://registry.npmjs.org/next/-/next-13.4.9.tgz",
"integrity": "sha512-M8z3k9VmG51SRT6v5uDKdJXcAqLzP3C+vaKfLIAM0Mhx1um1G7MDnO63+m52qPdZfrTFzMZNzfsgvm3ghuVHIQ==", "integrity": "sha512-vtefFm/BWIi/eWOqf1GsmKG3cjKw1k3LjuefKRcL3iiLl3zWzFdPG3as6xtxrGO6gwTzzaO1ktL4oiHt/uvTjA==",
"dependencies": { "dependencies": {
"@next/env": "13.4.7", "@next/env": "13.4.9",
"@swc/helpers": "0.5.1", "@swc/helpers": "0.5.1",
"busboy": "1.6.0", "busboy": "1.6.0",
"caniuse-lite": "^1.0.30001406", "caniuse-lite": "^1.0.30001406",
@ -3003,15 +3003,15 @@
"node": ">=16.8.0" "node": ">=16.8.0"
}, },
"optionalDependencies": { "optionalDependencies": {
"@next/swc-darwin-arm64": "13.4.7", "@next/swc-darwin-arm64": "13.4.9",
"@next/swc-darwin-x64": "13.4.7", "@next/swc-darwin-x64": "13.4.9",
"@next/swc-linux-arm64-gnu": "13.4.7", "@next/swc-linux-arm64-gnu": "13.4.9",
"@next/swc-linux-arm64-musl": "13.4.7", "@next/swc-linux-arm64-musl": "13.4.9",
"@next/swc-linux-x64-gnu": "13.4.7", "@next/swc-linux-x64-gnu": "13.4.9",
"@next/swc-linux-x64-musl": "13.4.7", "@next/swc-linux-x64-musl": "13.4.9",
"@next/swc-win32-arm64-msvc": "13.4.7", "@next/swc-win32-arm64-msvc": "13.4.9",
"@next/swc-win32-ia32-msvc": "13.4.7", "@next/swc-win32-ia32-msvc": "13.4.9",
"@next/swc-win32-x64-msvc": "13.4.7" "@next/swc-win32-x64-msvc": "13.4.9"
}, },
"peerDependencies": { "peerDependencies": {
"@opentelemetry/api": "^1.1.0", "@opentelemetry/api": "^1.1.0",

View file

@ -17,7 +17,7 @@
"encoding": "^0.1.13", "encoding": "^0.1.13",
"eslint": "8.44.0", "eslint": "8.44.0",
"eslint-config-next": "13.4.7", "eslint-config-next": "13.4.7",
"next": "13.4.7", "next": "13.4.9",
"postcss": "8.4.24", "postcss": "8.4.24",
"react": "18.2.0", "react": "18.2.0",
"react-dom": "18.2.0", "react-dom": "18.2.0",