-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
144 lines (109 loc) · 3.03 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
package main
import (
"encoding/json"
"expvar"
"fmt"
"io"
"log"
"net/http"
"os"
"context"
"time"
"github.com/gorilla/mux"
github "nlp.go/git"
"nlp.go/stemmer"
nlp "nlp.go/token"
)
var (
numTok = expvar.NewInt("token_caller")
)
func main() {
logger := log.New(log.Writer(), "apis ", log.LstdFlags|log.Lshortfile)
s := Server{
logger: logger,
}
r := mux.NewRouter()
r.HandleFunc("/health", s.healthHandler).Methods(http.MethodGet)
r.HandleFunc("/token", s.tokenizeHandler).Methods(http.MethodPost)
r.HandleFunc("/", s.landingHandler).Methods(http.MethodGet)
r.HandleFunc("/stem/{word}", s.stemHandler).Methods(http.MethodPost)
r.HandleFunc("/git/{username}", s.githubHandler).Methods(http.MethodPost)
http.Handle("/", r)
addr := os.Getenv("ADDR")
if addr == "" {
addr = ":8080"
}
logger.Printf("starting the api on port %s", addr)
logger.Printf("api running on http://localhost:8080")
if err := http.ListenAndServe(addr, nil); err != nil {
log.Fatalf("error: %s", err)
}
}
type Server struct {
logger *log.Logger
}
func (s *Server) healthHandler(w http.ResponseWriter, r *http.Request) {
s.logger.Print("Get request to '/health'")
fmt.Fprint(w, "Bankai")
}
func (s *Server) stemHandler(w http.ResponseWriter, r *http.Request) {
s.logger.Printf("Post request to '/stem{%s}'", mux.Vars(r)["word"])
vars := mux.Vars(r)
word := vars["word"]
stem := stemmer.Stem(word)
fmt.Fprintln(w, stem)
}
func (s *Server) landingHandler(w http.ResponseWriter, r *http.Request) {
s.logger.Print("Get request to '/'")
fmt.Fprintln(w, "This is the landing page")
}
func (s *Server) tokenizeHandler(w http.ResponseWriter, r *http.Request) {
s.logger.Print("Post request to '/token' ")
defer r.Body.Close()
rdr := io.LimitReader(r.Body, 1_000_000)
data, err := io.ReadAll(rdr)
if err != nil {
http.Error(w, "Can't read", http.StatusBadRequest)
return
}
if len(data) == 0 {
http.Error(w, "missing data", http.StatusBadRequest)
return
}
text := string(data)
token := nlp.Tokenize(text)
resp := map[string]any{
"tokens": token,
}
data, err = json.Marshal(resp)
if err != nil {
http.Error(w, "Can't encode", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(data)
numTok.Add(1)
}
func (s *Server) githubHandler(w http.ResponseWriter, r *http.Request) {
s.logger.Printf("Post request to '/git/{%s}'", mux.Vars(r)["username"])
username := mux.Vars(r)["username"]
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
name, repos, err := github.Getgitprofiile(ctx, username)
if err != nil{
s.logger.Printf("An error occured while trying to get user details on github %s", err)
fmt.Fprint(w, "Unable to receive user details try again later")
return
}
resp := map[string]any{
"Name": name,
"Number of repos": repos,
}
data, err := json.Marshal(resp)
if err != nil{
s.logger.Printf("Error while decoding the response %s", err)
http.Error(w, "Can't encode", http.StatusInternalServerError)
return
}
w.Write(data)
}