-
-
Notifications
You must be signed in to change notification settings - Fork 45
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
Update code #133
Open
Sup3820
wants to merge
3
commits into
Prince25:main
Choose a base branch
from
Sup3820:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Update code #133
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,108 +1,62 @@ | ||
import { toConsole } from "../src/utils/log.js"; | ||
|
||
toConsole("setup", "Setting up server..."); | ||
|
||
// Support require | ||
import { createRequire } from "module"; | ||
const require = createRequire(import.meta.url); | ||
|
||
// Attempt to read .env file | ||
// If it doesn't exist, create an .env file with example.env information | ||
toConsole("setup", "Looking for .env file..."); | ||
const fs = require("fs"); | ||
var firstRun = true; | ||
function readEnvironmentFile(firstRun) { | ||
let environmentFile = ""; | ||
try { | ||
environmentFile = fs.readFileSync("config/.env", { encoding: "utf8", flag: "r" }); | ||
if (environmentFile == "") throw new Error(".env file empty!"); | ||
if (firstRun) toConsole("info", ".env file found! Attempting to read..."); | ||
} catch { | ||
if (firstRun) toConsole("info", ".env file not found! Creating a new one..."); | ||
environmentFile = fs.readFileSync("config/example.env", { encoding: "utf8", flag: "r" }); | ||
fs.writeFileSync("config/.env", environmentFile); | ||
} | ||
return environmentFile; | ||
} | ||
readEnvironmentFile(firstRun); | ||
firstRun = false; | ||
|
||
// Import stuff | ||
toConsole("setup", "Importing important stuff..."); | ||
const { parse, stringify } = require("envfile"); | ||
var open = require("open"); | ||
var express = require("express"); | ||
var path = require("path"); | ||
var cors = require("cors"); | ||
var app = express(); | ||
import { readFileSync, writeFileSync } from "fs"; | ||
import express from "express"; | ||
import { parse, stringify } from "envfile"; | ||
import { resolve } from "path"; | ||
import open from "open"; | ||
|
||
// Setup express with CORS on port 3250 | ||
toConsole("setup", "Starting server..."); | ||
app.use(cors()); | ||
app.options("*", cors()); | ||
app.listen(3250, "0.0.0.0", listening); | ||
|
||
function listening() { | ||
toConsole("setup", "Server started!"); | ||
const require = createRequire(import.meta.url); | ||
const app = express(); | ||
const port = 3250; | ||
const envFile = "config/.env"; | ||
const exampleEnvFile = "config/example.env"; | ||
const configFile = "config/config.json"; | ||
|
||
// Attempt to read .env file, if it doesn't exist, create an .env file with example.env information | ||
let environmentFile = ""; | ||
try { | ||
environmentFile = readFileSync(envFile, { encoding: "utf8", flag: "r" }); | ||
if (environmentFile === "") throw new Error(".env file empty!"); | ||
toConsole("info", ".env file found! Attempting to read..."); | ||
} catch { | ||
toConsole("info", ".env file not found! Creating a new one..."); | ||
environmentFile = readFileSync(exampleEnvFile, { encoding: "utf8", flag: "r" }); | ||
writeFileSync(envFile, environmentFile); | ||
} | ||
|
||
app.use(express.static("public")); | ||
app.use(express.urlencoded({ extended: false })); | ||
app.use(express.json()); | ||
|
||
/* | ||
Setup routes | ||
*/ | ||
toConsole("setup", "Setting up routes..."); | ||
|
||
// index.html: https://localhost:3250/ | ||
app.get("/", getPage); | ||
function getPage(request, response) { | ||
response.sendFile(path.join(path.resolve() + "/server/index.html")); | ||
} | ||
|
||
// GET .env: https://localhost:3250/env | ||
app.get("/env", getEnvironment); | ||
function getEnvironment(request, response) { | ||
let environmentFile = readEnvironmentFile(firstRun); | ||
response.send(parse(environmentFile)); | ||
} | ||
|
||
// POST .env: https://localhost:3250/env | ||
app.post("/env", postEnvironment); | ||
function postEnvironment(request, response) { | ||
toConsole("info", "Settings received! Saving to .env..."); | ||
let environmentSettings = stringify(request.body); | ||
|
||
fs.writeFile("config/.env", environmentSettings, "utf8", function (error) { | ||
if (error) { | ||
response.status(400).send({ error: "Error writing .env" }); | ||
} else { | ||
response.send({ message: "Successfully saved .env" }); | ||
} | ||
}); | ||
} | ||
|
||
// GET config.json: https://localhost:3250/config | ||
app.get("/config", getSettings); | ||
function getSettings(request, response) { | ||
response.sendFile(path.join(path.resolve() + "/config/config.json")); | ||
} | ||
|
||
// POST config.json: https://localhost:3250/config | ||
app.post("/config", postSettings); | ||
function postSettings(request, response) { | ||
toConsole("info", "Settings received! Saving to config.json..."); | ||
let settings = JSON.stringify(request.body, undefined, 4); | ||
|
||
fs.writeFile("config/config.json", settings, "utf8", function (error) { | ||
if (error) { | ||
response.status(400).send({ error: "Error writing config.json" }); | ||
} else { | ||
response.send({ message: "Successfully saved config.json" }); | ||
} | ||
}); | ||
} | ||
|
||
toConsole("info", "Opening settings page on http://localhost:3250/..."); | ||
open("http://localhost:3250/"); | ||
// Setup routes | ||
app.get("/", (req, res) => { | ||
res.sendFile(resolve() + "/server/index.html"); | ||
}); | ||
|
||
app.get("/env", (req, res) => { | ||
res.send(parse(environmentFile)); | ||
}); | ||
|
||
app.post("/env", (req, res) => { | ||
const environmentSettings = stringify(req.body); | ||
writeFileSync(envFile, environmentSettings, "utf8"); | ||
res.send({ message: "Successfully saved .env" }); | ||
}); | ||
|
||
app.get("/config", (req, res) => { | ||
res.sendFile(resolve() + "/config/config.json"); | ||
}); | ||
|
||
app.post("/config", (req, res) => { | ||
const settings = JSON.stringify(req.body, undefined, 4); | ||
writeFileSync(configFile, settings, "utf8"); | ||
res.send({ message: "Successfully saved config.json" }); | ||
}); | ||
|
||
// Start server | ||
app.listen(port, "0.0.0.0", () => { | ||
toConsole("setup", `Server started on port ${port}`); | ||
toConsole("info", `Opening settings page on http://localhost:${port}/...`); | ||
open(`http://localhost:${port}/`); | ||
}); |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please tell me whether this makes any difference or the reason this was changed.