From 025e339d7a909df36fa672755e3d05a3b9524bdf Mon Sep 17 00:00:00 2001 From: legitnull Date: Wed, 12 Apr 2023 16:22:36 -0600 Subject: [PATCH] added better server handling and branch support --- .gitignore | 3 ++- README.md | 26 ++++++++++++++++++++------ dev.sh | 26 +++++++++++++++++--------- src/git.go | 8 ++++++-- src/main.go | 33 +++++++++++++++++++++++++++++++-- 5 files changed, 76 insertions(+), 20 deletions(-) diff --git a/.gitignore b/.gitignore index 4eeb276..56dbdf6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ data/ -comments.db/ \ No newline at end of file +comments.db/ +notes \ No newline at end of file diff --git a/README.md b/README.md index 18b5dd8..c91b6b7 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,13 @@ H0wdy!!! -feel free to commit, leave suggestions, issues, or really anything <3 +feel free to commit, leave suggestions/ideas, issues, or really anything <3 -## SETUP +# What is TCP.WIKI ? + +## Project Goals +secure and verifiable wiki for projects, code, courses, documents, articles, tutorials, and more + +## Setup **For a normal user you can follow this process:** First clone the repo: @@ -27,28 +32,37 @@ Then cd and run dev.sh cd tcp-wiki bash dev.sh ``` +Then you just have to execute this to run the server: +```go +cd src && go run . +``` Then you goto your browser and visit: http://127.0.0.1:8080/ This method just adds in some handy symlinks for development purposes -### Use with your own repo? +## Want to use with your own repo? -All you have to do is modify the main.go file: +All you have to do is modify the following lines in the src/main.go file: ```go const repoURL = "https://git.tcp.direct/S4D/tcp-wiki.git" ``` -Change this line to your repo link, and enjoy! +Change `https://git.tcp.direct/S4D/tcp-wiki.git` to your repo link, and: +```go +const repoBRANCH = "main" +``` +change `main` to your specific repo's branch and you should be good to go! ## TODO - [ ] MANY FUCKING THINGS - [ ] Webhook support for auto pull on push/update of the git repo -- [ ] Git Branch support +- [x] Git Branch support - [ ] add a star/upvote/like feature for pages - [ ] edit tracker - [ ] Author - [ ] last edited - [ ] last editor/commit + - [ ] PGP Signed & Verification - [ ] pgp signed intergration - [x] comments using bitcask - generated in comments.db/ - [ ] verification - no login pgp diff --git a/dev.sh b/dev.sh index 142421c..75c7f3f 100644 --- a/dev.sh +++ b/dev.sh @@ -1,14 +1,22 @@ #/bin/bash # this sets up super annoying shit like hard symlinks and whatever else -# Eventually front end will be in a seperate branch for production # but for now we can manage by editing via hardlinks. # This sets up your local readme as the main page and the files in assets as public -cd src && go run . -rm ../data/assets/* -ln ../assets/_layout.html ../data/assets/_layout.html -ln ../assets/main.css ../data/assets/main.css -rm ../data/README.md -ln ../README.md ../data/README.md -echo "Developer setup ready!" -echo "Goto: http://127.0.0.1:8080" +# Clone the repository +echo "Press Control+C when prompted" +go run ./src + + + +# Set up hard links +# !!! for main branch only !!! +rm data/assets/* +ln assets/_layout.html data/assets/_layout.html +ln assets/main.css data/assets/main.css +rm data/README.md +ln README.md data/README.md + +echo "Developer setup ready!" +echo "Go ahead and run go run src/" +echo "And Go to: http://127.0.0.1:8080" diff --git a/src/git.go b/src/git.go index 1fa0326..9e746f8 100644 --- a/src/git.go +++ b/src/git.go @@ -6,6 +6,7 @@ import ( "os" git "github.com/go-git/go-git/v5" + "github.com/go-git/go-git/v5/plumbing" ) func cloneRepository(repoURL, localPath string) error { @@ -16,7 +17,7 @@ func cloneRepository(repoURL, localPath string) error { return err } -func pullRepository(localPath string) error { +func pullRepository(localPath, branch string) error { repo, err := git.PlainOpen(localPath) if err != nil { return err @@ -27,7 +28,10 @@ func pullRepository(localPath string) error { return err } - err = worktree.Pull(&git.PullOptions{RemoteName: "origin"}) + err = worktree.Pull(&git.PullOptions{ + RemoteName: "origin", + ReferenceName: plumbing.NewBranchReferenceName(branch), + }) if err != nil && err != git.NoErrAlreadyUpToDate { return err } diff --git a/src/main.go b/src/main.go index 91a71f1..b0dd076 100644 --- a/src/main.go +++ b/src/main.go @@ -1,15 +1,22 @@ package main import ( + "context" + "fmt" "log" "net/http" + "os" + "os/signal" "strings" + "syscall" + "time" "github.com/go-git/go-git/v5" "github.com/prologic/bitcask" ) const repoURL = "https://git.tcp.direct/S4D/tcp-wiki.git" +const repoBRANCH = "main" const localPath = "../data" var commentsDB *bitcask.Bitcask @@ -28,7 +35,29 @@ func main() { http.HandleFunc("/", handler) http.HandleFunc("/submit_comment", submitCommentHandler) - log.Fatal(http.ListenAndServe(":8080", nil)) + + srv := &http.Server{Addr: ":8080"} + go func() { + if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed { + log.Fatalf("ListenAndServe() failed: %v", err) + } + }() + + fmt.Println("Server running at http://127.0.0.1:8080") + fmt.Println("Press Ctrl-C to stop the server") + + // Wait for interrupt signal to stop the server + c := make(chan os.Signal, 1) + signal.Notify(c, os.Interrupt, syscall.SIGTERM) + <-c + + // Shutdown the server gracefully + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + if err := srv.Shutdown(ctx); err != nil { + log.Fatalf("Server Shutdown failed: %v", err) + } + fmt.Println("Server stopped") } func handler(w http.ResponseWriter, r *http.Request) { @@ -41,7 +70,7 @@ func handler(w http.ResponseWriter, r *http.Request) { return } - err := pullRepository(localPath) + err := pullRepository(localPath, repoBRANCH) if err != nil { log.Printf("Failed to pull repository: %v", err) }