diff --git a/README.md b/README.md index 56bb49b..17909ba 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,44 @@ # padArchiver Tool to store a pad (from the link) into IPFS and Git. +## webServer +This is a webserver that automatically generates a web from the pads directories and files stored. +To run: +``` +./webServer +``` +## listPadsImporter +This is to import all pads from the json file 'list.json'. +Just need to edit the json file, for example: +```json +{ + "repoid": "repo01", + "pads": [ + { + "link": "http://board.net/p/pad1", + "dir": "Group1", + "title": "Pad1" + }, + { + "link": "http://board.net/p/pad2", + "dir": "Group1", + "title": "Pad2" + }, + { + "link": "http://board.net/p/pad3", + "dir": "Group2", + "title": "Pad3" + } + ] +} +``` +And then execute the importer: +``` +./importFromList +``` -## padArchiver-APIserver +## apiServer This is an API to run in localhost. #### Run @@ -28,7 +63,7 @@ To run using the compiled binary: #### API Routes -###### - GET /repos +##### - GET /repos this returns: ``` [ @@ -38,7 +73,7 @@ this returns: ``` -###### - GET /repos/{repoid} +##### - GET /repos/{repoid} this returns: ``` [ @@ -53,7 +88,7 @@ this returns: ``` -###### - POST /repos/{repoid}/pad +##### - POST /repos/{repoid}/pad data to send: ``` json: { @@ -73,7 +108,7 @@ this returns: ``` The IPFS hash is also added to the first line of the document, before adding the document to Git. -## padArchiver-cli +## cli To run the CLI, just need to run: ``` ./padArchiver-cli diff --git a/padArchiver-APIserver/padArchiver-APIserver b/apiServer/apiServer similarity index 99% rename from padArchiver-APIserver/padArchiver-APIserver rename to apiServer/apiServer index 19c7b2e..581a087 100755 Binary files a/padArchiver-APIserver/padArchiver-APIserver and b/apiServer/apiServer differ diff --git a/padArchiver-APIserver/config.json b/apiServer/config.json similarity index 100% rename from padArchiver-APIserver/config.json rename to apiServer/config.json diff --git a/padArchiver-APIserver/error.go b/apiServer/error.go similarity index 100% rename from padArchiver-APIserver/error.go rename to apiServer/error.go diff --git a/padArchiver-APIserver/main.go b/apiServer/main.go similarity index 100% rename from padArchiver-APIserver/main.go rename to apiServer/main.go diff --git a/padArchiver-APIserver/readConfig.go b/apiServer/readConfig.go similarity index 100% rename from padArchiver-APIserver/readConfig.go rename to apiServer/readConfig.go diff --git a/padArchiver-APIserver/test.py b/apiServer/test.py similarity index 100% rename from padArchiver-APIserver/test.py rename to apiServer/test.py diff --git a/padArchiver-cli/padArchiver-cli b/cli/cli similarity index 99% rename from padArchiver-cli/padArchiver-cli rename to cli/cli index 0b6cfba..1c76739 100755 Binary files a/padArchiver-cli/padArchiver-cli and b/cli/cli differ diff --git a/padArchiver-cli/main.go b/cli/main.go similarity index 100% rename from padArchiver-cli/main.go rename to cli/main.go diff --git a/listPadsImporter/error.go b/listPadsImporter/error.go new file mode 100644 index 0000000..2eacf91 --- /dev/null +++ b/listPadsImporter/error.go @@ -0,0 +1,9 @@ +package main + +import "github.com/fatih/color" + +func check(err error) { + if err != nil { + color.Red(err.Error()) + } +} diff --git a/listPadsImporter/list.json b/listPadsImporter/list.json new file mode 100644 index 0000000..6837beb --- /dev/null +++ b/listPadsImporter/list.json @@ -0,0 +1,20 @@ +{ + "repoid": "repo01", + "pads": [ + { + "link": "http://board.net/p/pad1", + "dir": "Group1", + "title": "Pad1" + }, + { + "link": "http://board.net/p/pad2", + "dir": "Group1", + "title": "Pad2" + }, + { + "link": "http://board.net/p/pad3", + "dir": "Group2", + "title": "Pad3" + } + ] +} diff --git a/listPadsImporter/listPadsImporter b/listPadsImporter/listPadsImporter new file mode 100755 index 0000000..11bdbb1 Binary files /dev/null and b/listPadsImporter/listPadsImporter differ diff --git a/listPadsImporter/main.go b/listPadsImporter/main.go new file mode 100644 index 0000000..e41259c --- /dev/null +++ b/listPadsImporter/main.go @@ -0,0 +1,66 @@ +package main + +import ( + "encoding/json" + "fmt" + "io/ioutil" + + padArchiver ".." + "github.com/fatih/color" +) + +type PadModel struct { + Link string `json:"link"` + Dir string `json:"dir"` + Title string `json:"title"` +} +type ListModel struct { + RepoID string `json:"repoid"` + Pads []PadModel `json:"pads"` +} + +func readList(path string) ListModel { + file, err := ioutil.ReadFile(path) + check(err) + content := string(file) + var list ListModel + json.Unmarshal([]byte(content), &list) + return list +} + +func main() { + asciiart := ` +. +. _ _ _ +. | | /\ | | (_) + _ __ __ _ __| | / \ _ __ ___| |__ ___ _____ _ __ +| '_ \ / _ |/ _ | / /\ \ | '__/ __| '_ \| \ \ / / _ \ '__| +| |_) | (_| | (_| |/ ____ \| | | (__| | | | |\ V / __/ | +| .__/ \__,_|\__,_/_/ \_\_| \___|_| |_|_| \_/ \___|_| - listPadsImporter +| | +|_| + + ` + color.Blue(asciiart) + fmt.Println(" v0.0.1") + color.Blue("https://github.com/arnaucode/padArchiver") + fmt.Println("") + fmt.Println("") + fmt.Println("") + + list := readList("list.json") + + //open the repo + repo := padArchiver.OpenRepo(list.RepoID) + fmt.Println("repo opened") + for _, pad := range list.Pads { + fmt.Println("importing pad:") + fmt.Println(" link: " + pad.Link) + fmt.Println(" dir: " + pad.Dir) + fmt.Println(" title: " + pad.Title) + ipfsHash, err := repo.StorePad(pad.Link, pad.Dir, pad.Title) + check(err) + fmt.Println(" ipfs hash: " + ipfsHash) + } + color.Green("listPadsImporter finished") +} diff --git a/webServer/error.go b/webServer/error.go new file mode 100644 index 0000000..2eacf91 --- /dev/null +++ b/webServer/error.go @@ -0,0 +1,9 @@ +package main + +import "github.com/fatih/color" + +func check(err error) { + if err != nil { + color.Red(err.Error()) + } +} diff --git a/webServer/files.go b/webServer/files.go new file mode 100644 index 0000000..680b6cc --- /dev/null +++ b/webServer/files.go @@ -0,0 +1,23 @@ +package main + +import ( + "io/ioutil" + + "github.com/fatih/color" + blackfriday "gopkg.in/russross/blackfriday.v2" +) + +func readFile(path string) string { + dat, err := ioutil.ReadFile(path) + if err != nil { + color.Red(path) + } + check(err) + return string(dat) +} + +func fileToHTML(path string) (string, error) { + mdcontent := readFile(path) + htmlcontent := string(blackfriday.Run([]byte(mdcontent))) + return htmlcontent, nil +} diff --git a/webServer/main.go b/webServer/main.go new file mode 100644 index 0000000..1a7ceaf --- /dev/null +++ b/webServer/main.go @@ -0,0 +1,88 @@ +package main + +import ( + "html/template" + "log" + "net/http" + "os" + "path/filepath" + "strings" + + padArchiver ".." + "github.com/gorilla/mux" +) + +type ItemModel struct { + Name string + Path string + IsDir bool + Content template.HTML +} +type MenuModel struct { + PageTitle string + Items []ItemModel +} + +func main() { + + router := mux.NewRouter() + router.HandleFunc("/", getDir).Methods("GET") + router.HandleFunc("/dir/{path}", getDir).Methods("GET") + router.HandleFunc("/page/{path}", getPage).Methods("GET") + + log.Println("padArchiver web server running") + log.Print("port: 8080") + log.Fatal(http.ListenAndServe(":8080", router)) +} + +func generateMenu(dirpath string) MenuModel { + var menuPage MenuModel + menuPage.PageTitle = "padArchiver - Menu" + _ = filepath.Walk(padArchiver.Storage+dirpath, func(path string, f os.FileInfo, err error) error { + if path != padArchiver.Storage { + path = strings.Replace(path, padArchiver.Storage, "", -1) + var item ItemModel + item.Name = path + path = strings.Replace(path, "/", "%", -1) + if f.IsDir() { + item.Path = "/dir/" + path + } else { + item.Path = "/page/" + path + } + item.IsDir = f.IsDir() + menuPage.Items = append(menuPage.Items, item) + } + return nil + }) + return menuPage +} +func getDir(w http.ResponseWriter, r *http.Request) { + vars := mux.Vars(r) + var dirpath string + if _, ok := vars["path"]; ok { + dirpath = vars["path"] + dirpath = strings.Replace(dirpath, "%", "/", -1) + } + + menuPage := generateMenu(dirpath) + + tmpl := template.Must(template.ParseFiles("templates/menuTemplate.html")) + tmpl.Execute(w, menuPage) +} + +func getPage(w http.ResponseWriter, r *http.Request) { + vars := mux.Vars(r) + path := vars["path"] + path = strings.Replace(path, "%", "/", -1) + path = padArchiver.Storage + path + + content, err := fileToHTML(path) + check(err) + + var item ItemModel + item.Name = path + item.Content = template.HTML(content) + + tmpl := template.Must(template.ParseFiles("templates/pageTemplate.html")) + tmpl.Execute(w, item) +} diff --git a/webServer/templates/menuTemplate.html b/webServer/templates/menuTemplate.html new file mode 100644 index 0000000..1e3c749 --- /dev/null +++ b/webServer/templates/menuTemplate.html @@ -0,0 +1,91 @@ + + + + + + + + + + + + + + padArchiver + + + + + +

+ +
+
+
+
+

{{.PageTitle}}

+
    + {{range .Items}} + {{if .IsDir}} +

    + + + {{else}} + {{.Name}} + {{end}} + {{end}} +
+
+ +
+
+ + + + + + + + + + + diff --git a/webServer/templates/pageTemplate.html b/webServer/templates/pageTemplate.html new file mode 100644 index 0000000..33add86 --- /dev/null +++ b/webServer/templates/pageTemplate.html @@ -0,0 +1,74 @@ + + + + + + + + + + + + + + padArchiver + + + + + +

+
+
+
+
+

{{.Name}}

+
{{.Content}} +
+
+ +
+ + + + + + + + + + + diff --git a/webServer/webServer b/webServer/webServer new file mode 100755 index 0000000..87b1ad9 Binary files /dev/null and b/webServer/webServer differ