Skip to content

Commit

Permalink
Fix CWD getting overriden in the server
Browse files Browse the repository at this point in the history
  • Loading branch information
sayden committed Oct 20, 2024
1 parent 80014d6 commit 6bbab54
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 14 deletions.
23 changes: 10 additions & 13 deletions server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type responseMutex struct {
response
}

var startingFolder, _ = os.Getwd()
var globalResponse responseMutex

func main() {
Expand All @@ -49,7 +50,9 @@ func main() {

ch := make(chan bool)
router.POST("/code", handlerCode(ch))
router.GET("/render", func(c *gin.Context) { c.HTML(http.StatusOK, "render.html", nil) })
router.GET("/render", func(c *gin.Context) {
c.HTML(http.StatusOK, "render.html", nil)
})
router.GET("/listen", handlerListen(ch))
router.GET("/state", func(c *gin.Context) {
globalResponse.Lock()
Expand Down Expand Up @@ -114,6 +117,12 @@ func handlerListen(ch <-chan bool) func(c *gin.Context) {

func handlerCode(ch chan<- bool) func(c *gin.Context) {
return func(c *gin.Context) {
defer func() {
if err := os.Chdir(startingFolder); err != nil {
log.Error(err)
}
}()

byt, err := io.ReadAll(c.Request.Body)
if err != nil {
log.Error(err)
Expand All @@ -131,18 +140,6 @@ func handlerCode(ch chan<- bool) func(c *gin.Context) {
wc := base64.NewEncoder(base64.StdEncoding, buf)
defer wc.Close()

// Capture current working directory
cwd, err := os.Getwd()
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
}
// Restore working directory after the function ends
defer func() {
if err = os.Chdir(cwd); err != nil {
log.Error(err)
}
}()

response, err := generateCounter(byt)
if err != nil {
log.Error(err)
Expand Down
2 changes: 1 addition & 1 deletion server/tailwind.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/** @type {import('tailwindcss').Config} */
module.exports = {
content: ["./static/**/*.html"],
content: ["./static/*.html", "./tailwind.css"],
theme: {},
plugins: [],
};

0 comments on commit 6bbab54

Please sign in to comment.