Skip to content

Commit

Permalink
Merge pull request #137 from Tibz-Dankan/refactor/dir-structure
Browse files Browse the repository at this point in the history
Refactor/dir structure
  • Loading branch information
Tibz-Dankan authored Aug 23, 2024
2 parents 03301a7 + 60531c3 commit fd73409
Show file tree
Hide file tree
Showing 26 changed files with 154 additions and 135 deletions.
4 changes: 4 additions & 0 deletions .gitkeep
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
cmd
internal
tests
Makefile
Empty file added Makefile
Empty file.
8 changes: 0 additions & 8 deletions internal/event/subscribers.go

This file was deleted.

6 changes: 1 addition & 5 deletions internal/event/eventBus.go → internal/events/eventBus.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package event
package events

import (
"log"
Expand Down Expand Up @@ -38,12 +38,8 @@ func (eb *EventBus) Subscribe(topic string, ch DataChannel) {
eb.rm.Lock()
if prev, found := eb.subscribers[topic]; found {
eb.subscribers[topic] = append(prev, ch)
log.Println("subscribed to channel :::", ch)

} else {
eb.subscribers[topic] = append([]DataChannel{}, ch)
log.Println("subscribed to channel :::", ch)

}
eb.rm.Unlock()
}
Expand Down
7 changes: 7 additions & 0 deletions internal/events/publishers/publishers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package publishers

import "log"

func InitEventPublishers() {
log.Println("Initiating global event publishers...")
}
36 changes: 36 additions & 0 deletions internal/events/publishers/request.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package publishers

import (
"log"
"sync"

"github.com/Tibz-Dankan/keep-active/internal/events"
"github.com/Tibz-Dankan/keep-active/internal/models"
)

// Publishes all apps to the topic "makeRequest"
// for the purposes of making requests to the apps.
// This function must be called in a scheduler
func PublishRequestEvent() {
app := models.App{}

apps, err := app.FindAll()
if err != nil {
log.Println("Error fetching apps:", err)
return
}

if len(apps) == 0 {
return
}

var wg sync.WaitGroup
for _, app := range apps {
wg.Add(1)
go func(app models.App) {
defer wg.Done()
events.EB.Publish("makeRequest", app)
}(app)
}
wg.Wait()
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package event
package subscribers

import (
"log"
"time"

"github.com/Tibz-Dankan/keep-active/internal/events"
"github.com/Tibz-Dankan/keep-active/internal/models"
)

func subscribeToPermissions() {
permissionCh := make(chan DataEvent)
EB.Subscribe("permissions", permissionCh)
permissionCh := make(chan events.DataEvent)
events.EB.Subscribe("permissions", permissionCh)

permission := models.Permissions{}
type User = models.User
Expand Down
30 changes: 30 additions & 0 deletions internal/events/subscribers/request.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package subscribers

import (
"log"

"github.com/Tibz-Dankan/keep-active/internal/events"
"github.com/Tibz-Dankan/keep-active/internal/models"
"github.com/Tibz-Dankan/keep-active/internal/routes/request"
)

// Subscribes/listens to the topic "makeRequest"
// and performs an http get request to each app
// whose data has been received.
func subscribeToRequestEvent() {
appCh := make(chan events.DataEvent)
events.EB.Subscribe("makeRequest", appCh)
type App = models.App

for {
appEvent := <-appCh
app, ok := appEvent.Data.(App)

if !ok {
log.Println("Interface does not hold type App")
return
}

go request.MakeAppRequest(app)
}
}
9 changes: 9 additions & 0 deletions internal/events/subscribers/subscribers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package subscribers

import "log"

func InitEventSubscribers() {
log.Println("Initiating global event subscribers...")
go subscribeToRequestEvent()
subscribeToPermissions()
}
3 changes: 0 additions & 3 deletions internal/routes/app/disableApp.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/json"
"net/http"

"github.com/Tibz-Dankan/keep-active/internal/event"
"github.com/Tibz-Dankan/keep-active/internal/models"
"github.com/Tibz-Dankan/keep-active/internal/services"
"github.com/gorilla/mux"
Expand Down Expand Up @@ -39,8 +38,6 @@ func disableApp(w http.ResponseWriter, r *http.Request) {
return
}

event.EB.Publish("updateApp", app)

response := map[string]interface{}{
"status": "success",
"message": "App is disabled successfully",
Expand Down
3 changes: 0 additions & 3 deletions internal/routes/app/enableApp.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/json"
"net/http"

"github.com/Tibz-Dankan/keep-active/internal/event"
"github.com/Tibz-Dankan/keep-active/internal/models"
"github.com/Tibz-Dankan/keep-active/internal/services"
"github.com/gorilla/mux"
Expand Down Expand Up @@ -39,8 +38,6 @@ func enableApp(w http.ResponseWriter, r *http.Request) {
return
}

event.EB.Publish("updateApp", app)

response := map[string]interface{}{
"status": "success",
"message": "App is enabled successfully",
Expand Down
4 changes: 2 additions & 2 deletions internal/routes/app/postApp.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"encoding/json"
"net/http"

"github.com/Tibz-Dankan/keep-active/internal/event"
"github.com/Tibz-Dankan/keep-active/internal/events"
"github.com/Tibz-Dankan/keep-active/internal/middlewares"
"github.com/Tibz-Dankan/keep-active/internal/models"
"github.com/Tibz-Dankan/keep-active/internal/services"
Expand Down Expand Up @@ -70,7 +70,7 @@ func PostAdd(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(response)

user := models.User{ID: userID}
event.EB.Publish("permissions", user)
events.EB.Publish("permissions", user)
}

func PostAppRoute(router *mux.Router) {
Expand Down
3 changes: 0 additions & 3 deletions internal/routes/app/updateApp.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/json"
"net/http"

"github.com/Tibz-Dankan/keep-active/internal/event"
"github.com/Tibz-Dankan/keep-active/internal/models"
"github.com/Tibz-Dankan/keep-active/internal/services"
"github.com/gorilla/mux"
Expand Down Expand Up @@ -72,8 +71,6 @@ func updateApp(w http.ResponseWriter, r *http.Request) {
return
}

event.EB.Publish("updateApp", app)

response := map[string]interface{}{
"status": "success",
"message": "Updated successfully",
Expand Down
4 changes: 2 additions & 2 deletions internal/routes/auth/resetPassword.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"encoding/json"
"net/http"

"github.com/Tibz-Dankan/keep-active/internal/event"
"github.com/Tibz-Dankan/keep-active/internal/events"
"github.com/Tibz-Dankan/keep-active/internal/models"
"github.com/Tibz-Dankan/keep-active/internal/services"
"github.com/gorilla/mux"
Expand Down Expand Up @@ -68,7 +68,7 @@ func resetPassword(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(response)

event.EB.Publish("permissions", user)
events.EB.Publish("permissions", user)
}

func ResetPasswordRoute(router *mux.Router) {
Expand Down
4 changes: 2 additions & 2 deletions internal/routes/auth/signin.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"encoding/json"
"net/http"

"github.com/Tibz-Dankan/keep-active/internal/event"
"github.com/Tibz-Dankan/keep-active/internal/events"
"github.com/Tibz-Dankan/keep-active/internal/models"
"github.com/Tibz-Dankan/keep-active/internal/services"
"github.com/gorilla/mux"
Expand Down Expand Up @@ -67,7 +67,7 @@ func signIn(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(response)

event.EB.Publish("permissions", user)
events.EB.Publish("permissions", user)
}

func SignInRoute(router *mux.Router) {
Expand Down
4 changes: 2 additions & 2 deletions internal/routes/auth/signup.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"encoding/json"
"net/http"

"github.com/Tibz-Dankan/keep-active/internal/event"
"github.com/Tibz-Dankan/keep-active/internal/events"
"github.com/Tibz-Dankan/keep-active/internal/models"
"github.com/Tibz-Dankan/keep-active/internal/services"
"github.com/gorilla/mux"
Expand Down Expand Up @@ -73,7 +73,7 @@ func signUp(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(response)

user.ID = userId
event.EB.Publish("permissions", user)
events.EB.Publish("permissions", user)
}

func SignUpRoute(router *mux.Router) {
Expand Down
4 changes: 2 additions & 2 deletions internal/routes/feedback/postFeedback.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"encoding/json"
"net/http"

"github.com/Tibz-Dankan/keep-active/internal/event"
"github.com/Tibz-Dankan/keep-active/internal/events"
"github.com/Tibz-Dankan/keep-active/internal/middlewares"
"github.com/Tibz-Dankan/keep-active/internal/models"
"github.com/Tibz-Dankan/keep-active/internal/services"
Expand Down Expand Up @@ -48,7 +48,7 @@ func postFeedback(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(response)

user := models.User{ID: userId}
event.EB.Publish("permissions", user)
events.EB.Publish("permissions", user)
}

func PostFeedbackRoute(router *mux.Router) {
Expand Down
3 changes: 0 additions & 3 deletions internal/routes/request/deleteRequestTime.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/json"
"net/http"

"github.com/Tibz-Dankan/keep-active/internal/event"
"github.com/Tibz-Dankan/keep-active/internal/models"
"github.com/Tibz-Dankan/keep-active/internal/services"
"github.com/gorilla/mux"
Expand Down Expand Up @@ -40,8 +39,6 @@ func deleteRequestTime(w http.ResponseWriter, r *http.Request) {
"status": "success",
"message": "Request Time deleted successfully",
}
app := models.App{ID: savedRequestTime.AppID}
event.EB.Publish("updateApp", app)

w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
Expand Down
8 changes: 4 additions & 4 deletions internal/routes/request/getLiveRequests.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"net/http"
"time"

"github.com/Tibz-Dankan/keep-active/internal/event"
"github.com/Tibz-Dankan/keep-active/internal/events"
"github.com/Tibz-Dankan/keep-active/internal/middlewares"
"github.com/Tibz-Dankan/keep-active/internal/services"
"github.com/gorilla/mux"
Expand Down Expand Up @@ -90,8 +90,8 @@ func getLiveRequests(w http.ResponseWriter, r *http.Request) {
}

type AppRequestProgress = services.AppRequestProgress
appCh := make(chan event.DataEvent)
event.EB.Subscribe("appRequestProgress", appCh)
appCh := make(chan events.DataEvent)
events.EB.Subscribe("appRequestProgress", appCh)

heartbeatTicker := time.NewTicker(30 * time.Second)

Expand Down Expand Up @@ -120,7 +120,7 @@ func getLiveRequests(w http.ResponseWriter, r *http.Request) {
}
case <-disconnect:
clientManager.RemoveClient(userId)
event.EB.Unsubscribe("appRequestProgress", appCh)
events.EB.Unsubscribe("appRequestProgress", appCh)
heartbeatTicker.Stop()
cancel()
log.Println("Client disconnected:", userId)
Expand Down
Loading

0 comments on commit fd73409

Please sign in to comment.