forked from nanliu/snap-plugin-webapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
131 lines (115 loc) · 3 KB
/
main.go
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package main
import (
"encoding/json"
"fmt"
"github.com/julienschmidt/httprouter"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
)
type Plugin struct {
Name string `json:"name"`
FullName string `json:"full_name"`
Type string `json:"type"`
Owner string `json:"owner"`
Description string `json:"description"`
URL string `json:"url"`
Forks int `json:"fork_count"`
Stars int `json:"star_count"`
Watchers int `json:"watch_count"`
Issues int `json:"issues_count"`
}
func Filter(vs []Plugin, f func(Plugin) bool) []Plugin {
vsf := make([]Plugin, 0)
for _, v := range vs {
if f(v) {
vsf = append(vsf, v)
}
}
return vsf
}
func Index(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
fmt.Fprintf(w, `Snap Plugin API Server:
/plugins
/plugins/collector
/plugins/processor
/plugins/publisher
/plugin/:name`)
}
func ListPlugin(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
fmt.Fprintf(w, "hello, %s!\n", ps.ByName("name"))
}
func ListPlugins(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
file, e := ioutil.ReadFile("./plugins.json")
if e != nil {
fmt.Fprintf(w, "File error: %v\n", e)
}
plugins := make([]Plugin, 0)
err := json.Unmarshal(file, &plugins)
if err != nil {
fmt.Fprint(w, err)
} else {
plugin_type := strings.ToLower(ps.ByName("type"))
if plugin_type != "" {
plugins = Filter(plugins, func(v Plugin) bool {
return strings.Contains(v.Type, plugin_type)
})
}
output, _ := json.MarshalIndent(plugins, "", " ")
fmt.Fprint(w, string(output))
}
}
func ListOwner(w http.ResponseWriter, r *http.Request, ps httprouter.Params){
file, e := ioutil.ReadFile("./plugins.json")
if e != nil {
fmt.Fprintf(w, "File error: %v\n", e)
}
plugins := make([]Plugin, 0)
err := json.Unmarshal(file, &plugins)
if err != nil {
fmt.Fprint(w, err)
}
plugin_type := strings.ToLower(ps.ByName("name"))
plugins = Filter(plugins, func(v Plugin) bool {
return strings.Contains(v.Type, "")
})
for _, value := range(plugins) {
if(plugin_type == value.Owner){
fmt.Fprintln(w, value.Name)
}
}
}
func ListOwners(w http.ResponseWriter, r *http.Request, ps httprouter.Params){
file, e := ioutil.ReadFile("./plugins.json")
if e != nil {
fmt.Fprintf(w, "File error: %v\n", e)
}
plugins := make([]Plugin, 0)
err := json.Unmarshal(file, &plugins)
if err != nil {
fmt.Fprint(w, err)
} else {
plugins = Filter(plugins, func(v Plugin) bool {
return strings.Contains(v.Type, "")
})
for _, value := range(plugins) {
fmt.Fprintln(w, value.Owner, ",", value.Name)
}
}
}
func main() {
router := httprouter.New()
router.GET("/", Index)
router.GET("/plugins", ListPlugins)
router.GET("/plugins/:type", ListPlugins)
router.GET("/plugin/:name", ListPlugin)
router.GET("/authors", ListOwners)
router.GET("/author/:name", ListOwner)
var port = os.Getenv("PORT")
if port == "" {
port = "8080"
}
log.Fatal(http.ListenAndServe(":"+port, router))
}