implemented webServer, and implemented listPadsImporter
arnaucode arnaucode@gmail.com
Sun, 15 Apr 2018 00:21:36 +0200
21 files changed,
420 insertions(+),
5 deletions(-)
jump to
M
README.md
→
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 @@ ```
#### API Routes -###### - GET /repos +##### - GET /repos this returns: ``` [@@ -38,7 +73,7 @@ ]
``` -###### - GET /repos/{repoid} +##### - GET /repos/{repoid} this returns: ``` [@@ -53,7 +88,7 @@ ]
``` -###### - POST /repos/{repoid}/pad +##### - POST /repos/{repoid}/pad data to send: ``` json: {@@ -73,7 +108,7 @@ }
``` 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
A
cli/cli
A
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()) + } +}
A
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" + } + ] +}
A
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") +}
A
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()) + } +}
A
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 +}
A
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) +}
A
webServer/templates/pageTemplate.html
@@ -0,0 +1,74 @@
+<!doctype html> +<html lang="en"> + +<head> + <!-- Required meta tags --> + <meta charset="utf-8"> + <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> + + <!-- Bootstrap CSS --> + <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" integrity="sha384-9gVQ4dYFwwWSjIDZnLEWnxCjeSWFphJiwGPXr1jddIhOegiu1FwO5qRGvFXOdJZ4" crossorigin="anonymous"> + + <style> + footer { + width:100%; + background: #38414f; + color: #bec0c4; + margin-top: 80px; + padding: 20px; + } + </style> + + <title>padArchiver</title> +</head> + +<body> + <nav class="navbar navbar-expand-lg navbar-light bg-light"> + <a class="navbar-brand" href="/">padArchiver</a> + <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation"> + <span class="navbar-toggler-icon"></span> + </button> + + <div class="collapse navbar-collapse" id="navbarSupportedContent"> + <ul class="navbar-nav mr-auto"> + <li class="nav-item active"> + <a class="nav-link" href="/">Home <span class="sr-only">(current)</span></a> + </li> + </ul> + </div> + </nav> + + <br><br> + <div class="container"> + <div class="row"> + <div class="col-sm-2"></div> + <div class="col-sm-10"> + <h3>{{.Name}}</h3> + <hr> {{.Content}} + </div> + </div> + + </div> + + <footer class="text-muted"> + <div class="container"> + <p class="float-right"> + <a href="#">Back to top</a> + </p> + <p> + padArchiver - webServer + </p> + <p> + Github: <a href="https://github.com/arnaucode/padArchiver" target="_blank">https://github.com/arnaucode/padArchiver</a> + </p> + </div> + </footer> + + <!-- Optional JavaScript --> + <!-- jQuery first, then Popper.js, then Bootstrap JS --> + <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script> + <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js" integrity="sha384-cs/chFZiN24E4KMATLdqdvsezGxaGsi4hLGOzlXwp5UZB1LY//20VyM2taTB4QvJ" crossorigin="anonymous"></script> + <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js" integrity="sha384-uefMccjFJAIv6A+rW+L4AHf99KvxDjWSu1z9VI8SKNVmz4sk7buKt/6v9KI65qnm" crossorigin="anonymous"></script> +</body> + +</html>