11package handlers
22
33import (
4- "net/http"
54 "encoding/json"
5+ "net/http"
66
77 "go.uber.org/zap"
88
@@ -12,16 +12,16 @@ import (
1212/* health handler provides status check on the backend server */
1313func HealthHandler (w http.ResponseWriter , r * http.Request ) {
1414 var response models.HealthResponse
15-
16- w .Header ().Set ("Content-Type" , "application/json" )
17- w .WriteHeader (http .StatusOK )
1815
19- response .Status = "ok"
20- if err := json .NewEncoder (w ).Encode (response ); err != nil {
21- zap .L ().Error ("Failed to send health response from the handler" ,
16+ w .Header ().Set ("Content-Type" , "application/json" )
17+ w .WriteHeader (http .StatusOK )
18+
19+ response .Status = "ok"
20+ if err := json .NewEncoder (w ).Encode (response ); err != nil {
21+ zap .L ().Error ("Failed to send health response from the handler" ,
2222 zap .Error (err ),
2323 )
24- }
24+ }
2525}
2626
2727/* allows users to create a session */
@@ -30,48 +30,48 @@ func LoginHandler(w http.ResponseWriter, r *http.Request) {
3030
3131 /* decode json response*/
3232 err := json .NewDecoder (r .Body ).Decode (& user )
33- if err != nil {
33+ if err != nil {
3434 zap .L ().Warn ("A request with invalid body recieved" )
35- http .Error (w , "Invalid request body" , http .StatusBadRequest )
36- return
37- }
35+ http .Error (w , "Invalid request body" , http .StatusBadRequest )
36+ return
37+ }
3838
3939 /* check if username and password exists */
4040 if user .Username == "" || user .Password == "" {
4141 zap .L ().Warn ("A request with no username or password recieved" )
42- http .Error (w , "Username and password are required" , http .StatusBadRequest )
43- return
44- }
42+ http .Error (w , "Username and password are required" , http .StatusBadRequest )
43+ return
44+ }
4545
4646 /* authenticate the user with ldap */
4747 authStatus := ldap .AuthenticateUser ()
48- if ! authStatus {
48+ if ! authStatus {
4949 zap .L ().Warn ("A request with invalid credentials recieved" )
50- http .Error (w , "Invalid credentials" , http .StatusUnauthorized )
51- return
52- }
50+ http .Error (w , "Invalid credentials" , http .StatusUnauthorized )
51+ return
52+ }
5353
5454 /* create a session if user exists */
5555 sessionmanager .CreateSession (user .Username )
5656
5757 /* create a JWT token for the user */
5858 token , err := authentication .GenerateJWT (user .Username )
59- if err != nil {
60- zap .L ().Error ("Error generating token" ,
59+ if err != nil {
60+ zap .L ().Error ("Error generating token" ,
6161 zap .Error (err ),
6262 )
63- http .Error (w , "Error generating token" , http .StatusInternalServerError )
64- return
65- }
63+ http .Error (w , "Error generating token" , http .StatusInternalServerError )
64+ return
65+ }
6666
6767 /* send response with JWT token to the user */
6868 response := map [string ]string {"token" : token }
69- w .Header ().Set ("Content-Type" , "application/json" )
70- if err := json .NewEncoder (w ).Encode (response ); err != nil {
71- zap .L ().Error ("Failed to encode response" ,
69+ w .Header ().Set ("Content-Type" , "application/json" )
70+ if err := json .NewEncoder (w ).Encode (response ); err != nil {
71+ zap .L ().Error ("Failed to encode response" ,
7272 zap .Error (err ),
7373 )
74- http .Error (w , "Failed to encode response" , http .StatusInternalServerError )
75- return
76- }
74+ http .Error (w , "Failed to encode response" , http .StatusInternalServerError )
75+ return
76+ }
7777}
0 commit comments