-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
148 lines (111 loc) · 3.39 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package main
import (
// "os"
"html"
"fmt"
"encoding/json"
"net/http"
"github.com/Ethanol48/medium-api-library/article"
"github.com/Ethanol48/medium-api-library/user"
)
type ApiResponse struct {
Message string `json:"message,omitempty"`
Data any `json:"data,omitempty"` // Use `any` for flexible data types or replace with specific types
}
func main() {
mux := http.NewServeMux()
/* user funcs */
mux.HandleFunc("GET /user/metadata", func(w http.ResponseWriter, r *http.Request) {
// url parameter
usr := r.URL.Query().Get("usr")
if (usr == "") {
w.WriteHeader(422)
fmt.Fprint(w, "we couldn't find the parameter 'usr' in your request")
return
}
// create a user link
userLink := fmt.Sprintf("https://medium.com/%s", usr)
metadata := user.GetUserMetadata(userLink)
fmt.Printf("metadata: %v\n", metadata)
type MetadataResponse struct {
Name string `json:"name"`;
Desc string `json:"desc"`;
About string `json:"about"`;
Followers string `json:"followers"`;
Following string `json:"following"`;
}
resp := MetadataResponse{
Name: metadata.Name,
Desc: metadata.Desc,
About: metadata.About,
Followers: metadata.Followers,
Following: metadata.Following,
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(200)
json.NewEncoder(w).Encode(resp)
})
/* article funcs */
mux.HandleFunc("GET /article/html", func(w http.ResponseWriter, r *http.Request) {
// url parameter
link := r.URL.Query().Get("link")
if (link == "") {
w.WriteHeader(422)
fmt.Fprint(w, "we couldn't find the parameter 'link' in your request")
return
}
// TODO: validation for link
art := article.GetArticle(link)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(200)
resp := ApiResponse{
Message: "",
Data: html.UnescapeString(art.ToHTML()),
}
json.NewEncoder(w).Encode(resp)
})
mux.HandleFunc("GET /article/markdown", func(w http.ResponseWriter, r *http.Request) {
// url parameter
link := r.URL.Query().Get("link")
if (link == "") {
w.WriteHeader(422)
fmt.Fprint(w, "we couldn't find the parameter 'link' in your request")
return
}
// TODO: validation for link
art := article.GetArticle(link)
resp := ApiResponse{
Message: "",
Data: html.UnescapeString(art.ToMarkdown()),
}
json.NewEncoder(w).Encode(resp)
})
mux.HandleFunc("GET /article/metadata", func(w http.ResponseWriter, r *http.Request) {
// url parameter
link := r.URL.Query().Get("link")
if (link == "") {
w.WriteHeader(422)
fmt.Fprint(w, "we couldn't find the parameter 'link' in your request")
return
}
// TODO: validation for link
art := article.GetArticle(link)
type MetadataResponse struct {
Title string `json:"title"`;
Tags []string `json:"tags"`;
ReadTime string `json:"readtime"`;
Published string `json:"published"`;
}
resp := MetadataResponse{
Title: art.Title,
Tags: art.Tags,
ReadTime: art.ReadTime,
Published: art.Published,
}
json.NewEncoder(w).Encode(resp)
})
fmt.Println("Serving api @ http://0.0.0.0:8080")
if err := http.ListenAndServe("0.0.0.0:8080", mux); err != nil {
fmt.Println(err.Error())
}
}