Skip to content

Commit

Permalink
Merge pull request #18 from DedLad/main
Browse files Browse the repository at this point in the history
Added Live-reload and File change watcher
  • Loading branch information
DedLad authored Feb 27, 2024
2 parents d7c162c + 0e52fba commit 6f84400
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 30 deletions.
21 changes: 0 additions & 21 deletions LICENSE

This file was deleted.

15 changes: 12 additions & 3 deletions content/about.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,20 @@ title: About
---

## About Me

I'm a computer science freshman at PES University, Bangalore.
I'm a sdadsads science freshman at PES University, Bangalore.NAAA hmm
HMMM DOES IT WORK?
I love exploring and building projects. Take a look at what I've [built](/pages/projects/) (or my [Github](https://github.com/anirudhsudhir)).

## What I'm working on right now
## What I'm working on right now?heheidek at this point
:'''ye sure

- Building [Bingo](https://github.com/anirudhsudhir/Bingo), a pastebin written in Go with templating and persistence.
- Learning the Rust programming language.
FFF
hmm?LMFAOo
does it work?dasds
LMFAOOdasds
bing bing
yee?
dsuaihidushd
BRUDDA LAS GO IT ACTUALLY WORKS LMFAOO
6 changes: 4 additions & 2 deletions content/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ date: 24-02-2024
title: Home
---

### Henlo!
A computer science student and tech enthusiast. Welcome to my home on the internet.
### Henlo!NOPE
A computer science student and tech enthusiast. Welcome to my home on the internet.HELLO
HELLO


![demo-image](static/plane.jpg)
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ require (
)

require (
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/sys v0.4.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
Expand All @@ -8,6 +10,8 @@ github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/yuin/goldmark v1.7.0 h1:EfOIvIMZIzHdB/R/zVrikYLPPwJlfMcNczJFMs1m6sA=
github.com/yuin/goldmark v1.7.0/go.mod h1:uzxRWxtg69N339t3louHJ7+O03ezfj6PlliRlaOzY1E=
golang.org/x/sys v0.4.0 h1:Zr2JFtRQNX3BCZ8YtxRE9hNJYC8J6I1MVbMg6owUp18=
golang.org/x/sys v0.4.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
8 changes: 4 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
func main() {

Check failure on line 11 in main.go

View workflow job for this annotation

GitHub Actions / build

other declaration of main
var serve bool
var addr string
var draft bool
var draft bool

rootCmd := &cobra.Command{
Use: "ssg",
Expand All @@ -20,9 +20,9 @@ func main() {
generator := ssg.Generator{
ErrorLogger: log.New(os.Stderr, "ERROR\t", log.Ldate|log.Ltime|log.Lshortfile),
}
if draft {
generator.Draft = true
}
if draft {
generator.Draft = true
}
generator.RenderSite(addr)

if serve {
Expand Down
96 changes: 96 additions & 0 deletions watcher.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
//please remove or comment out all the println statements if u dont want to see that shit spammed in console, and also remove start server bs from main.go(ILL TRY FIXING THIS LATER, AS OF NOW i AM DEAD)

package main

import (
"fmt"
"os"
"os/exec"
"path/filepath"
"sync"
"time"
)

var (
cmd *exec.Cmd
cmdMutex sync.Mutex
fileTimes = make(map[string]time.Time)
fileMutex sync.Mutex
rootDirs = []string{"content", "cmd/ssg", "layout", "."} // Directories to monitor, so add or remove as needed, shit was checkin everything god
extensions = []string{".go", ".md"} // File extensions to monitor,
)

func main() {

Check failure on line 23 in watcher.go

View workflow job for this annotation

GitHub Actions / build

main redeclared in this block
fmt.Println("Watcher is running...")
watch()
}

func watch() {
for {
fileMutex.Lock()
for _, rootDir := range rootDirs {
go func(rootDir string) {
err := filepath.Walk(rootDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() && hasValidExtension(path) {
checkFile(path, info.ModTime())
}
return nil
})
if err != nil {
fmt.Println("Error walking directory:", err)
}
}(rootDir)
}
fileMutex.Unlock()
time.Sleep(5 * time.Second) // this shit is supposed to be 1s but for now let it be 5s, change it to 1 when u want it instantaneous but it will spam ur console, so remove the printlns before u do so, added those printlns while troubleshooting this
}
}

func hasValidExtension(path string) bool {
ext := filepath.Ext(path)
for _, validExt := range extensions {
if ext == validExt {
return true
}
}
return false
} //the only reason this exists is, the program was checking gitignores and shit too, so i added this check

func checkFile(path string, modTime time.Time) {
fileMutex.Lock()
defer fileMutex.Unlock()

prevModTime, ok := fileTimes[path]
if !ok || !modTime.Equal(prevModTime) {
fileTimes[path] = modTime
restartServer()
} else {
fmt.Println("No changes detected in", path)
}
}

func restartServer() {
cmdMutex.Lock()
defer cmdMutex.Unlock()

if cmd != nil && cmd.Process != nil {
err := cmd.Process.Kill()
if err != nil {
fmt.Println("Error killing server:", err)
}
}

fmt.Println("Starting server...")
cmd = exec.Command("go", "run", "main.go", "--serve")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Start()
if err != nil {
fmt.Println("Error starting server:", err)
return
}
fmt.Println("Server started")
}

0 comments on commit 6f84400

Please sign in to comment.