|
| 1 | +# SPDX-License-Identifier: PMPL-1.0-or-later |
| 2 | +defmodule Opsm.Api.MobileRouter do |
| 3 | + @moduledoc """ |
| 4 | + REST API endpoints for OPSM mobile application. |
| 5 | + """ |
| 6 | + |
| 7 | + use Plug.Router |
| 8 | + |
| 9 | + plug Plug.Logger |
| 10 | + |
| 11 | + plug Plug.Parsers, |
| 12 | + parsers: [:json], |
| 13 | + json_decoder: Jason |
| 14 | + |
| 15 | + plug :match |
| 16 | + plug :dispatch |
| 17 | + |
| 18 | + get "/api/health" do |
| 19 | + send_json(conn, 200, %{ |
| 20 | + status: "healthy", |
| 21 | + version: "1.0.1", |
| 22 | + service: "opsm-api" |
| 23 | + }) |
| 24 | + end |
| 25 | + |
| 26 | + get "/api/packages/search" do |
| 27 | + query = conn.query_params["q"] || "" |
| 28 | + registry = conn.query_params["registry"] |
| 29 | + |
| 30 | + # Mock search results for testing |
| 31 | + packages = [ |
| 32 | + %{ |
| 33 | + name: "react", |
| 34 | + version: "18.2.0", |
| 35 | + registry: "npm", |
| 36 | + description: "A JavaScript library for building user interfaces" |
| 37 | + }, |
| 38 | + %{ |
| 39 | + name: "vue", |
| 40 | + version: "3.3.4", |
| 41 | + registry: "npm", |
| 42 | + description: "Progressive JavaScript Framework" |
| 43 | + }, |
| 44 | + %{ |
| 45 | + name: "axum", |
| 46 | + version: "0.7.3", |
| 47 | + registry: "crates", |
| 48 | + description: "Web framework for Rust" |
| 49 | + } |
| 50 | + ] |
| 51 | + |> Enum.filter(fn pkg -> |
| 52 | + String.contains?(String.downcase(pkg.name), String.downcase(query)) |
| 53 | + end) |
| 54 | + |> Enum.filter(fn pkg -> |
| 55 | + is_nil(registry) || pkg.registry == registry |
| 56 | + end) |
| 57 | + |
| 58 | + send_json(conn, 200, %{ |
| 59 | + packages: packages, |
| 60 | + total: length(packages) |
| 61 | + }) |
| 62 | + end |
| 63 | + |
| 64 | + get "/api/packages/:name/:version" do |
| 65 | + send_json(conn, 200, %{ |
| 66 | + name: name, |
| 67 | + version: version, |
| 68 | + registry: "npm", |
| 69 | + description: "Package information for #{name}@#{version}", |
| 70 | + dependencies: %{}, |
| 71 | + metadata: %{ |
| 72 | + license: "MIT", |
| 73 | + homepage: "https://example.com" |
| 74 | + } |
| 75 | + }) |
| 76 | + end |
| 77 | + |
| 78 | + post "/api/packages/install" do |
| 79 | + payload = conn.body_params |
| 80 | + |
| 81 | + send_json(conn, 200, %{ |
| 82 | + success: true, |
| 83 | + message: "Installed #{payload["name"]}@#{payload["version"]} from #{payload["registry"]}" |
| 84 | + }) |
| 85 | + end |
| 86 | + |
| 87 | + get "/api/packages/installed" do |
| 88 | + # Mock installed packages |
| 89 | + packages = [ |
| 90 | + %{ |
| 91 | + name: "lodash", |
| 92 | + version: "4.17.21", |
| 93 | + registry: "npm", |
| 94 | + description: "Lodash modular utilities" |
| 95 | + }, |
| 96 | + %{ |
| 97 | + name: "axios", |
| 98 | + version: "1.6.0", |
| 99 | + registry: "npm", |
| 100 | + description: "Promise based HTTP client" |
| 101 | + } |
| 102 | + ] |
| 103 | + |
| 104 | + send_json(conn, 200, packages) |
| 105 | + end |
| 106 | + |
| 107 | + post "/api/audit/lockfile" do |
| 108 | + payload = conn.body_params |
| 109 | + |
| 110 | + send_json(conn, 200, %{ |
| 111 | + issues: [ |
| 112 | + %{ |
| 113 | + severity: "medium", |
| 114 | + package: "lodash", |
| 115 | + description: "Prototype pollution vulnerability" |
| 116 | + } |
| 117 | + ], |
| 118 | + sustainability_score: 85.5, |
| 119 | + security_score: 72.3 |
| 120 | + }) |
| 121 | + end |
| 122 | + |
| 123 | + match _ do |
| 124 | + send_json(conn, 404, %{error: "not found"}) |
| 125 | + end |
| 126 | + |
| 127 | + defp send_json(conn, status, data) do |
| 128 | + body = Jason.encode!(data) |
| 129 | + conn |
| 130 | + |> Plug.Conn.put_resp_content_type("application/json") |
| 131 | + |> Plug.Conn.send_resp(status, body) |
| 132 | + end |
| 133 | +end |
0 commit comments