route.ts 1.18 KB
Newer Older
Jeffrey Morgan's avatar
Jeffrey Morgan committed
1
2
3
4
5
6
7
8
9
10
11
12
13
import { NextResponse } from 'next/server'
import semver from 'semver'
import { Octokit } from '@octokit/rest'
import { RequestError } from '@octokit/types'

const octokit = new Octokit()

export async function GET(req: Request) {
  const { searchParams } = new URL(req.url)

  const os = searchParams.get('os') || ''
  const version = searchParams.get('version') || ''

Jeffrey Morgan's avatar
Jeffrey Morgan committed
14
15
16
17
  if (!version) {
    return new Response('not found', { status: 404 })
  }

Jeffrey Morgan's avatar
Jeffrey Morgan committed
18
19
20
21
22
23
24
25
26
27
  try {
    const { data } = await octokit.repos.getLatestRelease({
      owner: 'jmorganca',
      repo: 'ollama',
    })

    // todo: get the correct asset for the current arch/os
    const asset = data.assets.find(a => a.name.toLowerCase().includes(os))

    if (!asset) {
Jeffrey Morgan's avatar
Jeffrey Morgan committed
28
      return new Response('not found', { status: 404 })
Jeffrey Morgan's avatar
Jeffrey Morgan committed
29
30
31
32
33
    }

    if (semver.lt(version, data.tag_name)) {
      return NextResponse.json({ version: data.tag_name, url: asset.browser_download_url })
    }
Jeffrey Morgan's avatar
Jeffrey Morgan committed
34
35

    return new Response('up to date', { status: 204 })
Jeffrey Morgan's avatar
Jeffrey Morgan committed
36
37
38
39
40
41
42
43
44
  } 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 })
  }
}