|
| 1 | +/* Copyright 2023 Google LLC. All Rights Reserved. |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License. |
| 14 | +==============================================================================*/ |
| 15 | + |
| 16 | +import { createHTTPServer } from '@trpc/server/adapters/standalone'; |
| 17 | +import { z } from 'zod'; |
| 18 | +import { publicProcedure, router } from './trpc.ts'; |
| 19 | + |
| 20 | +import { parseArgs } from 'jsr:@std/cli/parse-args'; |
| 21 | +const flags = parseArgs(Deno.args, { |
| 22 | + // boolean: ['help', 'color'], |
| 23 | + string: ['port', 'pathToExp'], |
| 24 | + default: { port: '9000' }, |
| 25 | + // negatable: ['color'], |
| 26 | +}); |
| 27 | + |
| 28 | +if (!flags.pathToExp) { |
| 29 | + throw new Error('--pathToExp must be defined.'); |
| 30 | +} |
| 31 | + |
| 32 | +Deno.chdir(flags.pathToExp); |
| 33 | +console.log(`running in dir: ${Deno.cwd()}`); |
| 34 | +console.log(`URL: http://localhost:${flags.port}/`); |
| 35 | + |
| 36 | +const appRouter = router({ |
| 37 | + curPath: publicProcedure.query(async () => { |
| 38 | + return Deno.cwd(); |
| 39 | + }), |
| 40 | + loadArray: publicProcedure.input(z.string()).query(async (opts) => { |
| 41 | + const subPath = opts.input; |
| 42 | + const uintArr = await Deno.readFile(subPath); |
| 43 | + return uintArr; |
| 44 | + }), |
| 45 | + loadStr: publicProcedure.input(z.string()).query(async (opts) => { |
| 46 | + const subPath = opts.input; |
| 47 | + const str = await Deno.readTextFile(subPath); |
| 48 | + return str; |
| 49 | + }), |
| 50 | + saveStr: publicProcedure |
| 51 | + .input(z.object({ path: z.string(), str: z.string(), force: z.boolean() })) |
| 52 | + .mutation(async (opts) => { |
| 53 | + await Deno.writeTextFile(opts.input.path, opts.input.str, { |
| 54 | + createNew: !opts.input.force, |
| 55 | + }); |
| 56 | + return; |
| 57 | + }), |
| 58 | +}); |
| 59 | + |
| 60 | +export type ExperimentServerRouter = typeof appRouter; |
| 61 | + |
| 62 | +const server = createHTTPServer({ |
| 63 | + router: appRouter, |
| 64 | +}); |
| 65 | + |
| 66 | +server.listen(flags.port); |
0 commit comments