Skip to content

Commit 5ff42ba

Browse files
committed
Refactor code
1 parent 7d8c9ab commit 5ff42ba

File tree

8 files changed

+37
-37
lines changed

8 files changed

+37
-37
lines changed

internal/app/app.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ import (
44
"context"
55
"github.com/core-go/health"
66
"github.com/gocql/gocql"
7-
"go-service/cql"
8-
"go-service/internal/handlers"
9-
"go-service/internal/services"
107
"time"
8+
9+
"go-service/internal/handler"
10+
"go-service/internal/service"
11+
"go-service/pkg/cql"
1112
)
1213

1314
const (
@@ -27,18 +28,18 @@ const (
2728
)
2829

2930
type ApplicationContext struct {
30-
HealthHandler *health.Handler
31-
UserHandler *handlers.UserHandler
31+
Health *health.Handler
32+
User *handler.UserHandler
3233
}
3334

34-
func NewApp(context context.Context, root Config) (*ApplicationContext, error) {
35+
func NewApp(ctx context.Context, config Config) (*ApplicationContext, error) {
3536
// connect to the cluster
36-
cluster := gocql.NewCluster(root.Cql.PublicIp)
37+
cluster := gocql.NewCluster(config.Cql.PublicIp)
3738
cluster.Consistency = gocql.Quorum
3839
cluster.ProtoVersion = 4
3940
cluster.Timeout = time.Second * 1000
4041
cluster.ConnectTimeout = time.Second * 1000
41-
cluster.Authenticator = gocql.PasswordAuthenticator{Username: root.Cql.UserName, Password: root.Cql.Password}
42+
cluster.Authenticator = gocql.PasswordAuthenticator{Username: config.Cql.UserName, Password: config.Cql.Password}
4243
session, err := cluster.CreateSession()
4344
if err != nil {
4445
return nil, err
@@ -66,14 +67,14 @@ func NewApp(context context.Context, root Config) (*ApplicationContext, error) {
6667
return nil, err
6768
}
6869

69-
userService := services.NewUserService(cluster)
70-
userHandler := handlers.NewUserHandler(userService)
70+
userService := service.NewUserService(cluster)
71+
userHandler := handler.NewUserHandler(userService)
7172

7273
cqlChecker := cql.NewHealthChecker(cluster)
7374
healthHandler := health.NewHandler(cqlChecker)
7475

7576
return &ApplicationContext{
76-
HealthHandler: healthHandler,
77-
UserHandler: userHandler,
77+
Health: healthHandler,
78+
User: userHandler,
7879
}, nil
7980
}

internal/app/route.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ const (
99
GET = "GET"
1010
POST = "POST"
1111
PUT = "PUT"
12-
PATCH = "PATCH"
1312
DELETE = "DELETE"
1413
)
1514

@@ -19,14 +18,14 @@ func Route(r *mux.Router, ctx context.Context, root Config) error {
1918
return err
2019
}
2120

22-
r.HandleFunc("/health", app.HealthHandler.Check).Methods(GET)
21+
r.HandleFunc("/health", app.Health.Check).Methods(GET)
2322

2423
userPath := "/users"
25-
r.HandleFunc(userPath, app.UserHandler.All).Methods(GET)
26-
r.HandleFunc(userPath+"/{id}", app.UserHandler.Load).Methods(GET)
27-
r.HandleFunc(userPath, app.UserHandler.Insert).Methods(POST)
28-
r.HandleFunc(userPath+"/{id}", app.UserHandler.Update).Methods(PUT)
29-
r.HandleFunc(userPath+"/{id}", app.UserHandler.Delete).Methods(DELETE)
24+
r.HandleFunc(userPath, app.User.All).Methods(GET)
25+
r.HandleFunc(userPath+"/{id}", app.User.Load).Methods(GET)
26+
r.HandleFunc(userPath, app.User.Insert).Methods(POST)
27+
r.HandleFunc(userPath+"/{id}", app.User.Update).Methods(PUT)
28+
r.HandleFunc(userPath+"/{id}", app.User.Delete).Methods(DELETE)
3029

3130
return nil
3231
}

internal/handlers/user_handler.go renamed to internal/handler/user_handler.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
package handlers
1+
package handler
22

33
import (
44
"encoding/json"
55
"github.com/gorilla/mux"
66
"net/http"
77

8-
. "go-service/internal/models"
9-
. "go-service/internal/services"
8+
. "go-service/internal/model"
9+
. "go-service/internal/service"
1010
)
1111

1212
type UserHandler struct {

internal/models/user.go renamed to internal/model/user.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package models
1+
package model
22

33
import "time"
44

internal/services/cql_user_service.go renamed to internal/service/cql_user_service.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
package services
1+
package service
22

33
import (
44
"context"
55
"fmt"
66
"github.com/gocql/gocql"
77
"strings"
88

9-
. "go-service/internal/models"
9+
. "go-service/internal/model"
1010
)
1111

1212
type CqlUserService struct {

internal/services/user_service.go renamed to internal/service/user_service.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
package services
1+
package service
22

33
import (
44
"context"
55

6-
. "go-service/internal/models"
6+
. "go-service/internal/model"
77
)
88

99
type UserService interface {

main.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import (
44
"context"
55
"fmt"
66
"github.com/core-go/config"
7+
"github.com/core-go/core"
78
"github.com/core-go/log"
89
mid "github.com/core-go/log/middleware"
9-
sv "github.com/core-go/core"
1010
"github.com/gorilla/mux"
1111
"net/http"
1212

@@ -15,9 +15,9 @@ import (
1515

1616
func main() {
1717
var conf app.Config
18-
er1 := config.Load(&conf, "configs/config")
19-
if er1 != nil {
20-
panic(er1)
18+
err := config.Load(&conf, "configs/config")
19+
if err != nil {
20+
panic(err)
2121
}
2222

2323
r := mux.NewRouter()
@@ -30,12 +30,12 @@ func main() {
3030
}
3131
r.Use(mid.Recover(log.PanicMsg))
3232

33-
er2 := app.Route(r, context.Background(), conf)
34-
if er2 != nil {
35-
panic(er2)
33+
err = app.Route(r, context.Background(), conf)
34+
if err != nil {
35+
panic(err)
3636
}
37-
fmt.Println(sv.ServerInfo(conf.Server))
38-
if er3 := http.ListenAndServe(sv.Addr(conf.Server.Port), r); er3 != nil {
39-
fmt.Println(er3.Error())
37+
fmt.Println(core.ServerInfo(conf.Server))
38+
if err = http.ListenAndServe(core.Addr(conf.Server.Port), r); err != nil {
39+
fmt.Println(err.Error())
4040
}
4141
}
File renamed without changes.

0 commit comments

Comments
 (0)