-
Notifications
You must be signed in to change notification settings - Fork 0
/
Api.go
84 lines (72 loc) · 1.7 KB
/
Api.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
package Panizza
import (
"encoding/json"
"errors"
"fmt"
"log"
"os"
)
var GlobalApi = APIManeger{
APIMapper: []API{},
APILength: 0,
}
type APIManeger struct {
APIMapper []API
APILength int
}
func (api *APIManeger) Add(Method string, URL string, Description string, Param []string, Location string) {
api_temp := API{
Method,
URL,
Description,
Param,
Location,
}
api.APIMapper = append(api.APIMapper, api_temp)
api.APILength++
}
//生成API文档字符串
func (api *APIManeger) ToHtmlString() string {
data, _ := json.Marshal(GlobalApi.APIMapper)
fmt.Println(string(data))
html := ApiTemplate1 + string(data) + ApiTemplate2
return html
}
//传入保存的文件的绝对路径
func (api *APIManeger) ToHtmlFile(saveFilePath string) {
data, _ := json.Marshal(GlobalApi.APIMapper)
//fmt.Println(string(data))
html := ApiTemplate1 + string(data) + ApiTemplate2
if IsDir(saveFilePath) {
panic(errors.New("Must give a file path, not a dir path!"))
}
//如果传入的路径是一个已经存在的文件就把他删除
if Existe(saveFilePath) && !IsDir(saveFilePath) {
if err := os.Remove(saveFilePath); err != nil {
panic(err)
}
}
file, err := os.Create(saveFilePath)
if err != nil {
panic(err)
}
file.WriteString(html)
log.Println("Document is generate at:", saveFilePath)
defer file.Close()
return
}
type API struct {
Method string
URL string
Description string
Param []string
Location string
}
func (a *API) String() string {
str := "Method:" + a.Method + "\n\r" + "URL:" + a.URL + "\n\r" + "Description:" + a.Description + "\n\r" + "params:"
for _, v := range a.Param {
str += v[1:] + ","
}
str += "\n\rLocation:" + a.Location
return str
}