A Docker-like CLI and HTTP API for managing headless VMs

run format

+25 -32
+25 -32
src/api/machines.ts
··· 35 }> {} 36 37 export class RemoveRunningVmError extends Data.TaggedError( 38 - "RemoveRunningVmError" 39 )<{ 40 id: string; 41 }> {} ··· 49 Effect.flatMap((params) => 50 listInstances(params.all === "true" || params.all === "1") 51 ), 52 - presentation(c) 53 - ) 54 - ) 55 - ); 56 57 app.post("/", (c) => 58 Effect.runPromise( ··· 63 const image = yield* getImage(params.image); 64 if (!image) { 65 return yield* Effect.fail( 66 - new ImageNotFoundError({ id: params.image }) 67 ); 68 } 69 ··· 97 }) 98 ), 99 presentation(c), 100 - Effect.catchAll((error) => handleError(error, c)) 101 - ) 102 - ) 103 - ); 104 105 app.get("/:id", (c) => 106 Effect.runPromise( 107 pipe( 108 parseParams(c), 109 Effect.flatMap(({ id }) => getInstanceState(id)), 110 - presentation(c) 111 - ) 112 - ) 113 - ); 114 115 app.delete("/:id", (c) => 116 Effect.runPromise( ··· 129 }) 130 ), 131 presentation(c), 132 - Effect.catchAll((error) => handleError(error, c)) 133 - ) 134 - ) 135 - ); 136 137 app.post("/:id/start", (c) => 138 Effect.runPromise( ··· 157 ? startRequest.portForward.join(",") 158 : vm.portForward, 159 }, 160 - firmwareArgs 161 ); 162 yield* createLogsDir(); 163 yield* startDetachedQemu(vm.id, vm, qemuArgs); ··· 165 }) 166 ), 167 presentation(c), 168 - Effect.catchAll((error) => handleError(error, c)) 169 - ) 170 - ) 171 - ); 172 173 app.post("/:id/stop", (c) => 174 Effect.runPromise( ··· 178 Effect.flatMap(killProcess), 179 Effect.flatMap(updateToStopped), 180 presentation(c), 181 - Effect.catchAll((error) => handleError(error, c)) 182 - ) 183 - ) 184 - ); 185 186 app.post("/:id/restart", (c) => 187 Effect.runPromise( ··· 208 ? startRequest.portForward.join(",") 209 : vm.portForward, 210 }, 211 - firmwareArgs 212 ); 213 yield* createLogsDir(); 214 yield* startDetachedQemu(vm.id, vm, qemuArgs); ··· 216 }) 217 ), 218 presentation(c), 219 - Effect.catchAll((error) => handleError(error, c)) 220 - ) 221 - ) 222 - ); 223 224 export default app;
··· 35 }> {} 36 37 export class RemoveRunningVmError extends Data.TaggedError( 38 + "RemoveRunningVmError", 39 )<{ 40 id: string; 41 }> {} ··· 49 Effect.flatMap((params) => 50 listInstances(params.all === "true" || params.all === "1") 51 ), 52 + presentation(c), 53 + ), 54 + )); 55 56 app.post("/", (c) => 57 Effect.runPromise( ··· 62 const image = yield* getImage(params.image); 63 if (!image) { 64 return yield* Effect.fail( 65 + new ImageNotFoundError({ id: params.image }), 66 ); 67 } 68 ··· 96 }) 97 ), 98 presentation(c), 99 + Effect.catchAll((error) => handleError(error, c)), 100 + ), 101 + )); 102 103 app.get("/:id", (c) => 104 Effect.runPromise( 105 pipe( 106 parseParams(c), 107 Effect.flatMap(({ id }) => getInstanceState(id)), 108 + presentation(c), 109 + ), 110 + )); 111 112 app.delete("/:id", (c) => 113 Effect.runPromise( ··· 126 }) 127 ), 128 presentation(c), 129 + Effect.catchAll((error) => handleError(error, c)), 130 + ), 131 + )); 132 133 app.post("/:id/start", (c) => 134 Effect.runPromise( ··· 153 ? startRequest.portForward.join(",") 154 : vm.portForward, 155 }, 156 + firmwareArgs, 157 ); 158 yield* createLogsDir(); 159 yield* startDetachedQemu(vm.id, vm, qemuArgs); ··· 161 }) 162 ), 163 presentation(c), 164 + Effect.catchAll((error) => handleError(error, c)), 165 + ), 166 + )); 167 168 app.post("/:id/stop", (c) => 169 Effect.runPromise( ··· 173 Effect.flatMap(killProcess), 174 Effect.flatMap(updateToStopped), 175 presentation(c), 176 + Effect.catchAll((error) => handleError(error, c)), 177 + ), 178 + )); 179 180 app.post("/:id/restart", (c) => 181 Effect.runPromise( ··· 202 ? startRequest.portForward.join(",") 203 : vm.portForward, 204 }, 205 + firmwareArgs, 206 ); 207 yield* createLogsDir(); 208 yield* startDetachedQemu(vm.id, vm, qemuArgs); ··· 210 }) 211 ), 212 presentation(c), 213 + Effect.catchAll((error) => handleError(error, c)), 214 + ), 215 + )); 216 217 export default app;