-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a42e768
commit 6d2da35
Showing
5 changed files
with
153 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import os | ||
import subprocess | ||
import sys | ||
from pathlib import Path | ||
|
||
def get_viewer_path(): | ||
return str(Path(__file__).parent) | ||
|
||
def start_nextjs_server(): | ||
viewer_path = get_viewer_path() | ||
static_dir = os.path.join(viewer_path, 'static') | ||
server_file = os.path.join(viewer_path, 'server.js') | ||
|
||
if not os.path.exists(os.path.join(static_dir, '.next')): | ||
print("Error: Next.js build artifacts not found. The package may not be built correctly.") | ||
sys.exit(1) | ||
|
||
try: | ||
# Run the server.js directly with node | ||
env = os.environ.copy() | ||
env["NODE_ENV"] = "production" | ||
|
||
subprocess.run( | ||
["node", server_file], | ||
cwd=viewer_path, | ||
env=env, | ||
check=True | ||
) | ||
except subprocess.CalledProcessError as e: | ||
print(f"Error starting Next.js server: {e}") | ||
sys.exit(1) | ||
except FileNotFoundError: | ||
print("Error: Node.js is not installed. Please install Node.js to run the viewer.") | ||
sys.exit(1) | ||
|
||
def main(): | ||
start_nextjs_server() | ||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
const { createServer } = require('http'); | ||
const { parse } = require('url'); | ||
const path = require('path'); | ||
|
||
// Get paths | ||
const staticDir = path.join(__dirname, 'static'); | ||
// Add node_modules to Node's module search path | ||
process.env.NODE_PATH = path.join(staticDir, 'node_modules'); | ||
require('module').Module._initPaths(); | ||
|
||
const next = require(path.join(staticDir, 'node_modules/next')); | ||
|
||
const dev = process.env.NODE_ENV !== 'production'; | ||
const hostname = 'localhost'; | ||
const port = 3000; | ||
|
||
// Configure Next.js | ||
const app = next({ | ||
dev, | ||
dir: staticDir, | ||
hostname, | ||
port | ||
}); | ||
|
||
const handle = app.getRequestHandler(); | ||
|
||
app.prepare().then(() => { | ||
createServer(async (req, res) => { | ||
try { | ||
const parsedUrl = parse(req.url, true); | ||
await handle(req, res, parsedUrl); | ||
} catch (err) { | ||
console.error('Error occurred handling', req.url, err); | ||
res.statusCode = 500; | ||
res.end('internal server error'); | ||
} | ||
}) | ||
.once('error', (err) => { | ||
console.error(err); | ||
process.exit(1); | ||
}) | ||
.listen(port, () => { | ||
console.log(`> Ready on http://${hostname}:${port}`); | ||
}); | ||
}); |