-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
336 lines (283 loc) · 8.29 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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
package main
import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
"path/filepath"
"strconv"
"github.com/golang-jwt/jwt/v5"
"github.com/gorilla/mux"
)
type Storage interface {
SignUp(*User) error
LoginUser(*Login) (*User, error)
PostJob(*Job) error
FetchJobApplicants(int) (*Job, error)
ListUsers() ([]*User, error)
GetApplicant(int) (*Profile, error)
ListJobs() ([]*Job, error)
GetJob_byId(int) (*Job, error)
}
type APIServer struct {
listenAddr string
store Storage
}
func NewAPIServer(listen string, store Storage) *APIServer {
return &APIServer{
listenAddr: listen,
store: store,
}
}
func (s *APIServer) Run() {
router := mux.NewRouter()
router.HandleFunc("/signup", (makeHTTPHandlerFunc(s.SignUp)))
router.HandleFunc("/login", (makeHTTPHandlerFunc(s.LoginUser)))
router.HandleFunc("/uploadresume", withJWTAuth(makeHTTPHandlerFunc(s.UploadResume), s.store))
router.HandleFunc("/admin_createjob", (makeHTTPHandlerFunc(s.Admin_Createjob)))
router.HandleFunc("/admin_jobs/{id}", (makeHTTPHandlerFunc(s.Admin_jobsInfo)))
router.HandleFunc("/admin_applicants", (makeHTTPHandlerFunc(s.ListApplicants)))
router.HandleFunc("/admin_applicant/{id}", (makeHTTPHandlerFunc(s.GetApplicant)))
router.HandleFunc("/jobs", (makeHTTPHandlerFunc(s.ListJobs)))
router.HandleFunc("/apply_jobs/{id}", (makeHTTPHandlerFunc(s.GetJob_byId)))
log.Println("Server running on port: ", s.listenAddr)
http.ListenAndServe(s.listenAddr, router)
}
func (s *APIServer) SignUp(w http.ResponseWriter, r *http.Request) error {
req := User{}
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
return err
}
user, err := SignUpAccount(req.Name, req.Email, req.Address, req.UserType, req.ProfileHeadline, req.PasswordHash)
if err != nil {
return err
}
if err := s.store.SignUp(user); err != nil {
return err
}
return writeJSON(w, http.StatusOK, user)
}
func (s *APIServer) LoginUser(w http.ResponseWriter, r *http.Request) error {
if r.Method != "POST" {
return fmt.Errorf("method is not allowed %s", r.Method)
}
login := &Login{}
if err := json.NewDecoder(r.Body).Decode(&login); err != nil {
return err
}
if _, err := s.store.LoginUser(login); err != nil {
return err
}
// Create New Token everytime user login
tokenString, err := createJWT(login)
if err != nil {
return err
}
return writeJSON(w, http.StatusOK, map[string]string{"token": tokenString})
}
func (s *APIServer) UploadResume(w http.ResponseWriter, r *http.Request) error {
if r.Method != "POST" {
return fmt.Errorf("method not allowed")
}
// Parse the multipart form
err := r.ParseMultipartForm(10 << 20) // 10 MB
if err != nil {
http.Error(w, "Unable to parse form", http.StatusBadRequest)
return err
}
// Retrieve the file from the form
file, handler, err := r.FormFile("resume")
if err != nil {
http.Error(w, "Error retrieving the file", http.StatusBadRequest)
return err
}
defer file.Close()
// Define the path where the file will be saved
uploadPath := "./uploads"
if err := os.MkdirAll(uploadPath, os.ModePerm); err != nil {
http.Error(w, "Unable to create upload directory", http.StatusInternalServerError)
return err
}
// Create the file on the server
dst, err := os.Create(filepath.Join(uploadPath, handler.Filename))
if err != nil {
http.Error(w, "Unable to create the file", http.StatusInternalServerError)
return err
}
defer dst.Close()
// Copy the uploaded file data to the destination file
if _, err := io.Copy(dst, file); err != nil {
http.Error(w, "Unable to save the file", http.StatusInternalServerError)
return err
}
// Respond to the client
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, "File uploaded successfully: %s", handler.Filename)
return nil
}
func (s *APIServer) Admin_Createjob(w http.ResponseWriter, r *http.Request) error {
if r.Method != "POST" {
return fmt.Errorf("method not allowed")
}
job := &Job{}
err := json.NewDecoder(r.Body).Decode(&job)
if err != nil {
return err
}
newJob, err := CreateNewJob(job.Title, job.Description, job.TotalApplications, job.CompanyName, job.PostedBy)
if err != nil {
return err
}
err = s.store.PostJob(newJob)
if err != nil {
return err
}
writeJSON(w, http.StatusCreated, map[string]string{"status": "Job Created"})
return nil
}
// This will fetch information about JobInfo and list of applicants
func (s *APIServer) Admin_jobsInfo(w http.ResponseWriter, r *http.Request) error {
id, err := getId(r)
if err != nil {
return err
}
jobs, err := s.store.FetchJobApplicants(id)
if err != nil {
return err
}
writeJSON(w, http.StatusOK, jobs)
return nil
}
// Fetch list of all the users in the system
func (s *APIServer) ListApplicants(w http.ResponseWriter, r *http.Request) error {
if r.Method != "GET" {
return fmt.Errorf("method not allowed")
}
jobs, err := s.store.ListUsers()
if err != nil {
return err
}
writeJSON(w, http.StatusCreated, jobs)
return nil
}
// Get Single Applicant by Id
func (s *APIServer) GetApplicant(w http.ResponseWriter, r *http.Request) error {
applicant_id, err := getId(r)
if err != nil {
return err
}
applicant, err := s.store.GetApplicant(applicant_id)
if err != nil {
return err
}
writeJSON(w, http.StatusCreated, applicant)
return nil
}
// Get All the Jobs
func (s *APIServer) ListJobs(w http.ResponseWriter, r *http.Request) error {
if r.Method != "GET" {
return fmt.Errorf("method not allowed")
}
jobs, err := s.store.ListJobs()
if err != nil {
return err
}
writeJSON(w, http.StatusCreated, jobs)
return nil
}
// Apply to job
func (s *APIServer) GetJob_byId(w http.ResponseWriter, r *http.Request) error {
if r.Method != "POST" {
return fmt.Errorf("method not allowed")
}
applicant_id, err := getId(r)
if err != nil {
return err
}
applicant, err := s.store.GetJob_byId(applicant_id)
if err != nil {
return err
}
writeJSON(w, http.StatusCreated, applicant)
return nil
}
///////////////////////////////////////////
//////// JSON Token /////////////////////////
func createJWT(account *Login) (string, error) {
claims := &jwt.MapClaims{
"expiresAt": 1500,
"accountEmail": account.Email,
}
signKey := "PASSWORD"
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
return token.SignedString([]byte(signKey))
}
func withJWTAuth(handlerFunc http.HandlerFunc, s Storage) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
fmt.Println("Calling JWT handler")
tokenString := r.Header.Get("auth")
token, err := validateJWT(tokenString)
if err != nil {
writeJSON(w, http.StatusNotAcceptable, apiError{Error: "invalid token"})
return
}
if !token.Valid {
writeJSON(w, http.StatusForbidden, apiError{Error: "invalid token"})
return
}
// userId, err := getId(r)
// if err != nil {
// writeJSON(w, http.StatusForbidden, apiError{Error: "invalid token"})
// return
// }
// account, err := s.GetAccountByID(userId)
// if err != nil {
// writeJSON(w, http.StatusForbidden, apiError{Error: "invalid token"})
// return
// }
// claims := token.Claims.(jwt.MapClaims)
// fmt.Println(claims, account.Number)
// if account.Number != claims["accountEmail"] {
// writeJSON(w, http.StatusForbidden, apiError{Error: "invalid token"})
// return
// }
handlerFunc(w, r)
}
}
func validateJWT(tokenString string) (*jwt.Token, error) {
secret := "PASSWORD"
return jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"])
}
return []byte(secret), nil
})
}
// Helper One
// //////////////////////////////////////////
func writeJSON(w http.ResponseWriter, status int, v any) error {
w.Header().Set("content-type", "application/json")
w.WriteHeader(status)
return json.NewEncoder(w).Encode(v)
}
type apiFunc func(http.ResponseWriter, *http.Request) error
type apiError struct {
Error string `json:"error"`
}
func makeHTTPHandlerFunc(f apiFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if err := f(w, r); err != nil {
writeJSON(w, http.StatusBadRequest, apiError{Error: err.Error()})
}
}
}
func getId(r *http.Request) (int, error) {
idStr := mux.Vars(r)["id"]
id, err := strconv.Atoi(idStr)
if err != nil {
return id, fmt.Errorf("invalid id given %s", idStr)
}
return id, nil
}