-
Notifications
You must be signed in to change notification settings - Fork 11
/
main.go
76 lines (62 loc) · 1.94 KB
/
main.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
package main
import (
"database/sql"
"fmt"
"log"
"net/http"
"os"
"strconv"
"github.com/gorilla/mux"
_ "github.com/lib/pq"
"github.com/rubinthomasdev/go-employee/employee/adapter/in/web"
"github.com/rubinthomasdev/go-employee/employee/adapter/out/db/postgres"
"github.com/rubinthomasdev/go-employee/employee/adapter/out/persistence"
"github.com/rubinthomasdev/go-employee/employee/application/service"
)
func main() {
// ** inmem start **
// create DB connection
// inMemDB := inmemdb.InMemDB{}
// create repo
// getEmpRepo := inmemdb.InMemRepository{Db: &inMemDB}
// getEmpRepo.Initialize()
// ** inmem end **
// ** postgres start **
// create DB connection
host := os.Getenv("PG_HOST")
port, _ := strconv.Atoi(os.Getenv("PG_PORT"))
user := os.Getenv("PG_USER")
password := os.Getenv("PG_PASSWORD")
dbname := os.Getenv("PG_DBNAME")
psqlInfo := fmt.Sprintf("host=%s port=%d user=%s "+
"password=%s dbname=%s sslmode=disable",
host, port, user, password, dbname)
db, err := sql.Open("postgres", psqlInfo)
if err != nil {
panic(err)
}
defer db.Close()
err = db.Ping()
if err != nil {
panic(err)
}
// create repo
getEmpRepo := postgres.PostgresRepository{DB: db}
// ** postgres end **
// create adapter
getEmpAdapter := persistence.EmployeePersistenceAdapter{EmployeeRepo: getEmpRepo, Mapper: persistence.EmployeeDataMapper{}}
// create service
getEmpSvc := service.GetEmployeeDetailsService{LoadEmployeeDataPort: getEmpAdapter}
// create handler
getEmpHandler := web.GetEmployeeDataHandler{GetEmployeeDataUseCase: getEmpSvc}
//http handler registration
// http.HandleFunc("/api/v1/employees/", getEmpHandler.GetEmployeeDetails)
// gorilla mux start
r := mux.NewRouter()
r.HandleFunc("/api/v1/employees/{empid}", getEmpHandler.GetEmployeeDetails)
r.HandleFunc("/api/v1/employees", getEmpHandler.GetEmployeeDetails)
http.Handle("/", r)
// gorilla mux end
//start the server
log.Fatal(http.ListenAndServe(":8080", nil))
}