Skip to content

Commit

Permalink
Stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
ConProgramming committed Feb 15, 2023
1 parent c2cf7d8 commit 47131dd
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 21 deletions.
15 changes: 7 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,13 @@ Write Python endpoints in [SvelteKit](https://kit.svelte.dev/) using [Modal](htt
- Create `sveltekit_modal_config.py`. The option `stub_asgi` is passed to [Modal](https://modal.com/docs/reference/modal.Stub#asgi). This is where you can define GPU acceleration, secrets, and an Image for pip installs, etc. Explore their [docs](https://modal.com/docs/guide)!
```python
import modal
import sveltekit_modal
config: sveltekit_modal.Config = {
'name': 'sveltekit-example',
'log': False,
'stub_asgi': {
'image': modal.Image.debian_slim()
}
config = {
'name': 'sveltekit-example',
'log': False,
'stub_asgi': {
'image': modal.Image.debian_slim()
}
}
```
- Update `.gitignore`, add `!.env.production`.
Expand Down
2 changes: 1 addition & 1 deletion src/bin.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ switch (cmd) {

cd(path.join('.', 'node_modules', 'sveltekit-modal', 'esm/src/vite'))

const deploy_log = await $`python3 sveltekit_modal_deploy.py`;
const deploy_log = await $`python3 -m sveltekit_modal.deploy`;
const modal_route_match = deploy_log.stdout.match(/https:\/\/.*?\.modal\.run/)?.toString();

cd(path.join('..', '..', '..', '../../..'))
Expand Down
14 changes: 4 additions & 10 deletions src/vite/mod.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { loadEnv, type Plugin } from 'vite'
import { type ProcessPromise, $ as run$, cd as cd$, fs, path, globby, chalk } from "zx";
import { type ProcessPromise, $ as run$, cd as cd$, chalk } from "zx";
import globsync from "rollup-plugin-globsync";

const get_pyServerEndpointAsString = (modal_url: URL) => `
Expand Down Expand Up @@ -42,6 +42,8 @@ export async function sveltekit_modal(): Promise<Plugin[]> {
'!./node_modules/sveltekit-modal/esm/src/vite/sveltekit_modal/app.py',
'!./node_modules/sveltekit-modal/esm/src/vite/sveltekit_modal/config.py',
'!./node_modules/sveltekit-modal/esm/src/vite/sveltekit_modal/pyproject.toml',
'!./node_modules/sveltekit-modal/esm/src/vite/sveltekit_modal/deploy.py',
'!./node_modules/sveltekit-modal/esm/src/vite/sveltekit_modal/server.py',
]
});

Expand All @@ -52,20 +54,12 @@ export async function sveltekit_modal(): Promise<Plugin[]> {
await kill_all_process();
},
async configureServer({ config }) {
const packagelocation = path.join(config.root, 'node_modules', 'sveltekit-modal', 'esm/src/vite');

await fs.copy('sveltekit_modal_config.py', path.join(packagelocation, '/sveltekit_modal/sveltekit_modal_config.py'));

for (const file of await globby('src/routes/**/*.py')) {
await fs.copy(file, path.join(packagelocation, '/sveltekit_modal/', file));
}

run$.verbose = false;
run$.env.PYTHONDONTWRITEBYTECODE = '1';

cd$(packagelocation)

const local_process: ProcessPromise = run$`python3 sveltekit_modal_serve.py`;
const local_process: ProcessPromise = run$`python3 -m sveltekit_modal.serve`;
child_processes.push(local_process);

cd$(config.root)
Expand Down
22 changes: 20 additions & 2 deletions src/vite/sveltekit_modal/serve.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import sys
from .app import stub
import traceback

from synchronicity import Interface
from modal.cli.app import _show_stub_ref_failure_help
from modal_utils.async_utils import synchronizer
from modal_utils.package_utils import NoSuchStub, import_stub, parse_stub_ref

from .sveltekit_modal_config import config

from watchfiles import DefaultFilter
Expand Down Expand Up @@ -39,4 +45,16 @@ def flush(self):


if __name__ == '__main__':
stub.serve(stdout=Logger(sys.stdout), show_progress=True)
parsed_stub_ref = parse_stub_ref('sveltekit_modal.app')
try:
stub = import_stub(parsed_stub_ref)
except NoSuchStub:
_show_stub_ref_failure_help(parsed_stub_ref)
sys.exit(1)
except Exception:
traceback.print_exc()
sys.exit(1)

_stub = synchronizer._translate_in(stub)
blocking_stub = synchronizer._translate_out(_stub, Interface.BLOCKING)
blocking_stub.serve(stdout=Logger(sys.stdout), show_progress=True)

0 comments on commit 47131dd

Please sign in to comment.