Skip to content

Commit

Permalink
Merge pull request #353 from tp-link-extender/startup-fixes
Browse files Browse the repository at this point in the history
Server startup fixes
  • Loading branch information
Heliodex authored Sep 12, 2024
2 parents 5d7cc86 + 395e0e5 commit 7210d6e
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 31 deletions.
38 changes: 24 additions & 14 deletions Economy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package main

import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
Expand Down Expand Up @@ -42,7 +43,8 @@ type (

const (
filepathDockerised = "./data/ledger" // jsonl file
filepath = "../data/economy/ledger"
folderpath = "../data/economy"
filepath = folderpath + "/ledger"

Micro currency = 1
Milli = 1e3 * Micro
Expand Down Expand Up @@ -467,7 +469,17 @@ func main() {
currentFilepath = filepathDockerised
}
file, err = os.OpenFile(currentFilepath, os.O_CREATE|os.O_APPEND|os.O_RDWR, 0o644)
Assert(err, "Failed to open ledger.")
if err != nil {
if errors.Is(err, os.ErrNotExist) {
fmt.Println(c.InPurple("Economy data folder not found, creating..."))
err = os.MkdirAll(folderpath, 0o644)
Assert(err, "Failed to create economy data folder")
file, err = os.Create(currentFilepath)
Assert(err, "Failed to create ledger")
} else {
Assert(err, "Failed to open ledger")
}
}
defer file.Close()
updateBalances()

Expand All @@ -478,19 +490,17 @@ func main() {
println("Fee percentage", int(currentFee()*100))
println("Stipend size ", toReadable(currentStipend()))

router := http.NewServeMux()

router.HandleFunc("GET /currentFee", currentFeeRoute)
router.HandleFunc("GET /currentStipend", currentStipendRoute)
router.HandleFunc("GET /balance/{id}", balanceRoute)
router.HandleFunc("GET /transactions", adminTransactionsRoute)
router.HandleFunc("GET /transactions/{id}", transactionsRoute)
router.HandleFunc("POST /transact", transactRoute)
router.HandleFunc("POST /mint", mintRoute)
router.HandleFunc("POST /burn", burnRoute)
router.HandleFunc("POST /stipend/{id}", stipendRoute)
http.HandleFunc("GET /currentFee", currentFeeRoute)
http.HandleFunc("GET /currentStipend", currentStipendRoute)
http.HandleFunc("GET /balance/{id}", balanceRoute)
http.HandleFunc("GET /transactions", adminTransactionsRoute)
http.HandleFunc("GET /transactions/{id}", transactionsRoute)
http.HandleFunc("POST /transact", transactRoute)
http.HandleFunc("POST /mint", mintRoute)
http.HandleFunc("POST /burn", burnRoute)
http.HandleFunc("POST /stipend/{id}", stipendRoute)

fmt.Println(c.InGreen("~ Economy service is up on port 2009 ~"))
err = http.ListenAndServe(":2009", router) // 03/Jan/2009 Chancellor on brink of second bailout for banks
err = http.ListenAndServe(":2009", nil) // 03/Jan/2009 Chancellor on brink of second bailout for banks
Assert(err, "Failed to start server")
}
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Instructions:
> [!TIP]
> If you don't have or can't install Docker, you can start the database manually by [installing SurrealDB](https://surrealdb.com/install) and running `surreal start -l trace -u root -p root --allow-scripting file:./data/database` in the repository's root directory, and the economy service manually by [installing Go](https://go.dev/dl/) and running `go run .` in the **Economy** directory. You'll need to run these in separate terminals or in the background.
To start a local dev server, go to the **Site** directory, run `bun -b dev`, and navigate to the link shown in the terminal (remember not to use HTTPS!). Upon saving a file, your changes will be shown in the web browser.
To start a local dev server, go to the **Site** directory, run `bun dev`, and navigate to the link shown in the terminal (remember not to use HTTPS!). Upon saving a file, your changes will be shown in the web browser.

- If you are using WSL2, the server may not correctly reflect the changes you make if the repository is stored on the Windows drive. To fix this, move the repository into a folder managed by WSL, or alternatively add the following to the default export of vite.config.ts:

Expand Down Expand Up @@ -85,7 +85,7 @@ Instructions:
- If you're using Caddy with multiple configuration files, import the Caddyfile in the repository's root directory into a Caddyfile somewhere else, and run `caddy start` and `caddy reload` from there.
- Run `docker compose up -d` to start the database and economy service
- Copy the `.env.example` file to `.env` to set up the environment variables
- Run `bun -b prod` to install dependencies and begin building
- Run `bun prod` to install dependencies and begin building
- Run `bun ./build` (not to be confused with `bun build`) to start Mercury Core.

> [!TIP]
Expand Down
10 changes: 5 additions & 5 deletions Site/package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"name": "mercurycore",
"scripts": {
"dev": "vite dev",
"build": "vite build",
"dev": "bun x -b vite dev",
"build": "bun x -b vite build",
"format": "biome format src --write",
"lint": "biome lint src",
"prod": "bun i -p && vite build",
"preview": "vite preview",
"buildview": "vite build && vite preview",
"prod": "bun i -p && bun x -b vite build",
"preview": "bun x -b vite preview",
"buildview": "bun x -b vite build && bun x -b vite preview",
"check": "svelte-check --tsconfig ./tsconfig.json",
"check:watch": "svelte-check --tsconfig ./tsconfig.json --watch"
},
Expand Down
5 changes: 5 additions & 0 deletions Site/src/lib/server/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ import { brickColours } from "$lib/brickColours"
import { z } from "zod"
import rawconfig from "../../../../mercury.core.ts"

if (!process.versions.bun || !Bun) {
console.error("Bun not detected. Did you accidentally start Mercury Core with Node?")
process.exit(1)
}

const brickColourEnum = z.union([
z.literal(1), // br
z.literal(1), // uh
Expand Down
15 changes: 8 additions & 7 deletions Site/src/routes/+layout.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script lang="ts">
import { dev } from "$app/environment"
import { browser, dev } from "$app/environment"
import { enhance } from "$app/forms"
import { navigating } from "$app/stores"
import User from "$components/User.svelte"
Expand All @@ -22,12 +22,13 @@
let timeout: Timer | null
// 100ms is the minimum time the loading bar will be shown
$: if ($navigating && !timeout) timeout = setTimeout(nprogress.start, 100)
else if (timeout) {
clearTimeout(timeout)
timeout = null
nprogress.done()
}
$: if (browser)
if ($navigating && !timeout) timeout = setTimeout(nprogress.start, 100)
else if (timeout) {
clearTimeout(timeout)
timeout = null
nprogress.done()
}
async function ping() {
// Keep the user's online status up to date
Expand Down
6 changes: 3 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ services:
- ./data/surreal:/database
command:
- start
- -l trace
- -u root
- -p root
- -l=trace
- -u=root
- -p=root
- --allow-scripting
- file:database

Expand Down

0 comments on commit 7210d6e

Please sign in to comment.