implemented webServer, and implemented listPadsImporter

This commit is contained in:
arnaucode
2018-04-15 00:21:36 +02:00
parent 3691547c68
commit 3e0ef09e55
19 changed files with 420 additions and 5 deletions

BIN
apiServer/apiServer Executable file

Binary file not shown.

3
apiServer/config.json Normal file
View File

@@ -0,0 +1,3 @@
{
"port": "3080"
}

9
apiServer/error.go Normal file
View File

@@ -0,0 +1,9 @@
package main
import "log"
func check(err error) {
if err != nil {
log.Println(err)
}
}

100
apiServer/main.go Normal file
View File

@@ -0,0 +1,100 @@
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"path/filepath"
"strings"
padArchiver ".."
"github.com/gorilla/mux"
)
type PadModel struct {
Link string `json:"link"`
Dir string `json:"dir"`
Title string `json:"title"`
IpfsHash string `json:"ipfsHash"`
}
type Repo struct {
Pads []string `json:"pads"`
}
func main() {
readConfig("config.json")
router := mux.NewRouter()
router.HandleFunc("/repos", GetReposList).Methods("GET")
router.HandleFunc("/repos/{repoid}", GetRepoIDList).Methods("GET")
router.HandleFunc("/repos/{repoid}/pad", PostStorePad).Methods("POST")
log.Println("padArchiver API server running")
log.Print("port: ")
log.Println(config.Port)
log.Fatal(http.ListenAndServe(":"+config.Port, router))
}
func GetReposList(w http.ResponseWriter, r *http.Request) {
file, err := os.Open(padArchiver.Storage)
if err != nil {
log.Fatalf("failed opening directory: %s", err)
}
defer file.Close()
list, _ := file.Readdirnames(0) // 0 to read all files and folders
var repos []string
for _, name := range list {
repos = append(repos, strings.Replace(name, padArchiver.Storage+"/", "", -1))
}
jResp, err := json.Marshal(repos)
check(err)
fmt.Fprintln(w, string(jResp))
}
func GetRepoIDList(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
repoid := vars["repoid"]
fileList := []string{}
err := filepath.Walk(padArchiver.Storage+"/"+repoid, func(path string, f os.FileInfo, err error) error {
fileList = append(fileList, path)
return nil
})
var files []string
for _, file := range fileList {
files = append(files, strings.Replace(file, padArchiver.Storage+"/", "", -1))
}
jResp, err := json.Marshal(files)
check(err)
fmt.Fprintln(w, string(jResp))
}
func PostStorePad(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
repoid := vars["repoid"]
//open the repo
repo := padArchiver.OpenRepo(repoid)
//get the pad json
var pad PadModel
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&pad)
if err != nil {
panic(err)
}
defer r.Body.Close()
ipfsHash, err := repo.StorePad(pad.Link, pad.Dir, pad.Title)
if err != nil {
http.Error(w, "error storing pad", http.StatusConflict)
}
pad.IpfsHash = ipfsHash
jResp, err := json.Marshal(pad)
check(err)
fmt.Fprintln(w, string(jResp))
}

19
apiServer/readConfig.go Normal file
View File

@@ -0,0 +1,19 @@
package main
import (
"encoding/json"
"io/ioutil"
)
type Config struct {
Port string `json:"port"`
}
var config Config
func readConfig(path string) {
file, err := ioutil.ReadFile(path)
check(err)
content := string(file)
json.Unmarshal([]byte(content), &config)
}

48
apiServer/test.py Normal file
View File

@@ -0,0 +1,48 @@
'''
this script uses requests and provoj
provoj is a simple library to test endpoints of an API RESTful
provoj can be downloaded using: pip install provoj
provoj repository: https://github.com/arnaucode/provoj
To run this test, just need to run:
python test.py
'''
import provoj
import requests
test = provoj.NewTest("testing padArchiver API Server")
url = "http://127.0.0.1:3080"
jsonData = {"link": "http://board.net/p/pad1", "dir": "Group1", "title": "Pad1"}
r = requests.post(url + "/repos/repo01/pad", json=jsonData)
test.rStatus("POST add new pad", r)
print(r.json())
jsonData = {"link": "http://board.net/p/pad2", "dir": "Group2", "title": "Pad2"}
r = requests.post(url + "/repos/repo01/pad", json=jsonData)
test.rStatus("POST add new pad", r)
print(r.json())
jsonData = {"link": "http://board.net/p/pad3", "dir": "Group2", "title": "Pad3"}
r = requests.post(url + "/repos/repo01/pad", json=jsonData)
test.rStatus("POST add new pad", r)
print(r.json())
r = requests.get(url + "/repos")
test.rStatus("GET repos list", r)
print(r.json())
reposList = r.json()
testRepo = reposList[0]
r = requests.get(url + "/repos/" + testRepo)
test.rStatus("GET repo " + testRepo + " list", r)
print(r.json())
test.printScores()