Skip to content

Commit fa55059

Browse files
authored
Merge pull request #27 from Edmartt/edmartt/chore/updates-dockerfile
Edmartt/chore/updates dockerfile
2 parents be27f4e + 8e05eaa commit fa55059

File tree

5 files changed

+42
-45
lines changed

5 files changed

+42
-45
lines changed

Dockerfile

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM golang:1.18-alpine
1+
FROM golang:1.18-alpine as base
22

33
RUN apk add build-base
44

@@ -9,16 +9,19 @@ ENV CGP_ENABLED=0
99
ENV GOOS=linux
1010
ENV GOARCH=amd64
1111

12-
COPY go.mod .
13-
COPY go.sum .
14-
15-
RUN go mod download
16-
17-
COPY *.go .
1812
COPY . .
1913

2014
RUN go build ./... && go build
2115

16+
#Second stage building
17+
18+
FROM alpine
19+
RUN apk update upgrade
20+
WORKDIR /app
21+
22+
COPY --from=base /app .
23+
RUN chmod +x go-authentication-api
24+
2225
EXPOSE 8081
2326

2427
CMD ["./go-authentication-api"]

internal/users/data/irepository.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import "github.com/Edmartt/go-authentication-api/internal/users/models"
44

55
var RepoAccessInterface IUserRepository
66

7-
type IUserRepository interface{
7+
type IUserRepository interface {
88
Find(id string) *models.User
9-
Create(user models.User) string
9+
Create(user *models.User) string
1010
}

internal/users/data/repository.go

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,61 +2,60 @@ package data
22

33
import (
44
"log"
5+
56
"github.com/Edmartt/go-authentication-api/internal/database"
67
"github.com/Edmartt/go-authentication-api/internal/users/models"
78
)
89

9-
type UserRepository struct{
10-
DB database.IConnection
11-
user models.User
10+
type UserRepository struct {
11+
DB database.IConnection
12+
user *models.User
1213
}
1314

14-
15-
func newRepository() database.IConnection{
15+
func newRepository() database.IConnection {
1616
db := UserRepository{
1717
DB: database.SQLite{},
1818
}
1919
return db.DB
2020
}
2121

22-
23-
func (data UserRepository) Find(username string) (*models.User){
22+
func (data UserRepository) Find(username string) *models.User {
2423
connection, connError := newRepository().GetConnection()
25-
if connError != nil{
24+
if connError != nil {
2625
log.Println("connection error")
2726
}
28-
27+
2928
connection.Where("username = ?", username).First(&data.user)
30-
31-
if data.user.Id == ""{
32-
return &data.user
29+
30+
if data.user.Id == "" {
31+
return data.user
3332
}
3433

35-
return &data.user
34+
return data.user
3635
}
3736

38-
func (data UserRepository) Create(user models.User) string{
37+
func (data UserRepository) Create(user *models.User) string {
3938
connection, connError := newRepository().GetConnection()
40-
if connError != nil{
39+
if connError != nil {
4140
log.Println("connection error")
4241
}
4342
connection.Create(&user)
4443
return user.Id
4544
}
4645

47-
func (data UserRepository) Update(user models.User){
46+
func (data UserRepository) Update(user models.User) {
4847
connection, connError := data.DB.GetConnection()
4948

50-
if connError != nil{
49+
if connError != nil {
5150
log.Println("connection error")
5251
}
5352
connection.Save(&user)
5453
}
5554

56-
func (data UserRepository) Delete(user models.User, id string){
55+
func (data UserRepository) Delete(user models.User, id string) {
5756
connection, connError := data.DB.GetConnection()
5857

59-
if connError != nil{
58+
if connError != nil {
6059
log.Println("connection error")
6160
}
6261
connection.Delete(&user, id)

internal/users/transport/handlers.go

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,36 +19,35 @@ import (
1919

2020
//Handler struct gives access to user data access layer
2121
type Handlers struct {
22-
user models.User
22+
user *models.User
2323
logResponse LoginResponse
2424
sigResponse SignupResponse
25-
wrapper jwt.JWTWrapper
25+
wrapper jwt.JWTWrapper
2626
}
2727

28-
func init(){
28+
func init() {
2929
data.RepoAccessInterface = data.UserRepository{}
3030
}
3131

3232
//Login endpoint
33-
func(h *Handlers) Login(w http.ResponseWriter, request *http.Request){
33+
func (h *Handlers) Login(w http.ResponseWriter, request *http.Request) {
3434

3535
reqBody, requestError := io.ReadAll(request.Body)
3636

37-
if requestError != nil{
37+
if requestError != nil {
3838
log.Println(requestError.Error())
3939
}
4040

4141
json.Unmarshal(reqBody, &h.user)
4242

4343
searchedUser := data.RepoAccessInterface.Find(h.user.Username)
4444

45-
46-
if searchedUser.Username == h.user.Username{
47-
if hasher.CheckHash(searchedUser.Password, h.user.Password){
45+
if searchedUser.Username == h.user.Username {
46+
if hasher.CheckHash(searchedUser.Password, h.user.Password) {
4847

4948
newToken, err := h.wrapper.GenerateJWT(h.user.Username, 5)
5049

51-
if err != nil{
50+
if err != nil {
5251
w.WriteHeader(http.StatusInternalServerError)
5352
return
5453
}
@@ -67,10 +66,9 @@ func(h *Handlers) Login(w http.ResponseWriter, request *http.Request){
6766
w.WriteHeader(http.StatusUnauthorized)
6867
json.NewEncoder(w).Encode("Username or Password Wrong")
6968

70-
return
7169
}
7270

73-
func (h *Handlers)Signup(w http.ResponseWriter, request *http.Request) {
71+
func (h *Handlers) Signup(w http.ResponseWriter, request *http.Request) {
7472

7573
h.user.Id = uuid.NewString()
7674
requestError := json.NewDecoder(request.Body).Decode(&h.user)
@@ -83,15 +81,13 @@ func (h *Handlers)Signup(w http.ResponseWriter, request *http.Request) {
8381
return
8482
}
8583

86-
8784
data.RepoAccessInterface.Create(h.user)
8885
w.WriteHeader(http.StatusCreated)
8986
h.sigResponse.Status = "User Created"
9087
json.NewEncoder(w).Encode(h.sigResponse)
91-
return
9288
}
9389

94-
func (h *Handlers)GetUserData(w http.ResponseWriter, request *http.Request){
90+
func (h *Handlers) GetUserData(w http.ResponseWriter, request *http.Request) {
9591

9692
uName := request.Context().Value("username") // value from mux context took from ValidateToken middleware
9793

@@ -101,9 +97,8 @@ func (h *Handlers)GetUserData(w http.ResponseWriter, request *http.Request){
10197
h.user.Username = data.Username
10298
h.user.Password = data.Password
10399
h.user.CreatedAt = data.CreatedAt
104-
100+
105101
data.Password = ""
106102
w.WriteHeader(http.StatusOK)
107103
json.NewEncoder(w).Encode(&data)
108-
return
109104
}

pkg/jwt/jwt.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func (wrap *JWTWrapper) GenerateJWT(attribute string, quantity time.Duration) (t
2424
Attribute: attribute,
2525
StandardClaims: jwt.StandardClaims{
2626
ExpiresAt: expirationTime.Unix(),
27-
Issuer: "Sam Sepiol",
27+
Issuer: "Edmartt",
2828
},
2929
}
3030
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)

0 commit comments

Comments
 (0)