2023-04-02 18:39:31 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/go-git/go-git/v5"
|
2023-04-12 09:48:12 +00:00
|
|
|
"github.com/prologic/bitcask"
|
2023-04-02 18:39:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const repoURL = "https://git.tcp.direct/S4D/tcp-wiki.git"
|
2023-04-12 09:48:12 +00:00
|
|
|
const localPath = "data"
|
|
|
|
|
|
|
|
var commentsDB *bitcask.Bitcask
|
2023-04-02 18:39:31 +00:00
|
|
|
|
|
|
|
func main() {
|
|
|
|
err := cloneRepository(repoURL, localPath)
|
|
|
|
if err != nil && err != git.ErrRepositoryAlreadyExists {
|
|
|
|
log.Fatalf("Failed to clone repository: %v", err)
|
|
|
|
}
|
|
|
|
|
2023-04-12 09:48:12 +00:00
|
|
|
commentsDB, err = bitcask.Open("./comments.db")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed to open comments database: %v", err)
|
|
|
|
}
|
|
|
|
defer commentsDB.Close()
|
|
|
|
|
2023-04-02 18:39:31 +00:00
|
|
|
http.HandleFunc("/", handler)
|
2023-04-12 09:48:12 +00:00
|
|
|
http.HandleFunc("/submit_comment", submitCommentHandler)
|
2023-04-02 18:39:31 +00:00
|
|
|
log.Fatal(http.ListenAndServe(":8080", nil))
|
|
|
|
}
|
|
|
|
|
|
|
|
func handler(w http.ResponseWriter, r *http.Request) {
|
2023-04-12 09:48:12 +00:00
|
|
|
//for debugging
|
|
|
|
log.Printf("LOCAL PATH: %q", localPath)
|
|
|
|
|
|
|
|
//...
|
|
|
|
|
2023-04-02 18:39:31 +00:00
|
|
|
if r.URL.Path == "/favicon.ico" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err := pullRepository(localPath)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Failed to pull repository: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
filePath := strings.TrimPrefix(r.URL.Path, "/")
|
|
|
|
if filePath == "" {
|
|
|
|
filePath = "README.md"
|
|
|
|
}
|
2023-04-12 09:48:12 +00:00
|
|
|
log.Printf("Rendering file %q from path %q", filePath, r.URL.Path)
|
2023-04-02 18:39:31 +00:00
|
|
|
|
2023-04-12 09:48:12 +00:00
|
|
|
err = renderPage(w, r, localPath, filePath, commentsDB)
|
2023-04-02 18:39:31 +00:00
|
|
|
if err != nil {
|
2023-04-12 09:48:12 +00:00
|
|
|
log.Printf("Comment loading? %q", commentsDB.Path())
|
|
|
|
|
2023-04-02 18:39:31 +00:00
|
|
|
http.Error(w, "File not found", http.StatusNotFound)
|
|
|
|
}
|
|
|
|
}
|