Skip to content

Commit

Permalink
Merge pull request #13 from EliasB-NU/fiberRewrite
Browse files Browse the repository at this point in the history
Its fiber now, yeah
  • Loading branch information
EliasB-NU authored Jun 19, 2024
2 parents 80531de + ade1ba8 commit acd7a45
Show file tree
Hide file tree
Showing 24 changed files with 587 additions and 589 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
config.json
data/
data/
deprecated/
25 changes: 21 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,28 @@ module showmaster

go 1.22.3

require github.com/gorilla/websocket v1.5.1
require (
github.com/gofiber/fiber/v2 v2.52.4
github.com/lib/pq v1.10.9
)

require github.com/rs/cors v1.11.0
require (
github.com/fasthttp/websocket v1.5.9 // indirect
github.com/savsgio/gotils v0.0.0-20240303185622-093b76447511 // indirect
golang.org/x/net v0.26.0 // indirect
)

require (
github.com/lib/pq v1.10.9
golang.org/x/net v0.17.0 // indirect
github.com/andybalholm/brotli v1.1.0 // indirect
github.com/gofiber/websocket/v2 v2.2.1
github.com/google/uuid v1.6.0 // indirect
github.com/klauspost/compress v1.17.9 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasthttp v1.55.0 // indirect
github.com/valyala/tcplisten v1.0.0 // indirect
golang.org/x/sys v0.21.0 // indirect
)
42 changes: 36 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,8 +1,38 @@
github.com/gorilla/websocket v1.5.1 h1:gmztn0JnHVt9JZquRuzLw3g4wouNVzKL15iLr/zn/QY=
github.com/gorilla/websocket v1.5.1/go.mod h1:x3kM2JMyaluk02fnUJpQuwD2dCS5NDG2ZHL0uE0tcaY=
github.com/andybalholm/brotli v1.1.0 h1:eLKJA0d02Lf0mVpIDgYnqXcUn0GqVmEFny3VuID1U3M=
github.com/andybalholm/brotli v1.1.0/go.mod h1:sms7XGricyQI9K10gOSf56VKKWS4oLer58Q+mhRPtnY=
github.com/fasthttp/websocket v1.5.9 h1:9deGuzYcCRKjk940kNwSN6Hd14hk4zYwropm4UsUIUQ=
github.com/fasthttp/websocket v1.5.9/go.mod h1:NLzHBFur260OMuZHohOfYQwMTpR7sfSpUnuqKxMpgKA=
github.com/gofiber/fiber/v2 v2.52.4 h1:P+T+4iK7VaqUsq2PALYEfBBo6bJZ4q3FP8cZ84EggTM=
github.com/gofiber/fiber/v2 v2.52.4/go.mod h1:KEOE+cXMhXG0zHc9d8+E38hoX+ZN7bhOtgeF2oT6jrQ=
github.com/gofiber/websocket/v2 v2.2.1 h1:C9cjxvloojayOp9AovmpQrk8VqvVnT8Oao3+IUygH7w=
github.com/gofiber/websocket/v2 v2.2.1/go.mod h1:Ao/+nyNnX5u/hIFPuHl28a+NIkrqK7PRimyKaj4JxVU=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/klauspost/compress v1.17.9 h1:6KIumPrER1LHsvBVuDa0r5xaG0Es51mhhB9BQB2qeMA=
github.com/klauspost/compress v1.17.9/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw=
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/rs/cors v1.11.0 h1:0B9GE/r9Bc2UxRMMtymBkHTenPkHDv0CW4Y98GBY+po=
github.com/rs/cors v1.11.0/go.mod h1:XyqrcTp5zjWr1wsJ8PIRZssZ8b/WMcMf71DJnit4EMU=
golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM=
golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
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/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/savsgio/gotils v0.0.0-20240303185622-093b76447511 h1:KanIMPX0QdEdB4R3CiimCAbxFrhB3j7h0/OvpYGVQa8=
github.com/savsgio/gotils v0.0.0-20240303185622-093b76447511/go.mod h1:sM7Mt7uEoCeFSCBM+qBrqvEo+/9vdmj19wzp3yzUhmg=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasthttp v1.55.0 h1:Zkefzgt6a7+bVKHnu/YaYSOPfNYNisSVBo/unVCf8k8=
github.com/valyala/fasthttp v1.55.0/go.mod h1:NkY9JtkrpPKmgwV3HTaS2HWaJss9RSIsRVfcxxoHiOM=
github.com/valyala/tcplisten v1.0.0 h1:rBHj/Xf+E1tRGZyWIWwJDiRY0zc1Js+CV5DqwacVSA8=
github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc=
golang.org/x/net v0.26.0 h1:soB7SVo0PWrY4vPW/+ay0jKDNScG2X9wFeYlXIvJsOQ=
golang.org/x/net v0.26.0/go.mod h1:5YKkiSynbBIh3p6iOc/vibscux0x38BZDkn8sCUPxHE=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws=
golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
3 changes: 1 addition & 2 deletions public/projectSite/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,12 @@ document.addEventListener('DOMContentLoaded', function() {

console.log(id);

fetch('/api/newinsert', {
fetch('/api/newinsert:'+table, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
table: table,
id: id,
name: name,
audio: audio,
Expand Down
42 changes: 23 additions & 19 deletions public/rowSite/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ console.log(cleanURL);

document.addEventListener("DOMContentLoaded", () => {
const headlineElement = document.getElementById('headline');
headlineElement.textContent = 'ShowMaster - V2.2 | '+cleanURL;
headlineElement.textContent = 'ShowMaster - V3 | '+cleanURL;

const ws = new WebSocket('/ws'+subpath);
const ws = new WebSocket('/ws');

ws.onopen = function(event) {
console.log('WebSocket connection established.');
Expand All @@ -23,19 +23,16 @@ document.addEventListener("DOMContentLoaded", () => {
ws.onmessage = function(event) {
console.log(event.data);
const msgs = JSON.parse(event.data);
if (msgs === "refresh") {
if (msgs === cleanURL+":refresh") {
fetchData();
} else if (msgs === "reset+"+cleanURL) {
} else if (msgs === cleanURL+":reset") {
resetStopwatch();
} else if (msgs === "start+"+cleanURL) {
} else if (msgs === cleanURL+":start") {
startStopwatch();
} else if (msgs === "stop+"+cleanURL) {
} else if (msgs === cleanURL+":stop") {
stopStopwatch();
onStopUpdate();
} else {
displayRows(msgs);
}

}
};

const stopwatchElement = document.getElementById('timer');
Expand Down Expand Up @@ -89,7 +86,7 @@ document.addEventListener("DOMContentLoaded", () => {
}

// Initialize the stopwatch with data from the backend
fetch('/api/stopwatch-status'+subpath)
fetch('/api/stopwatch-status:'+cleanURL)
.then(response => response.json())
.then(data => {
duration = data.Duration;
Expand All @@ -102,7 +99,7 @@ document.addEventListener("DOMContentLoaded", () => {
.catch(error => console.error('Error fetching stopwatch status:', error));

function onStopUpdate() {
fetch('/api/stopwatch-status'+subpath)
fetch('/api/stopwatch-status:'+cleanURL)
.then(response => response.json())
.then(data => {
duration = data.Duration;
Expand All @@ -122,7 +119,7 @@ document.addEventListener("DOMContentLoaded", () => {
};

// Make the POST request
fetch('/api/stopwatch-update'+subpath, {
fetch('/api/stopwatch-update:'+cleanURL, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
Expand All @@ -149,7 +146,7 @@ document.addEventListener("DOMContentLoaded", () => {
};

// Make the POST request
fetch('/api/stopwatch-update'+subpath, {
fetch('/api/stopwatch-update:'+cleanURL, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
Expand Down Expand Up @@ -179,7 +176,7 @@ document.addEventListener("DOMContentLoaded", () => {
};

// Make the POST request
fetch('/api/stopwatch-update'+subpath, {
fetch('/api/stopwatch-update:'+cleanURL, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
Expand All @@ -201,6 +198,13 @@ document.addEventListener("DOMContentLoaded", () => {
startPauseBtn.textContent = "Start";
});

fetch('/api/getdata:'+cleanURL)
.then(response => response.json())
.then(data => {
displayRows(data);
})
.catch(error => console.error('Error fetching data:', error));

var highlightedRow = -1
var rows = []

Expand Down Expand Up @@ -254,14 +258,14 @@ document.addEventListener("DOMContentLoaded", () => {
}
})

function sendDataToBackend(number) {
function sendDataToBackend(num) {
// Construct the data to send
const data = {
number: number
number: num
};

// Make the POST request
fetch('/api/highlightedrow'+subpath, {
fetch('/api/updatehighlightedrow:'+cleanURL, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
Expand All @@ -283,7 +287,7 @@ document.addEventListener("DOMContentLoaded", () => {
}

function fetchData() {
fetch('/api/data'+subpath)
fetch('/api/getdata:'+cleanURL)
.then(response => response.json())
.then(data => {
displayRows(data);
Expand Down
2 changes: 1 addition & 1 deletion src/database/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

var CFG config.CFG = *config.GetConfig()

func InitDB() *sql.DB {
func GetDB() *sql.DB {
// Connect to PostgreSQL database
psql := fmt.Sprintf("postgres://%s:%s@%s:%d/%s?sslmode=disable", CFG.DB.Username, CFG.DB.Password, CFG.DB.Host, CFG.DB.Port, CFG.DB.Database)
db, err := sql.Open("postgres", psql)
Expand Down
2 changes: 1 addition & 1 deletion src/database/getTables.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
func GetTables() []string {
var (
err error
db sql.DB = *InitDB()
db sql.DB = *GetDB()
)
defer db.Close()

Expand Down
5 changes: 3 additions & 2 deletions src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
func main() {
database.InitalCheckup()

web.InitiateWeb()
defer web.WS.Close() // Close the websocket
web.SetLogLevel() // Log level
go web.CheckForNewRowSite() // because of import cycle problem, periodic check if there is a new table
web.StartTheWeb() // Inits all the api endpoints and sites
}
22 changes: 11 additions & 11 deletions src/web/stopwatch.go → src/util/stopwatch.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package web
package util

import (
"sync"
Expand All @@ -8,7 +8,7 @@ import (
type Stopwatch struct {
startTime time.Time
accumulated time.Duration
running bool
Running bool
mutex sync.Mutex
}

Expand All @@ -19,44 +19,44 @@ func NewStopwatch() *Stopwatch {
func (s *Stopwatch) Start() {
s.mutex.Lock()
defer s.mutex.Unlock()
if !s.running {
if !s.Running {
s.startTime = time.Now()
s.running = true
s.Running = true
go s.runTimer()
}
}

func (s *Stopwatch) Stop() time.Duration {
s.mutex.Lock()
defer s.mutex.Unlock()
if s.running {
if s.Running {
s.accumulated += time.Since(s.startTime)
s.running = false
s.Running = false
}
return s.accumulated
}

func (s *Stopwatch) Resume() {
s.mutex.Lock()
defer s.mutex.Unlock()
if !s.running {
if !s.Running {
s.startTime = time.Now()
s.running = true
s.Running = true
go s.runTimer()
}
}

func (s *Stopwatch) Reset() {
s.mutex.Lock()
defer s.mutex.Unlock()
s.running = false
s.Running = false
s.accumulated = 0
}

func (s *Stopwatch) ElapsedSeconds() float64 {
s.mutex.Lock()
defer s.mutex.Unlock()
if s.running {
if s.Running {
return s.accumulated.Seconds() + time.Since(s.startTime).Seconds()
}
return s.accumulated.Seconds()
Expand All @@ -68,7 +68,7 @@ func (s *Stopwatch) runTimer() {

for range ticker.C {
s.mutex.Lock()
if !s.running {
if !s.Running {
s.mutex.Unlock()
return
}
Expand Down
64 changes: 0 additions & 64 deletions src/web/allWebStuffInitiated.go

This file was deleted.

Loading

0 comments on commit acd7a45

Please sign in to comment.