added better server handling and branch support

This commit is contained in:
legitnull 2023-04-12 16:22:36 -06:00
parent cfb953c6cb
commit 025e339d7a
5 changed files with 76 additions and 20 deletions

3
.gitignore vendored
View File

@ -1,2 +1,3 @@
data/
comments.db/
comments.db/
notes

View File

@ -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

26
dev.sh
View File

@ -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"

View File

@ -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
}

View File

@ -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)
}