-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapiv1.go
More file actions
91 lines (77 loc) · 1.72 KB
/
apiv1.go
File metadata and controls
91 lines (77 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package main
import (
"github.com/kataras/iris"
"io/ioutil"
"encoding/json"
"github.com/k0kubun/pp"
"log"
"errors"
)
func LinkApi(api iris.Party, manager *WorkManager) {
api.Get("/get-endpoints", func(c iris.Context) {
type MiniWork struct {
Name string `json:"name"`
URL string `json:"url"`
Type string `json:"type"`
Endpoint string `json:"endpoint"`
}
miniWorks := make([]MiniWork, 0)
for _, w := range manager.Works {
if w.Type == "match" {
mw := MiniWork{
Name: w.Name,
URL: w.URL,
Type: w.Type,
Endpoint: "/api/v1.0/" + w.Name,
}
miniWorks = append(miniWorks, mw)
}
}
c.StatusCode(iris.StatusOK)
c.JSON(iris.Map{
"data": miniWorks,
"error": nil,
})
})
api.Get("/{endpoint: string}", func(c iris.Context) {
endpoint := c.Params().Get("endpoint")
pp.Println(endpoint)
for _, work := range manager.Works {
if work.Name == endpoint {
filePath := "./data/" + work.Type + "_" +work.Name + ".json"
pp.Println(filePath)
data, err := ioutil.ReadFile(filePath)
if err != nil{
log.Println("Error at ioutil.ReadFile()")
c.StatusCode(iris.StatusInternalServerError)
c.JSON(iris.Map{
"data": nil,
"error": err,
})
return
}
var response interface{}
err = json.Unmarshal(data, &response)
if err != nil{
c.StatusCode(iris.StatusInternalServerError)
c.JSON(iris.Map{
"data": nil,
"error": err,
})
return
}
c.JSON(iris.Map{
"data": response,
"error": nil,
})
return
}
}
c.StatusCode(iris.StatusInternalServerError)
c.JSON(iris.Map{
"data": nil,
"error": errors.New("work not found, check uri endpoint").Error(),
})
return
})
}