-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathredis-gorilla
More file actions
110 lines (90 loc) · 3.07 KB
/
redis-gorilla
File metadata and controls
110 lines (90 loc) · 3.07 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package main
import (
"database/sql"
"encoding/json"
"log"
"net/http"
"github.com/go-redis/redis/v8"
"github.com/gorilla/mux"
_ "github.com/lib/pq"
"github.com/spf13/viper"
"golang.org/x/net/context"
)
var (
redisClient *redis.Client
db *sql.DB
)
// PostgreSQLDatabaseService represents a service to interact with PostgreSQL database.
type PostgreSQLDatabaseService struct {
DB *sql.DB
}
// NewPostgreSQLDatabaseService creates a new PostgreSQLDatabaseService instance.
func NewPostgreSQLDatabaseService(db *sql.DB) *PostgreSQLDatabaseService {
return &PostgreSQLDatabaseService{DB: db}
}
// GetCourier retrieves a courier's information from the PostgreSQL database.
func (s *PostgreSQLDatabaseService) GetCourier(courierID int) (Courier, error) {
// Implement your database query logic here to retrieve courier data.
// Example:
var courier Courier
err := s.DB.QueryRow("SELECT id, name, contact FROM couriers WHERE id = $1", courierID).Scan(&courier.ID, &courier.Name, &courier.Contact)
if err != nil {
return Courier{}, err
}
return courier, nil
}
// ...
func main() {
// Load configuration using Viper
viper.SetConfigName("config") // Load a configuration file named config.yaml or config.json, etc.
viper.AddConfigPath("config/") // Path to the directory where your config file is located
viper.ReadInConfig()
// Initialize the Redis client
redisClient = redis.NewClient(&redis.Options{
Addr: viper.GetString("redis.address"),
})
// Initialize the PostgreSQL database connection
db, err := sql.Open("postgres", viper.GetString("database.connection_string"))
if err != nil {
log.Fatalf("Failed to connect to PostgreSQL database: %v", err)
}
defer db.Close()
// Create a PostgreSQLDatabaseService instance
databaseService := NewPostgreSQLDatabaseService(db)
r := mux.NewRouter()
// Define a route for updating courier information
r.HandleFunc("/update_courier_info", UpdateCourierInfo(databaseService)).Methods("POST")
http.Handle("/", r)
log.Fatal(http.ListenAndServe(":8080", nil))
}
// UpdateCourierInfo is the handler for updating courier information.
func UpdateCourierInfo(dbService *PostgreSQLDatabaseService) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var updatedCourier Courier
err := json.NewDecoder(r.Body).Decode(&updatedCourier)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
// Store the updated courier information in Redis
key := "courier:" + string(updatedCourier.ID)
data, err := json.Marshal(updatedCourier)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
ctx := context.Background()
err = redisClient.Set(ctx, key, string(data), 0).Err()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Return a response, e.g., confirming the update
response := map[string]interface{}{
"message": "Courier information updated successfully",
"updated_data": updatedCourier,
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(response)
}
}