Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an optional flag to use https on macos. #107

Merged
merged 2 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions FAQ.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,8 @@ for http in such a case.
To get around this, tunnel the 8998 port from the remote server to the localhost
via ssh and access [localhost:8998](http://localhost:8998) via http normally
after that.

### How to get the key.pem and cert.pem files required for serving over https?
```bash
openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -days 365 -nodes -subj "/CN=localhost"
```
27 changes: 23 additions & 4 deletions moshi_mlx/moshi_mlx/local_web.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,14 +332,25 @@ async def handle_root(_):
log("info", f"serving static content from {static_path}")
app.router.add_get("/", handle_root)
app.router.add_static("/", path=static_path, name="static")
log("info", f"listening to http://{args.host}:{args.port}")
runner = web.AppRunner(app)
await runner.setup()
site = web.TCPSite(runner, args.host, args.port)
ssl_context = None
protocol = "http"
if args.ssl is not None:
import ssl

ssl_context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
cert_file = os.path.join(args.ssl, "cert.pem")
key_file = os.path.join(args.ssl, "key.pem")
ssl_context.load_cert_chain(certfile=cert_file, keyfile=key_file)
protocol = "https"
site = web.TCPSite(runner, args.host, args.port, ssl_context=ssl_context)

log("info", f"listening to {protocol}://{args.host}:{args.port}")

if not args.no_browser:
log("info", f"opening browser at http://{args.host}:{args.port}")
webbrowser.open(f"http://{args.host}:{args.port}")
log("info", f"opening browser at {protocol}://{args.host}:{args.port}")
webbrowser.open(f"{protocol}://{args.host}:{args.port}")

await asyncio.gather(
recv_loop(), send_loop(), recv_loop2(), send_loop2(), site.start()
Expand All @@ -363,6 +374,14 @@ def main():
parser.add_argument("--static", type=str)
parser.add_argument("--host", default="localhost", type=str)
parser.add_argument("--port", default=8998, type=int)
parser.add_argument(
"--ssl",
type=str,
help=(
"use https instead of http, this flag should point to a directory "
"that contains valid key.pem and cert.pem files"
)
)
parser.add_argument("--no-browser", action="store_true")

args = parser.parse_args()
Expand Down
6 changes: 6 additions & 0 deletions rust/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ maturin dev -r -m rust/mimi-pyo3/Cargo.toml

## Rust server

If you don't have ssl certificates yet, generate a `key.pem` and `cert.pem` file
using the following command.
```bash
openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -days 365 -nodes -subj "/CN=localhost"
```

In order to run the rust inference server, use the following command from within
the this directory:

Expand Down
Loading