Skip to content

Commit

Permalink
pause endpoint framework added (#10)
Browse files Browse the repository at this point in the history
backend functionality set up

pause functionality set up

cleaned up pause session function

unpause architecture set up

restart functionality finished

test: 💍 handlePause and handleUnpause
  • Loading branch information
patrickjfl authored Jan 16, 2022
1 parent 4591dc1 commit fa7055e
Show file tree
Hide file tree
Showing 6 changed files with 266 additions and 83 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ To run the server locally
* `scripts/deploy.sh $VERSION_NUMBER` - Deploys any changes to kubernetes manifests, builds a new docker image, pushes it to docker hub and finally scales the deployment to pull the newly created image.
* `scripts/deploy_kubernetes_config.sh` - Deploys just kubernetes manifest changes (kubernetes secret is excluded from the script).
* `scripts/push_docker.sh $VERSION_NUMBER` - Builds and pushes the code to dockerhub with a $VERSION_NUMBER as a tag.
* `go test -v ./...` - Runs all tests

## Deployment

Expand Down
58 changes: 46 additions & 12 deletions http-routes/http-routes.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
package httproutes

import (
"log"
"encoding/json"
"log"
"net/http"

"github.com/gorilla/websocket"
"github.com/jaskaransarkaria/programming-timer-server/session"
"github.com/jaskaransarkaria/programming-timer-server/readers"
"github.com/jaskaransarkaria/programming-timer-server/session"
"github.com/jaskaransarkaria/programming-timer-server/utils"
)

var upgrader = websocket.Upgrader{
// empty struct means use defaults
ReadBufferSize: 1024,
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}

func enableCors(w *http.ResponseWriter) {(*w).Header().Set("Access-Control-Allow-Origin", "*")}
func enableCors(w *http.ResponseWriter) { (*w).Header().Set("Access-Control-Allow-Origin", "*") }

func wsEndpoint(w http.ResponseWriter, r *http.Request) {
// this is for CORS - allow all origin
Expand All @@ -42,24 +43,53 @@ func updateSessionEndpoint(w http.ResponseWriter, r *http.Request) {
session.UpdateTimerChannel <- sessionToUpdate
}

func pauseSessionEndpoint(w http.ResponseWriter, r *http.Request) {
var sessionToPause session.PauseRequest
var requestBody = r.Body
log.Println("request body", requestBody)
enableCors(&w)
err := json.NewDecoder(requestBody).Decode(&sessionToPause)
log.Println("pause session endpoint reached", sessionToPause)
if err != nil {
log.Println(err)
}
defer r.Body.Close()
session.PauseTimerChannel <- sessionToPause
}

func unpauseSessionEndpoint(w http.ResponseWriter, r *http.Request) {
var sessionToUnpause session.UnpauseRequest
var requestBody = r.Body
log.Println("request body", requestBody)
enableCors(&w)
err := json.NewDecoder(requestBody).Decode(&sessionToUnpause)
log.Println("unpause session endpoint reached", sessionToUnpause)
if err != nil {
log.Println(err)
}
defer r.Body.Close()
session.UnpauseTimerChannel <- sessionToUnpause
}

func newSessionEndpoint(w http.ResponseWriter, r *http.Request) {
var timerRequest session.StartTimerReq
var requestBody = r.Body
log.Println(requestBody)
enableCors(&w)
err := json.NewDecoder(requestBody).Decode(&timerRequest)
if err != nil {
log.Println(err)
}
defer r.Body.Close()
newUser := session.User{ UUID: utils.GenerateRandomID("user") }
newUser := session.User{UUID: utils.GenerateRandomID("user")}
newSession := session.CreateNewUserAndSession(
timerRequest,
newUser,
newUser,
utils.GenerateRandomID,
)
resp := session.InitSessionResponse{
Session: newSession,
User: newUser,
Session: newSession,
User: newUser,
}
newSessionRes, _ := json.Marshal(resp)
w.Write(newSessionRes)
Expand All @@ -74,22 +104,22 @@ func joinSessionEndpoint(w http.ResponseWriter, r *http.Request) {
log.Println(err)
}
defer r.Body.Close()
var newUser = session.User{ UUID: utils.GenerateRandomID("user") }
var newUser = session.User{UUID: utils.GenerateRandomID("user")}
matchedSession, err := session.JoinExistingSession(sessionRequest, newUser)
if err != nil {
bufferedErr, _ := json.Marshal(err)
w.Write(bufferedErr)
}
resp := session.InitSessionResponse{
Session: matchedSession,
User: newUser,
Session: matchedSession,
User: newUser,
}
bufferedExistingSession, _ := json.Marshal(resp)
w.Write(bufferedExistingSession)
}

func SetupRoutes() {
http.HandleFunc("/healthz", func (w http.ResponseWriter, r *http.Request) {
http.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
w.Write([]byte("ok"))
})
Expand All @@ -98,4 +128,8 @@ func SetupRoutes() {
http.HandleFunc("/session/join", joinSessionEndpoint)
http.HandleFunc("/session/update", updateSessionEndpoint)
go readers.UpdateChannelReader()
http.HandleFunc("/session/pause", pauseSessionEndpoint)
go readers.PauseChannelReader()
http.HandleFunc("/session/unpause", unpauseSessionEndpoint)
go readers.UnpauseChannelReader()
}
8 changes: 5 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package main

import (
"github.com/jaskaransarkaria/programming-timer-server/http-routes"
"flag"
"fmt"
"net/http"
"log"
"flag"
"net/http"

httproutes "github.com/jaskaransarkaria/programming-timer-server/http-routes"
)

// flag allows you to create cli flags and assign a default
var addr = flag.String("addr", "0.0.0.0:8080", "http service address")

Expand Down
21 changes: 19 additions & 2 deletions readers/readers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package readers

import (
"log"

"github.com/gorilla/websocket"
"github.com/jaskaransarkaria/programming-timer-server/session"
)
Expand All @@ -18,7 +19,7 @@ func ConnReader(conn *websocket.Conn) {
}
conn.Close()
break
} else {
} else {
log.Println(string(p))
addToSessionErr := session.AddUserConnToSession(string(p), conn)
if addToSessionErr != nil {
Expand All @@ -31,7 +32,23 @@ func ConnReader(conn *websocket.Conn) {
// UpdateChannelReader handle updates to sessions
func UpdateChannelReader() {
for {
recievedUpdate := <- session.UpdateTimerChannel
recievedUpdate := <-session.UpdateTimerChannel
session.HandleUpdateSession(recievedUpdate)
}
}

//PauseChannelReader handles pause requests
func PauseChannelReader() {
for {
pauseRequest := <-session.PauseTimerChannel
session.HandlePauseSession(pauseRequest)
}
}

//UnpauseChannelReader handles restart requests
func UnpauseChannelReader() {
for {
unpauseRequest := <-session.UnpauseTimerChannel
session.HandleUnpauseSession(unpauseRequest)
}
}
Loading

0 comments on commit fa7055e

Please sign in to comment.