Skip to content

Commit 2e31cc7

Browse files
authored
Merge branch 'staging' into ben/profile-page
2 parents f69ec1a + 83b47d5 commit 2e31cc7

File tree

10 files changed

+149
-13
lines changed

10 files changed

+149
-13
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
test_create_api_results.txt
2+
test_list_api_results.txt
3+
test_update_api_results.txt
4+
5+
.env.example
6+
7+
.git
8+
.gitignore
9+
10+
.dockerignore
11+
Dockerfile
12+
13+
README.md

apps/question-service/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Ignore private key json file
22
cs3219-g24-firebase-adminsdk-9cm7h-b1675603ab.json
3+
cs3219-g24-staging-firebase-adminsdk-suafv-9c0d1b2299.json
34
test_create_api_results.txt
45
test_list_api_results.txt
56
test_update_api_results.txt

apps/question-service/Dockerfile

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
FROM golang:1.23
2+
3+
WORKDIR /usr/src/app
4+
5+
# pre-copy/cache go.mod for pre-downloading dependencies and only redownloading them in subsequent builds if they change
6+
COPY go.mod go.sum ./
7+
RUN go mod tidy && go mod download && go mod verify
8+
9+
COPY . .
10+
11+
RUN go build -v -o /usr/local/bin/app ./main.go
12+
13+
EXPOSE 8080
14+
15+
CMD ["app"]

apps/question-service/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,20 @@ go run main.go
4949

5050
The server will be available at http://localhost:8080.
5151

52+
## Running the Application via Docker
53+
54+
To run the application via Docker, run the following command:
55+
56+
```bash
57+
docker build -t question-service .
58+
```
59+
60+
```bash
61+
docker run -p 8080:8080 -d question-service
62+
```
63+
64+
The server will be available at http://localhost:8080.
65+
5266
## API Endpoints
5367

5468
- `POST /questions`

apps/question-service/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ require (
2525
github.com/go-chi/cors v1.2.1
2626
github.com/go-logr/logr v1.4.2 // indirect
2727
github.com/go-logr/stdr v1.2.2 // indirect
28-
github.com/golang-jwt/jwt/v4 v4.5.0 // indirect
28+
github.com/golang-jwt/jwt/v4 v4.5.0
2929
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
3030
github.com/golang/protobuf v1.5.4 // indirect
3131
github.com/google/s2a-go v0.1.8 // indirect

apps/question-service/main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"net/http"
99
"os"
1010
"question-service/handlers"
11+
mymiddleware "question-service/middleware"
1112
"question-service/utils"
1213
"time"
1314

@@ -65,6 +66,7 @@ func main() {
6566
r := chi.NewRouter()
6667
r.Use(middleware.Logger)
6768
r.Use(middleware.Timeout(60 * time.Second))
69+
r.Use(mymiddleware.VerifyJWT)
6870

6971
r.Use(cors.Handler(cors.Options{
7072
// AllowedOrigins: []string{"http://localhost:3000"}, // Use this to allow specific origin hosts
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package middleware
2+
3+
import (
4+
"github.com/golang-jwt/jwt/v4"
5+
"net/http"
6+
"os"
7+
"strings"
8+
)
9+
10+
func VerifyJWT(next http.Handler) http.Handler {
11+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
12+
// Get the token from the Authorization header
13+
authHeader := r.Header.Get("Authorization")
14+
if authHeader == "" {
15+
http.Error(w, "Authorization header is missing", http.StatusUnauthorized)
16+
return
17+
}
18+
19+
// Split the header to get the token
20+
tokenString := strings.Split(authHeader, " ")[1]
21+
22+
// Retrieve the JWT secret from environment variables
23+
jwtSecret := []byte(os.Getenv("JWT_SECRET"))
24+
if jwtSecret == nil {
25+
http.Error(w, "JWT secret is not set", http.StatusInternalServerError)
26+
return
27+
}
28+
29+
// Parse the token
30+
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
31+
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
32+
return nil, http.ErrNotSupported
33+
}
34+
return jwtSecret, nil
35+
})
36+
37+
if err != nil || !token.Valid {
38+
http.Error(w, "Invalid token", http.StatusUnauthorized)
39+
return
40+
}
41+
42+
// Optionally, you can extract claims from the token and attach them to the request context
43+
next.ServeHTTP(w, r)
44+
})
45+
}

apps/user-service/Dockerfile

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,15 @@ ARG DB_CLOUD_URI
44
ARG JWT_SECRET
55
ENV DB_CLOUD_URI=${DB_CLOUD_URI}
66
ENV JWT_SECRET=${JWT_SECRET}
7-
# ARG NODE_ENV=development
8-
# ENV NODE_ENV=${NODE_ENV}
97
ENV ENV=PROD
108

11-
FROM base as deps
9+
FROM base AS deps
1210

13-
# RUN apk add --no-cache libc6-compat
1411
WORKDIR /app
1512

1613
COPY package*.json ./
1714

1815
RUN npm install
19-
RUN npm audit fix --force
2016

2117
# Uncomment the following line if you are building code for production.
2218
# RUN npm ci --omit=dev

0 commit comments

Comments
 (0)