Initial commit: Infrastructure host tracking app
build-and-push / build-and-push (push) Successful in 1m26s
build-and-push / build-and-push (push) Successful in 1m26s
Fastify + node:sqlite single-process app with vanilla JS UI for looking up hosts by hardware ID, hostname, or asset ID. Includes per-host network interface tracking, sites/rooms/server-types CRUD, Docker packaging, and a Gitea Actions workflow that runs tests then builds and pushes to gitea.thewrightserver.net/josh/infrastructure. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
import { schemas } from '../schemas.js';
|
||||
import { translateSqliteError } from '../sqlite-errors.js';
|
||||
|
||||
export default async function hostsRoutes(fastify) {
|
||||
const { db } = fastify;
|
||||
|
||||
fastify.get('/', {
|
||||
schema: {
|
||||
querystring: {
|
||||
type: 'object',
|
||||
properties: { q: { type: 'string' } },
|
||||
},
|
||||
response: {
|
||||
200: { type: 'array', items: schemas.hostResponse },
|
||||
},
|
||||
},
|
||||
}, async (req) => {
|
||||
const q = (req.query.q ?? '').trim();
|
||||
return q ? db.hosts.search(q) : db.hosts.list();
|
||||
});
|
||||
|
||||
fastify.get('/by-hardware-id/:hardwareId', {
|
||||
schema: {
|
||||
params: {
|
||||
type: 'object',
|
||||
required: ['hardwareId'],
|
||||
properties: { hardwareId: { type: 'string', minLength: 1 } },
|
||||
},
|
||||
response: { 200: schemas.hostResponse, 404: schemas.errorResponse },
|
||||
},
|
||||
}, async (req) => {
|
||||
const host = db.hosts.getByHardwareId(req.params.hardwareId);
|
||||
if (!host) throw fastify.httpErrors.notFound('host not found');
|
||||
return host;
|
||||
});
|
||||
|
||||
fastify.get('/:id', {
|
||||
schema: {
|
||||
params: schemas.idParam,
|
||||
response: { 200: schemas.hostResponse, 404: schemas.errorResponse },
|
||||
},
|
||||
}, async (req) => {
|
||||
const host = db.hosts.get(req.params.id);
|
||||
if (!host) throw fastify.httpErrors.notFound('host not found');
|
||||
return host;
|
||||
});
|
||||
|
||||
fastify.post('/', {
|
||||
schema: {
|
||||
body: schemas.hostBody,
|
||||
response: { 201: schemas.hostResponse },
|
||||
},
|
||||
}, async (req, reply) => {
|
||||
try {
|
||||
const host = db.hosts.create(req.body);
|
||||
reply.code(201);
|
||||
return host;
|
||||
} catch (err) {
|
||||
translateSqliteError(err, fastify);
|
||||
}
|
||||
});
|
||||
|
||||
fastify.put('/:id', {
|
||||
schema: {
|
||||
params: schemas.idParam,
|
||||
body: schemas.hostBody,
|
||||
response: { 200: schemas.hostResponse, 404: schemas.errorResponse },
|
||||
},
|
||||
}, async (req) => {
|
||||
try {
|
||||
const host = db.hosts.update(req.params.id, req.body);
|
||||
if (!host) throw fastify.httpErrors.notFound('host not found');
|
||||
return host;
|
||||
} catch (err) {
|
||||
if (err.statusCode) throw err;
|
||||
translateSqliteError(err, fastify);
|
||||
}
|
||||
});
|
||||
|
||||
fastify.delete('/:id', {
|
||||
schema: { params: schemas.idParam },
|
||||
}, async (req, reply) => {
|
||||
const removed = db.hosts.delete(req.params.id);
|
||||
if (!removed) throw fastify.httpErrors.notFound('host not found');
|
||||
reply.code(204);
|
||||
return null;
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user