Javascript
This commit is contained in:
9
index.js
9
index.js
@@ -1,9 +0,0 @@
|
||||
import path from 'path';
|
||||
import { fileURLToPath } from 'url';
|
||||
const __filename = fileURLToPath(import.meta.url);
|
||||
const __dirname = path.dirname(__filename);
|
||||
|
||||
const modulePath = process.env.QUILL_PATH;
|
||||
const { default: Server } = await import(modulePath);
|
||||
|
||||
const server = new Server(__dirname, 3003);
|
||||
25
package.json
Normal file
25
package.json
Normal file
@@ -0,0 +1,25 @@
|
||||
{
|
||||
"name": "Hyperia",
|
||||
"type": "module",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"start": "node server/index.js",
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"argon2": "^0.44.0",
|
||||
"chalk": "^5.6.2",
|
||||
"cookie-parser": "^1.4.7",
|
||||
"cors": "^2.8.5",
|
||||
"dotenv": "^17.2.3",
|
||||
"express": "^4.18.2",
|
||||
"http-proxy": "^1.18.1",
|
||||
"jsonwebtoken": "^9.0.2",
|
||||
"moment": "^2.30.1",
|
||||
"ws": "^8.18.3"
|
||||
}
|
||||
}
|
||||
110
server/_/quilldb.js
Normal file
110
server/_/quilldb.js
Normal file
@@ -0,0 +1,110 @@
|
||||
import chalk from 'chalk'
|
||||
import path from 'path';
|
||||
import fs from 'fs/promises';
|
||||
import { pathToFileURL } from 'url';
|
||||
|
||||
function Node(node) {
|
||||
let traits = [
|
||||
"labels"
|
||||
]
|
||||
for(let i = 0; i < traits.length; i++) {
|
||||
if(!node[traits[i]]) {
|
||||
throw new Error(`Node is missing field "${traits[i]}": ${JSON.stringify(node)}`)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default class QuillDB {
|
||||
#nodes; get nodes() {return this.#nodes};
|
||||
#edges; get edges() {return this.#edges};
|
||||
#labels = {}; get labels() {return this.#labels}
|
||||
|
||||
constructor() {
|
||||
this.loadData()
|
||||
}
|
||||
|
||||
#checkLabelSchemas(id, entry, labelModels) {
|
||||
entry.labels.forEach(label => {
|
||||
const model = labelModels[label];
|
||||
if (!model) {
|
||||
throw new Error("Data has unknown label or missing model: " + label)
|
||||
}
|
||||
model(entry);
|
||||
this.#labels[label].push(id);
|
||||
});
|
||||
}
|
||||
|
||||
async getLabelModels() {
|
||||
const labelHandlers = {};
|
||||
const labelDir = path.join(process.cwd(), 'server/db/model');
|
||||
const files = await fs.readdir(labelDir);
|
||||
|
||||
for (const file of files) {
|
||||
if (!file.endsWith('.js')) continue;
|
||||
|
||||
const label = path.basename(file, '.js');
|
||||
const modulePath = path.join(labelDir, file);
|
||||
const module = await import(pathToFileURL(modulePath).href);
|
||||
labelHandlers[label] = module.default;
|
||||
|
||||
if (!this.#labels[label]) {
|
||||
this.#labels[label] = [];
|
||||
}
|
||||
}
|
||||
return labelHandlers
|
||||
}
|
||||
|
||||
async loadData() {
|
||||
const dbData = await fs.readFile(path.join(process.cwd(), 'db/db.json'), 'utf8');
|
||||
let dbJson;
|
||||
try {
|
||||
dbJson = JSON.parse(dbData);
|
||||
} catch {
|
||||
dbJson = []
|
||||
}
|
||||
this.#nodes = dbJson["nodes"];
|
||||
this.#edges = dbJson["edges"];
|
||||
|
||||
let labelModels = await this.getLabelModels();
|
||||
|
||||
// Index by label
|
||||
for (const [id, entry] of Object.entries(this.#nodes)) {
|
||||
Node(entry)
|
||||
this.#checkLabelSchemas(id, entry, labelModels)
|
||||
}
|
||||
for (const [id, entry] of Object.entries(this.#edges)) {
|
||||
Edge(entry)
|
||||
this.#checkLabelSchemas(id, entry, labelModels)
|
||||
}
|
||||
|
||||
console.log(chalk.yellow("DB established."))
|
||||
Object.preventExtensions(this);
|
||||
}
|
||||
|
||||
// superKey = "nodes" || "edges"
|
||||
async writeData(superKey, key, value) {
|
||||
const dbData = await fs.readFile(path.join(process.cwd(), 'db/db.json'), 'utf8');
|
||||
let dbJson;
|
||||
try {
|
||||
dbJson = JSON.parse(dbData);
|
||||
} catch {
|
||||
dbJson = []
|
||||
}
|
||||
|
||||
dbJson[superKey][key] = value;
|
||||
|
||||
await fs.writeFile(path.join(process.cwd(), 'db/db.json'), JSON.stringify(dbJson, null, 2), 'utf8')
|
||||
}
|
||||
|
||||
generateUserID() {
|
||||
let id = this.#labels["User"].length + 1;
|
||||
while (this.get.user(`user-${id}`)) {
|
||||
id++;
|
||||
}
|
||||
return `user-${id}`; // O(1) most of the time
|
||||
}
|
||||
|
||||
async getAll() {
|
||||
return { nodes: this.#nodes }
|
||||
}
|
||||
}
|
||||
71
server/auth.js
Normal file
71
server/auth.js
Normal file
@@ -0,0 +1,71 @@
|
||||
import dotenv from 'dotenv';
|
||||
import chalk from 'chalk';
|
||||
import jwt from 'jsonwebtoken'
|
||||
import argon2 from 'argon2'
|
||||
import { randomUUID } from 'node:crypto'
|
||||
dotenv.config();
|
||||
|
||||
export default class AuthHandler {
|
||||
ips = new Map()
|
||||
#secret
|
||||
|
||||
constructor() {
|
||||
this.#secret = process.env.JWT_SECRET;
|
||||
}
|
||||
|
||||
isLoggedInUser(req, res) {
|
||||
const token = req.cookies.auth_token; // read cookie
|
||||
|
||||
if (!token) {
|
||||
return false
|
||||
}
|
||||
|
||||
try {
|
||||
return true
|
||||
} catch (err) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
async login(req, res) {
|
||||
const { email, password } = req.body;
|
||||
let foundUser = global.db.get.userByEmail(email)
|
||||
if(!foundUser) {
|
||||
res.status(400).json({ error: 'Incorrect email.' });
|
||||
return;
|
||||
}
|
||||
const storedHash = foundUser.password
|
||||
const valid = await argon2.verify(storedHash, password);
|
||||
if (!valid) {
|
||||
res.status(400).json({ error: 'Incorrect password.' });
|
||||
} else {
|
||||
const payload = { id: foundUser.id };
|
||||
const secret = process.env.JWT_SECRET;
|
||||
const options = { expiresIn: "2h" };
|
||||
const token = jwt.sign(payload, secret, options);
|
||||
|
||||
res.cookie("auth_token", token, {
|
||||
httpOnly: true, // cannot be accessed by JS
|
||||
secure: process.env.ENV === "production", // only over HTTPS
|
||||
sameSite: "lax", // like SameSiteLaxMode
|
||||
maxAge: 2 * 60 * 60 * 1000, // 2 hours in milliseconds
|
||||
path: "/", // available on entire site
|
||||
domain: process.env.ENV === "production" ? "." + process.env.BASE_URL : undefined
|
||||
});
|
||||
|
||||
res.redirect("/")
|
||||
}
|
||||
}
|
||||
|
||||
logout(req, res) {
|
||||
res.cookie('auth_token', '', {
|
||||
httpOnly: true,
|
||||
secure: process.env.NODE_ENV === 'production',
|
||||
sameSite: 'lax',
|
||||
maxAge: 0, // expire immediately
|
||||
path: '/',
|
||||
});
|
||||
|
||||
res.redirect("/")
|
||||
}
|
||||
}
|
||||
@@ -1,51 +0,0 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
)
|
||||
|
||||
var ENV string
|
||||
|
||||
// URLs
|
||||
var BASE_URL string
|
||||
const PORT = "3003"
|
||||
|
||||
// Auth
|
||||
var JWT_SECRET string
|
||||
|
||||
// Logging
|
||||
var LOG_TO_FILE bool
|
||||
|
||||
func SetConfiguration() {
|
||||
err := godotenv.Load()
|
||||
if err != nil {
|
||||
fmt.Println("no .env file found. Needs to be added to server directory.")
|
||||
}
|
||||
|
||||
ENV = os.Getenv("ENV")
|
||||
if ENV != "production" && ENV != "development" {
|
||||
fmt.Println("invalid value for ENV, must be 'development' or 'production'")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
BASE_URL = os.Getenv("BASE_URL")
|
||||
if BASE_URL == "" {
|
||||
fmt.Println("BASE_URL not provided, aborting")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
JWT_SECRET = os.Getenv("JWT_SECRET")
|
||||
if JWT_SECRET == "" {
|
||||
fmt.Println("JWT_SECRET not provided, aborting")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
LOG_TO_FILE, err = strconv.ParseBool(os.Getenv("LOG_TO_FILE"))
|
||||
if err != nil {
|
||||
LOG_TO_FILE = false
|
||||
}
|
||||
}
|
||||
@@ -1,58 +0,0 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
// "github.com/alexedwards/argon2id"
|
||||
)
|
||||
|
||||
type User struct {
|
||||
Email string `json:"email"`
|
||||
Password string `json:"password"`
|
||||
// Other fields as needed
|
||||
}
|
||||
|
||||
var DB map[string]User
|
||||
|
||||
type GetService struct{}
|
||||
var Get = GetService{}
|
||||
|
||||
func (g GetService) UserByEmail(email string) (map[string]interface{}, error) {
|
||||
for key, value := range DB {
|
||||
if value.Email == email {
|
||||
log.Println("found")
|
||||
return map[string]interface{}{
|
||||
"key": key,
|
||||
"email": value.Email,
|
||||
"password": value.Password,
|
||||
}, nil
|
||||
}
|
||||
fmt.Printf("Key: %s, Value: %v\n", key, value)
|
||||
}
|
||||
|
||||
return nil, errors.New("user not found")
|
||||
}
|
||||
|
||||
func InitDB() error {
|
||||
file, err := os.Open("../db/users.json")
|
||||
if err != nil {
|
||||
fmt.Println("Error opening file:", err)
|
||||
return errors.New("Failed to read db")
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
var result map[string]User
|
||||
err = json.NewDecoder(file).Decode(&result)
|
||||
if err != nil {
|
||||
fmt.Println("Error decoding JSON:", err)
|
||||
return errors.New("failed to decode db")
|
||||
}
|
||||
|
||||
DB = result
|
||||
|
||||
return nil
|
||||
}
|
||||
31
server/db/db.js
Normal file
31
server/db/db.js
Normal file
@@ -0,0 +1,31 @@
|
||||
import QuillDB from "../_/quilldb.js"
|
||||
|
||||
export default class Database extends QuillDB {
|
||||
|
||||
get = {
|
||||
user: (id) => {
|
||||
return this.nodes[id]
|
||||
},
|
||||
userByEmail: (email) => {
|
||||
for (const id of this.labels["User"]) {
|
||||
const user = this.get.user(id);
|
||||
if (user.email === email) {
|
||||
return { id, ...user }
|
||||
}
|
||||
}
|
||||
return null;
|
||||
},
|
||||
}
|
||||
|
||||
generateUserID() {
|
||||
let id = this.labels["User"].length + 1;
|
||||
while (this.get.user(`user-${id}`)) {
|
||||
id++;
|
||||
}
|
||||
return `user-${id}`; // O(1) most of the time
|
||||
}
|
||||
|
||||
async getAll() {
|
||||
return { nodes: this.nodes }
|
||||
}
|
||||
}
|
||||
14
server/db/model/User.js
Normal file
14
server/db/model/User.js
Normal file
@@ -0,0 +1,14 @@
|
||||
export default function User(node) {
|
||||
let traits = [
|
||||
"firstName",
|
||||
"lastName",
|
||||
"email",
|
||||
"password"
|
||||
]
|
||||
for(let i = 0; i < traits.length; i++) {
|
||||
if(!node[traits[i]]) {
|
||||
if (traits[i] == "lastName") { continue; } // Ignores optional Last Name
|
||||
throw new Error(`User ${node.email} is missing trait ${traits[i]}`)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,34 +0,0 @@
|
||||
module hyperia
|
||||
|
||||
go 1.24.0
|
||||
|
||||
toolchain go1.24.5
|
||||
|
||||
require (
|
||||
github.com/alexedwards/argon2id v1.0.0
|
||||
github.com/golang-jwt/jwt/v5 v5.3.0
|
||||
github.com/joho/godotenv v1.5.1
|
||||
github.com/lib/pq v1.10.9
|
||||
github.com/mailgun/mailgun-go/v4 v4.23.0
|
||||
github.com/mssola/user_agent v0.6.0
|
||||
github.com/rs/zerolog v1.34.0
|
||||
gopkg.in/natefinch/lumberjack.v2 v2.2.1
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/go-chi/chi/v5 v5.2.1 // indirect
|
||||
github.com/gorilla/websocket v1.5.3 // indirect
|
||||
github.com/json-iterator/go v1.1.12 // indirect
|
||||
github.com/mailgun/errors v0.4.0 // indirect
|
||||
github.com/mattn/go-colorable v0.1.14 // indirect
|
||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
|
||||
github.com/modern-go/reflect2 v1.0.2 // indirect
|
||||
github.com/yuin/goldmark v1.7.13 // indirect
|
||||
golang.org/x/crypto v0.14.0 // indirect
|
||||
golang.org/x/mod v0.27.0 // indirect
|
||||
golang.org/x/sys v0.35.0 // indirect
|
||||
golang.org/x/tools v0.36.1-0.20250903222949-a5c0eb837c9f // indirect
|
||||
golang.org/x/tools/cmd/godoc v0.1.0-deprecated // indirect
|
||||
golang.org/x/tools/godoc v0.1.0-deprecated // indirect
|
||||
)
|
||||
107
server/go.sum
107
server/go.sum
@@ -1,107 +0,0 @@
|
||||
github.com/alexedwards/argon2id v1.0.0 h1:wJzDx66hqWX7siL/SRUmgz3F8YMrd/nfX/xHHcQQP0w=
|
||||
github.com/alexedwards/argon2id v1.0.0/go.mod h1:tYKkqIjzXvZdzPvADMWOEZ+l6+BD6CtBXMj5fnJppiw=
|
||||
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/go-chi/chi/v5 v5.2.1 h1:KOIHODQj58PmL80G2Eak4WdvUzjSJSm0vG72crDCqb8=
|
||||
github.com/go-chi/chi/v5 v5.2.1/go.mod h1:L2yAIGWB3H+phAw1NxKwWM+7eUH/lU8pOMm5hHcoops=
|
||||
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
|
||||
github.com/golang-jwt/jwt/v5 v5.3.0 h1:pv4AsKCKKZuqlgs5sUmn4x8UlGa0kEVt/puTpKx9vvo=
|
||||
github.com/golang-jwt/jwt/v5 v5.3.0/go.mod h1:fxCRLWMO43lRc8nhHWY6LGqRcf+1gQWArsqaEUEa5bE=
|
||||
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
||||
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
|
||||
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
|
||||
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
|
||||
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
|
||||
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
|
||||
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
|
||||
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
|
||||
github.com/mailgun/errors v0.4.0 h1:6LFBvod6VIW83CMIOT9sYNp28TCX0NejFPP4dSX++i8=
|
||||
github.com/mailgun/errors v0.4.0/go.mod h1:xGBaaKdEdQT0/FhwvoXv4oBaqqmVZz9P1XEnvD/onc0=
|
||||
github.com/mailgun/mailgun-go/v4 v4.23.0 h1:jPEMJzzin2s7lvehcfv/0UkyBu18GvcURPr2+xtZRbk=
|
||||
github.com/mailgun/mailgun-go/v4 v4.23.0/go.mod h1:imTtizoFtpfZqPqGP8vltVBB6q9yWcv6llBhfFeElZU=
|
||||
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
|
||||
github.com/mattn/go-colorable v0.1.14 h1:9A9LHSqF/7dyVVX6g0U9cwm9pG3kP9gSzcuIPHPsaIE=
|
||||
github.com/mattn/go-colorable v0.1.14/go.mod h1:6LmQG8QLFO4G5z1gPvYEzlUgJ2wF+stgPZH1UqBm1s8=
|
||||
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
|
||||
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
|
||||
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
|
||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
|
||||
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
|
||||
github.com/mssola/user_agent v0.6.0 h1:uwPR4rtWlCHRFyyP9u2KOV0u8iQXmS7Z7feTrstQwk4=
|
||||
github.com/mssola/user_agent v0.6.0/go.mod h1:TTPno8LPY3wAIEKRpAtkdMT0f8SE24pLRGPahjCH4uw=
|
||||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/rs/xid v1.6.0/go.mod h1:7XoLgs4eV+QndskICGsho+ADou8ySMSjJKDIan90Nz0=
|
||||
github.com/rs/zerolog v1.34.0 h1:k43nTLIwcTVQAncfCw4KZ2VY6ukYoZaBPNOE8txlOeY=
|
||||
github.com/rs/zerolog v1.34.0/go.mod h1:bJsvje4Z08ROH4Nhs5iH600c3IkWhwp44iRc54W6wYQ=
|
||||
github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0=
|
||||
github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
|
||||
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
|
||||
github.com/yuin/goldmark v1.7.13 h1:GPddIs617DnBLFFVJFgpo1aBfe/4xcvMc3SB5t/D0pA=
|
||||
github.com/yuin/goldmark v1.7.13/go.mod h1:ip/1k0VRfGynBgxOz0yCqHrbZXhcjxyuS66Brc7iBKg=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
|
||||
golang.org/x/crypto v0.14.0 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc=
|
||||
golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4=
|
||||
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
|
||||
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
|
||||
golang.org/x/mod v0.27.0 h1:kb+q2PyFnEADO2IEF935ehFUXlWiNjJWtRNgBLSfbxQ=
|
||||
golang.org/x/mod v0.27.0/go.mod h1:rWI627Fq0DEoudcK+MBkNkCe0EetEaDSwJJkCcjpazc=
|
||||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
|
||||
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
|
||||
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
|
||||
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
|
||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.35.0 h1:vz1N37gP5bs89s7He8XuIYXpyY0+QlsKmzipCbUtyxI=
|
||||
golang.org/x/sys v0.35.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
|
||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
||||
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
|
||||
golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo=
|
||||
golang.org/x/term v0.13.0/go.mod h1:LTmsnFJwVN6bCy1rVCoS+qHT1HhALEFxKncY3WNNh4U=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
||||
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
|
||||
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
|
||||
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
|
||||
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
|
||||
golang.org/x/tools v0.36.1-0.20250903222949-a5c0eb837c9f h1:jDEaVlf+r7N8Re8Es5pGylGkfnqcx9dfUCsd1T+biTs=
|
||||
golang.org/x/tools v0.36.1-0.20250903222949-a5c0eb837c9f/go.mod h1:n+8pplxVZfXnmHBxWsfPnQRJ5vWroQDk+U2MFpjwtFY=
|
||||
golang.org/x/tools/cmd/godoc v0.1.0-deprecated h1:sEGTwp9aZNTHsdf/2BGaRqE4ZLndRVH17rbQ2OVun9Q=
|
||||
golang.org/x/tools/cmd/godoc v0.1.0-deprecated/go.mod h1:J6VY4iFch6TIm456U3fnw1EJZaIqcYlhHu6GpHQ9HJk=
|
||||
golang.org/x/tools/godoc v0.1.0-deprecated h1:o+aZ1BOj6Hsx/GBdJO/s815sqftjSnrZZwyYTHODvtk=
|
||||
golang.org/x/tools/godoc v0.1.0-deprecated/go.mod h1:qM63CriJ961IHWmnWa9CjZnBndniPt4a3CK0PVB9bIg=
|
||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
gopkg.in/natefinch/lumberjack.v2 v2.2.1 h1:bBRl1b0OH9s/DuPhuXpNl+VtCaJXFZ5/uEFST95x9zc=
|
||||
gopkg.in/natefinch/lumberjack.v2 v2.2.1/go.mod h1:YD8tP3GAjkrDg1eZH7EGmyESg/lsYskCTPBJVb9jqSc=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
12
server/handlers.js
Normal file
12
server/handlers.js
Normal file
@@ -0,0 +1,12 @@
|
||||
import { broadcast } from './ws.js';
|
||||
|
||||
const handlers = {
|
||||
updateLocation(req, res) {
|
||||
const { name, latitude, longitude, timestamp } = req.body;
|
||||
console.log(`Received location: (${name}, ${latitude}, ${longitude}) at ${timestamp}`);
|
||||
broadcast("update-location", { name, latitude, longitude, timestamp });
|
||||
res.json({ success: true });
|
||||
}
|
||||
}
|
||||
|
||||
export default handlers;
|
||||
@@ -1,148 +0,0 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"github.com/rs/zerolog/log"
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"regexp"
|
||||
"time"
|
||||
"context"
|
||||
|
||||
"hyperia/config"
|
||||
"github.com/mailgun/mailgun-go/v4"
|
||||
)
|
||||
|
||||
type joinRequest struct {
|
||||
Email string `json:"email"`
|
||||
}
|
||||
|
||||
var emailRegex = regexp.MustCompile(`^[a-zA-Z0-9._%+\-]+@[a-zA-Z0-9.\-]+\.[a-zA-Z]{2,}$`)
|
||||
|
||||
func isValidEmail(email string) bool {
|
||||
return emailRegex.MatchString(email)
|
||||
}
|
||||
|
||||
func HandleJoin(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
http.Error(w, "Only POST allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
var creds joinRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&creds); err != nil {
|
||||
http.Error(w, "Invalid JSON", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
if !isValidEmail(creds.Email) {
|
||||
http.Error(w, "Invalid email address", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// exists, err := EmailExists(creds.Email)
|
||||
// if err != nil {
|
||||
// log.Printf("Error checking email: %v", err)
|
||||
// http.Error(w, "Internal server error", http.StatusInternalServerError)
|
||||
// return
|
||||
// }
|
||||
// if exists {
|
||||
// http.Error(w, "Email already exists.", http.StatusConflict)
|
||||
// return
|
||||
// }
|
||||
|
||||
// err = CreateApplicant(creds.Email)
|
||||
// if err != nil {
|
||||
// log.Printf("Error creating applicant: %v", err)
|
||||
// http.Error(w, "Failed to create applicant", http.StatusInternalServerError)
|
||||
// return
|
||||
// }
|
||||
|
||||
// token, err := generateVerificationToken(creds.Email)
|
||||
// if err != nil {
|
||||
// log.Printf("Error generating verification token: %v", err)
|
||||
// http.Error(w, "Error, please try again later.", http.StatusInternalServerError)
|
||||
// return
|
||||
// }
|
||||
|
||||
// err = sendWelcomeEmail(creds.Email, token)
|
||||
// if err != nil {
|
||||
// log.Printf("Error sending welcome email: %v", err)
|
||||
// http.Error(w, "Failed to send email", http.StatusInternalServerError)
|
||||
// return
|
||||
// }
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte("OK"))
|
||||
}
|
||||
|
||||
func generateVerificationToken(email string) (string, error) {
|
||||
// Create 32 random bytes → 64-char hex string
|
||||
b := make([]byte, 32)
|
||||
if _, err := rand.Read(b); err != nil {
|
||||
return "", err
|
||||
}
|
||||
token := hex.EncodeToString(b)
|
||||
|
||||
// err := CreateApplicantVerification(email, token)
|
||||
// if err != nil {
|
||||
// return "", err
|
||||
// }
|
||||
|
||||
return token, nil
|
||||
}
|
||||
|
||||
func mailgunEmail(to string, token string) error {
|
||||
// link format: https://hyperia.so/verify?token=7a1a7cb986437cf8868b18cf43d73ce2e947d65aef30b42419bab957f5e51a09
|
||||
domain := "mg.hyperia.so"
|
||||
apiKey := "aeb90a0c75ef782eab6fc3d48fdf4435-812b35f5-fe818055"
|
||||
|
||||
mg := mailgun.NewMailgun(domain, apiKey)
|
||||
|
||||
sender := "welcome@" + domain
|
||||
subject := "Verify Your Email"
|
||||
verifyLink := config.BASE_URL + "/verify?token=" + token
|
||||
body := "Thanks for signing up! Please verify your email by clicking this link: " + verifyLink
|
||||
|
||||
message := mg.NewMessage(sender, subject, body, to)
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
|
||||
defer cancel()
|
||||
|
||||
_, _, err := mg.Send(ctx, message)
|
||||
return err
|
||||
}
|
||||
|
||||
func sendWelcomeEmail(to string, token string) error {
|
||||
if config.ENV == "development" {
|
||||
verifyLink := config.BASE_URL + "/verify?token=" + token
|
||||
log.Debug().Msgf("email Verify Link: %s", verifyLink)
|
||||
return nil
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
// query := `
|
||||
// INSERT INTO emails ("to", "from", subject, body, createdon, createdby, status)
|
||||
// VALUES ($1, $2, $3, $4, $5, $6, $7)
|
||||
// `
|
||||
|
||||
// sender := "noreply@mail.hyperia.so"
|
||||
// subject := "Verify Your Email"
|
||||
// verifyLink := config.BASE_URL + "/verify?token=" + token
|
||||
// body := "Thanks for signing up! Please verify your email by clicking this link: " + verifyLink
|
||||
|
||||
// _, err := DB.Exec(
|
||||
// query,
|
||||
// to,
|
||||
// sender,
|
||||
// subject,
|
||||
// body,
|
||||
// time.Now(), // createdon
|
||||
// "go-backend", // createdby
|
||||
// "pending", // status
|
||||
// )
|
||||
|
||||
// return err
|
||||
}
|
||||
@@ -1,101 +0,0 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
"strconv"
|
||||
|
||||
"hyperia/db"
|
||||
|
||||
"github.com/alexedwards/argon2id"
|
||||
)
|
||||
|
||||
type loginRequest struct {
|
||||
Email string `json:"email"`
|
||||
Password string `json:"password"`
|
||||
}
|
||||
|
||||
type user struct {
|
||||
ID int `json:"id"`
|
||||
Email string `json:"email"`
|
||||
}
|
||||
|
||||
func HandleLogin(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
http.Error(w, "Only POST allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
var creds loginRequest
|
||||
if err := r.ParseForm(); err != nil {
|
||||
http.Error(w, "Unable to parse form", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
email := r.FormValue("email")
|
||||
password := r.FormValue("password")
|
||||
creds.Email = email
|
||||
creds.Password = password
|
||||
|
||||
user, err := getUserByCredentials(creds)
|
||||
if err != nil || user == nil {
|
||||
http.Error(w, "Unauthorized: "+ err.Error(), http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
keyInt, err := strconv.Atoi(user["key"].(string))
|
||||
if err != nil {
|
||||
// This means the string couldn't be parsed as an int — handle it
|
||||
log.Println("user['key'] is not a valid int:", err)
|
||||
http.Error(w, "internal server error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
jwtToken, err := GenerateJWT(keyInt)
|
||||
if err != nil {
|
||||
log.Println("JWT generation error:", err)
|
||||
http.Error(w, "Failed to generate auth token", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
cookie := &http.Cookie{
|
||||
Name: "auth_token",
|
||||
Value: jwtToken,
|
||||
Path: "/",
|
||||
HttpOnly: true,
|
||||
Domain: "." + os.Getenv("BASE_URL"), // or ".localhost" — this allows subdomains
|
||||
Secure: true, // default to true (production)
|
||||
MaxAge: 2 * 60 * 60,
|
||||
SameSite: http.SameSiteLaxMode,
|
||||
}
|
||||
|
||||
http.SetCookie(w, cookie)
|
||||
http.Redirect(w, r, "/", http.StatusSeeOther)
|
||||
}
|
||||
|
||||
func getUserByCredentials(loginCreds loginRequest) (map[string]interface{}, error) {
|
||||
|
||||
email := strings.TrimSpace(strings.ToLower(loginCreds.Email))
|
||||
|
||||
user, err := db.Get.UserByEmail(email)
|
||||
if err != nil {
|
||||
return nil, errors.New("user not found")
|
||||
}
|
||||
|
||||
dbPassword, ok := user["password"].(string)
|
||||
if !ok {
|
||||
return nil, errors.New("password format is invalid")
|
||||
}
|
||||
|
||||
log.Println("pass: ", loginCreds, loginCreds.Password, dbPassword)
|
||||
|
||||
match, err := argon2id.ComparePasswordAndHash(loginCreds.Password, dbPassword)
|
||||
if err != nil || !match {
|
||||
return nil, errors.New("invalid password")
|
||||
}
|
||||
|
||||
return user, nil
|
||||
}
|
||||
@@ -1,27 +0,0 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"time"
|
||||
"os"
|
||||
|
||||
"hyperia/config"
|
||||
)
|
||||
|
||||
func HandleLogout(w http.ResponseWriter, r *http.Request) {
|
||||
// Create a cookie with the same name and domain, but expired
|
||||
cookie := &http.Cookie{
|
||||
Name: "auth_token",
|
||||
Value: "",
|
||||
Path: "/",
|
||||
HttpOnly: true,
|
||||
Domain: "." + os.Getenv("BASE_URL"), // must match what you set when logging in
|
||||
Secure: true,
|
||||
Expires: time.Unix(0, 0), // way in the past
|
||||
MaxAge: -1, // tells browser to delete immediately
|
||||
SameSite: http.SameSiteLaxMode,
|
||||
}
|
||||
|
||||
http.SetCookie(w, cookie)
|
||||
http.Redirect(w, r, config.BASE_URL, http.StatusSeeOther)
|
||||
}
|
||||
@@ -1,100 +0,0 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// Struct for incoming JSON request
|
||||
type SignupRequest struct {
|
||||
Email string `json:"email"`
|
||||
}
|
||||
|
||||
// Struct for JSON response
|
||||
type SignupResponse struct {
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
type User struct {
|
||||
Email string `json:"email"`
|
||||
}
|
||||
|
||||
func HandleSignup(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
http.Error(w, "Only POST allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
if err := r.ParseForm(); err != nil {
|
||||
http.Error(w, "Failed to parse form", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
email := strings.TrimSpace(r.FormValue("email"))
|
||||
if email == "" {
|
||||
http.Error(w, "Missing email", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// Optional: basic email format check
|
||||
if !strings.Contains(email, "@") {
|
||||
http.Error(w, "Invalid email format", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
log.Printf("Received signup from email: %s", email)
|
||||
err := AddUserToFile(email, "db/users.json")
|
||||
if err != nil {
|
||||
log.Printf("Error saving user: %v", err)
|
||||
http.Error(w, "Failed to save user", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
// Respond with success
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(SignupResponse{
|
||||
Message: fmt.Sprintf("Signup received. In the coming days, you will receive an email telling you how to join."),
|
||||
})
|
||||
}
|
||||
|
||||
func AddUserToFile(email string, filepath string) error {
|
||||
// Read the current users (if file exists)
|
||||
users := make(map[string]User)
|
||||
|
||||
if existingData, err := os.ReadFile(filepath); err == nil && len(existingData) > 0 {
|
||||
if err := json.Unmarshal(existingData, &users); err != nil {
|
||||
return fmt.Errorf("invalid users.json format: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Find the next numeric key
|
||||
maxID := 0
|
||||
for key := range users {
|
||||
id, err := strconv.Atoi(key)
|
||||
if err == nil && id > maxID {
|
||||
maxID = id
|
||||
}
|
||||
}
|
||||
newID := strconv.Itoa(maxID + 1)
|
||||
|
||||
// Add new user
|
||||
users[newID] = User{Email: email}
|
||||
|
||||
// Marshal updated data
|
||||
updated, err := json.MarshalIndent(users, "", " ")
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not marshal updated users: %v", err)
|
||||
}
|
||||
|
||||
// Write updated data back to file
|
||||
if err := os.WriteFile(filepath, updated, 0644); err != nil {
|
||||
return fmt.Errorf("could not write to users file: %v", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"hyperia/config"
|
||||
"github.com/golang-jwt/jwt/v5"
|
||||
)
|
||||
|
||||
func GenerateJWT(userId int) (string, error) {
|
||||
claims := jwt.MapClaims{
|
||||
"applicant_id": userId,
|
||||
"exp": time.Now().Add(2 * time.Hour).Unix(), // expires in 2 hours
|
||||
"iat": time.Now().Unix(),
|
||||
}
|
||||
|
||||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
||||
jwtSecret := []byte(config.JWT_SECRET)
|
||||
signedToken, err := token.SignedString(jwtSecret)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return signedToken, nil
|
||||
}
|
||||
149
server/index.js
Normal file
149
server/index.js
Normal file
@@ -0,0 +1,149 @@
|
||||
import express from 'express';
|
||||
import cors from 'cors'
|
||||
import cookieParser from 'cookie-parser'
|
||||
import http from 'http'
|
||||
import chalk from 'chalk'
|
||||
import moment from 'moment'
|
||||
import path from 'path';
|
||||
import { initWebSocket } from './ws.js'
|
||||
|
||||
import Database from "./db/db.js"
|
||||
import AuthHandler from './auth.js';
|
||||
import handlers from "./handlers.js";
|
||||
|
||||
// Get __dirname in ES6 environment
|
||||
import { fileURLToPath } from 'url';
|
||||
const __filename = fileURLToPath(import.meta.url);
|
||||
const __dirname = path.dirname(__filename);
|
||||
|
||||
class Server {
|
||||
db;
|
||||
auth;
|
||||
UIPath = path.join(__dirname, '../ui')
|
||||
|
||||
registerRoutes(router) {
|
||||
// router.post('/api/location', handlers.updateLocation)
|
||||
router.post('/login', this.auth.login)
|
||||
router.get('/signout', this.auth.logout)
|
||||
router.get('/*', this.get)
|
||||
return router
|
||||
}
|
||||
|
||||
authMiddleware = (req, res, next) => {
|
||||
const authHeader = req.headers.authorization;
|
||||
if (!authHeader) {
|
||||
return res.status(401).json({ error: 'Authorization token required.' });
|
||||
}
|
||||
|
||||
const [scheme, token] = authHeader.split(' ');
|
||||
if (scheme !== 'Bearer' || !token) {
|
||||
return res.status(401).json({ error: 'Malformed authorization header.' })
|
||||
}
|
||||
|
||||
try {
|
||||
const payload = this.auth.verify(token);
|
||||
req.user = payload;
|
||||
return next();
|
||||
} catch (err) {
|
||||
return res.status(403).json({ error: 'Invalid or expired token.' });
|
||||
}
|
||||
}
|
||||
|
||||
get = async (req, res) => {
|
||||
if(!this.auth.isLoggedInUser(req, res)) {
|
||||
console.log("Not logged in")
|
||||
let url = req.url
|
||||
if(url === "/") {
|
||||
url = "/index.html"
|
||||
} else if(!url.includes(".")) { // TODO: Make public app single-page
|
||||
url = path.join("/pages", url) + ".html"
|
||||
}
|
||||
|
||||
let filePath;
|
||||
if(url.startsWith("/_")) {
|
||||
filePath = path.join(this.UIPath, url);
|
||||
} else {
|
||||
filePath = path.join(this.UIPath, "public", url);
|
||||
}
|
||||
|
||||
res.sendFile(filePath);
|
||||
} else {
|
||||
let url = req.url
|
||||
|
||||
let filePath;
|
||||
if(url.startsWith("/_")) {
|
||||
filePath = path.join(this.UIPath, url);
|
||||
} else if(url.includes("75820185")) {
|
||||
filePath = path.join(this.UIPath, "site", url.split("75820185")[1]);
|
||||
} else {
|
||||
filePath = path.join(this.UIPath, "site", "index.html");
|
||||
}
|
||||
|
||||
res.sendFile(filePath);
|
||||
}
|
||||
}
|
||||
|
||||
logRequest(req, res, next) {
|
||||
const formattedDate = moment().format('M.D');
|
||||
const formattedTime = moment().format('h:mma');
|
||||
if(req.url.includes("/api/")) {
|
||||
console.log(chalk.blue(` ${req.method} ${req.url} | ${formattedDate} ${formattedTime}`));
|
||||
} else {
|
||||
if(req.url === "/")
|
||||
console.log(chalk.gray(` ${req.method} ${req.url} | ${formattedDate} ${formattedTime}`));
|
||||
}
|
||||
next();
|
||||
}
|
||||
|
||||
logResponse(req, res, next) {
|
||||
const originalSend = res.send;
|
||||
res.send = function (body) {
|
||||
if(res.statusCode >= 400) {
|
||||
console.log(chalk.blue( `<-${chalk.red(res.statusCode)}- ${req.method} ${req.url} | ${chalk.red(body)}`));
|
||||
} else {
|
||||
console.log(chalk.blue(`<-${res.statusCode}- ${req.method} ${req.url}`));
|
||||
}
|
||||
originalSend.call(this, body);
|
||||
};
|
||||
next();
|
||||
}
|
||||
|
||||
constructor() {
|
||||
this.db = new Database()
|
||||
global.db = this.db
|
||||
this.auth = new AuthHandler()
|
||||
const app = express();
|
||||
app.use(cors({ origin: '*' }));
|
||||
app.use(express.json());
|
||||
app.use(express.urlencoded({ extended: true }));
|
||||
app.use(cookieParser());
|
||||
|
||||
app.use(this.logRequest);
|
||||
app.use(this.logResponse);
|
||||
|
||||
let router = express.Router();
|
||||
this.registerRoutes(router)
|
||||
app.use('/', router);
|
||||
|
||||
const server = http.createServer(app);
|
||||
initWebSocket(server);
|
||||
const PORT = 3003;
|
||||
server.listen(PORT, () => {
|
||||
console.log("\n")
|
||||
console.log(chalk.yellow("**************America****************"))
|
||||
console.log(chalk.yellowBright(`Server is running on port ${PORT}: http://localhost`));
|
||||
console.log(chalk.yellow("***************************************"))
|
||||
console.log("\n")
|
||||
});
|
||||
|
||||
process.on('SIGINT', async () => {
|
||||
console.log(chalk.red('Closing server...'));
|
||||
console.log(chalk.green('Database connection closed.'));
|
||||
process.exit(0);
|
||||
});
|
||||
|
||||
Object.preventExtensions(this);
|
||||
}
|
||||
}
|
||||
|
||||
const server = new Server()
|
||||
@@ -1,28 +0,0 @@
|
||||
package logger
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"hyperia/config"
|
||||
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/rs/zerolog/log"
|
||||
"gopkg.in/natefinch/lumberjack.v2"
|
||||
)
|
||||
|
||||
// Very basic setup for starters,
|
||||
func ConfigureLogger() {
|
||||
if !config.LOG_TO_FILE {
|
||||
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
|
||||
} else {
|
||||
fmt.Println("logging to file /var/log/hyperia-server.log")
|
||||
logFile := &lumberjack.Logger{
|
||||
Filename: "/var/log/hyperia-server.log", // Path to your log file
|
||||
MaxSize: 100, // Max size in MB before rotation
|
||||
MaxBackups: 3, // Max number of old log files to keep
|
||||
MaxAge: 28, // Max number of days to retain old log files
|
||||
Compress: true, // Whether to compress old log files
|
||||
}
|
||||
log.Logger = zerolog.New(logFile).With().Timestamp().Logger()
|
||||
}
|
||||
}
|
||||
203
server/main.go
203
server/main.go
@@ -1,203 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
"hyperia/config"
|
||||
"hyperia/db"
|
||||
"hyperia/handlers"
|
||||
"hyperia/logger"
|
||||
// "runtime/debug"
|
||||
"strings"
|
||||
|
||||
"github.com/gorilla/websocket"
|
||||
"github.com/golang-jwt/jwt/v5"
|
||||
"github.com/rs/zerolog/log"
|
||||
|
||||
"github.com/alexedwards/argon2id"
|
||||
)
|
||||
|
||||
func isWebSocketRequest(r *http.Request) bool {
|
||||
connHeader := strings.ToLower(r.Header.Get("Connection"))
|
||||
upgradeHeader := strings.ToLower(r.Header.Get("Upgrade"))
|
||||
return strings.Contains(connHeader, "upgrade") && upgradeHeader == "websocket"
|
||||
}
|
||||
|
||||
func HashPassword() {
|
||||
hash, err := argon2id.CreateHash("banktest", argon2id.DefaultParams)
|
||||
if err != nil {
|
||||
log.Fatal().Msgf("failed to hash password: %v", err)
|
||||
}
|
||||
|
||||
fmt.Println("Argon2id hash: ")
|
||||
fmt.Println(hash)
|
||||
}
|
||||
|
||||
func main() {
|
||||
config.SetConfiguration()
|
||||
logger.ConfigureLogger()
|
||||
|
||||
err := db.InitDB()
|
||||
if err != nil {
|
||||
log.Fatal().Msgf("failed to connect to database: %v", err)
|
||||
} else {
|
||||
log.Info().Msg("successfully connected to PostgreSQL")
|
||||
}
|
||||
|
||||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
if(loggedIn(w, r)) {
|
||||
if isWebSocketRequest(r) {
|
||||
handleWebSocket(w, r)
|
||||
return
|
||||
}
|
||||
handleSite(w, r)
|
||||
} else {
|
||||
handlePublic(w, r)
|
||||
}
|
||||
})
|
||||
|
||||
log.Info().Msgf("Server starting on http://localhost:%s", config.PORT)
|
||||
err = http.ListenAndServe(":"+config.PORT, nil)
|
||||
if err != nil {
|
||||
log.Fatal().Msgf("failed to start server: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func handlePublic(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path == "/api/signup" {
|
||||
handlers.HandleSignup(w, r)
|
||||
return
|
||||
}
|
||||
if r.URL.Path == "/api/login" {
|
||||
handlers.HandleLogin(w, r)
|
||||
return
|
||||
}
|
||||
if strings.HasPrefix(r.URL.Path, "/_") {
|
||||
handleAsset(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
servePublicFile(w, r)
|
||||
}
|
||||
|
||||
func servePublicFile(w http.ResponseWriter, r *http.Request) {
|
||||
path := r.URL.Path
|
||||
|
||||
if path == "/" {
|
||||
|
||||
// Required for sign in / sign out redirect to work properly
|
||||
w.Header().Set("Cache-Control", "no-store, no-cache, must-revalidate, proxy-revalidate")
|
||||
w.Header().Set("Pragma", "no-cache")
|
||||
w.Header().Set("Expires", "0")
|
||||
w.Header().Set("Surrogate-Control", "no-store")
|
||||
|
||||
path = "/index.html"
|
||||
} else if !strings.Contains(path, ".") {
|
||||
path = filepath.Join("/pages", path) + ".html"
|
||||
}
|
||||
|
||||
filePath := filepath.Join("../ui/public", path)
|
||||
log.Debug().Msgf("serving: %s", filePath)
|
||||
http.ServeFile(w, r, filePath)
|
||||
}
|
||||
|
||||
func handleSite(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path == "/signout" {
|
||||
handlers.HandleLogout(w, r)
|
||||
return
|
||||
}
|
||||
if strings.Contains(r.URL.Path, "/_") {
|
||||
handleAsset(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
serveSiteFiles(w, r)
|
||||
}
|
||||
|
||||
func serveSiteFiles(w http.ResponseWriter, r *http.Request) {
|
||||
path := r.URL.Path
|
||||
|
||||
if(strings.Contains(path, "75820185")) { // necessary because, if we are at a url path 2 layers or more, the browser will insert that path at the beginning of the url
|
||||
_, after, _ := strings.Cut(path, "75820185")
|
||||
path = after
|
||||
} else {
|
||||
// Required for sign in / sign out redirect to work properly
|
||||
w.Header().Set("Cache-Control", "no-store, no-cache, must-revalidate, proxy-revalidate")
|
||||
w.Header().Set("Pragma", "no-cache")
|
||||
w.Header().Set("Expires", "0")
|
||||
w.Header().Set("Surrogate-Control", "no-store")
|
||||
|
||||
path = "/index.html"
|
||||
}
|
||||
|
||||
filePath := filepath.Join("../ui/site", path)
|
||||
log.Debug().Msgf("serving: %s", filePath)
|
||||
http.ServeFile(w, r, filePath)
|
||||
}
|
||||
|
||||
func handleAsset(w http.ResponseWriter, r *http.Request) {
|
||||
_, after, _ := strings.Cut(r.URL.Path, "/_")
|
||||
filePath := filepath.Join("../ui", "/_" + after)
|
||||
log.Debug().Msgf("serving asset: %s", filePath)
|
||||
http.ServeFile(w, r, filePath)
|
||||
}
|
||||
|
||||
func loggedIn(w http.ResponseWriter, r *http.Request) bool {
|
||||
cookie, err := r.Cookie("auth_token")
|
||||
if err != nil {
|
||||
log.Warn().Msg("Unauthorized - missing auth token")
|
||||
return false
|
||||
}
|
||||
jwtToken := cookie.Value
|
||||
|
||||
token, err := jwt.Parse(jwtToken, func(token *jwt.Token) (interface{}, error) {
|
||||
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
|
||||
return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"])
|
||||
}
|
||||
return []byte(config.JWT_SECRET), nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
log.Err(err).Msg("error authenticating jwt")
|
||||
return false
|
||||
}
|
||||
if err != nil || !token.Valid {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
|
||||
var upgrader = websocket.Upgrader{
|
||||
CheckOrigin: func(r *http.Request) bool {
|
||||
return true // In production, validate the origin!
|
||||
},
|
||||
}
|
||||
|
||||
func handleWebSocket(w http.ResponseWriter, r *http.Request) {
|
||||
conn, err := upgrader.Upgrade(w, r, nil)
|
||||
if err != nil {
|
||||
fmt.Println("WebSocket upgrade failed:", err)
|
||||
http.Error(w, "WebSocket upgrade failed", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
fmt.Println("WebSocket connection established")
|
||||
|
||||
for {
|
||||
msgType, msg, err := conn.ReadMessage()
|
||||
if err != nil {
|
||||
fmt.Println("Read error:", err)
|
||||
break
|
||||
}
|
||||
fmt.Printf("Received: %s\n", msg)
|
||||
|
||||
if err := conn.WriteMessage(msgType, msg); err != nil {
|
||||
fmt.Println("Write error:", err)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
31
server/ws.js
Normal file
31
server/ws.js
Normal file
@@ -0,0 +1,31 @@
|
||||
import { WebSocket, WebSocketServer } from 'ws';
|
||||
|
||||
let wss;
|
||||
|
||||
export function initWebSocket(server) {
|
||||
wss = new WebSocketServer({ server });
|
||||
|
||||
wss.on('connection', (ws, req) => {
|
||||
console.log('✅ New WebSocket client connected');
|
||||
|
||||
ws.on('close', () => {
|
||||
console.log('Client disconnected');
|
||||
});
|
||||
});
|
||||
|
||||
console.log('WebSocket server initialized');
|
||||
}
|
||||
|
||||
// Broadcast a message to all connected clients
|
||||
export function broadcast(reqType, data) {
|
||||
if (!wss) return;
|
||||
|
||||
const payload = typeof data === 'string' ? data : JSON.stringify(data);
|
||||
const message = `${reqType}|${payload}`;
|
||||
|
||||
wss.clients.forEach(client => {
|
||||
if (client.readyState === WebSocket.OPEN) {
|
||||
client.send(message);
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -72,7 +72,7 @@
|
||||
</div>
|
||||
|
||||
<div id="items">
|
||||
<form id="login-form" action="/api/login" method="POST">
|
||||
<form id="login-form" action="/login" method="POST">
|
||||
<input name="email" placeholder="email" style="margin-bottom: 15px;" required>
|
||||
<br>
|
||||
<input name="password" type="password" placeholder="password" required>
|
||||
|
||||
@@ -10,6 +10,7 @@ class JobsSidebar extends Shadow {
|
||||
.paddingRight(3, em)
|
||||
.gap(1, em)
|
||||
.borderRight("1px solid var(--periwinkle)")
|
||||
.borderTop("1px solid var(--periwinkle)")
|
||||
.minWidth(10, vw)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user