summary refs log tree commit diff stats
path: root/src/db.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/db.rs')
-rw-r--r--src/db.rs29
1 files changed, 26 insertions, 3 deletions
diff --git a/src/db.rs b/src/db.rs
index d3ecd6a..6c1214b 100644
--- a/src/db.rs
+++ b/src/db.rs
@@ -1,5 +1,8 @@
+use log::info;
+use rand;
 use rusqlite;
 use std::sync::mpsc;
+use std::time;
 
 #[derive(Debug)]
 pub struct Post {
@@ -9,9 +12,10 @@ pub struct Post {
     body: String,
 }
 
+#[derive(Debug)]
 pub struct Conn {
     db: rusqlite::Connection,
-    tx: mpsc::Sender<Cmd>,
+    rx: mpsc::Receiver<Cmd>,
 }
 
 #[derive(Debug)]
@@ -24,6 +28,8 @@ pub enum Cmd {
 
 impl Conn {
     fn init() -> rusqlite::Connection {
+        let start = time::Instant::now();
+        info!("Connecting to database");
         let conn = rusqlite::Connection::open_with_flags(
             "/tmp/db.sql",
             rusqlite::OpenFlags::SQLITE_OPEN_FULL_MUTEX
@@ -43,13 +49,18 @@ impl Conn {
         )
         .expect("Could not initialize DB");
 
+        info!(
+            "Database connection established in {}ms",
+            start.elapsed().as_millis()
+        );
+
         conn
     }
 
-    pub fn new(tx: mpsc::Sender<Cmd>) -> Self {
+    pub fn new(rx: mpsc::Receiver<Cmd>) -> Self {
         Conn {
             db: Conn::init(),
-            tx,
+            rx,
         }
     }
 }
@@ -66,6 +77,18 @@ impl Cmd {
 }
 
 impl Post {
+    pub fn new(title: &str, author: &str, body: &str) -> Self {
+        let id = rand::random::<u32>();
+        let title = title.to_string();
+        let author = author.to_string();
+        let body = body.to_string();
+        Post {
+            id,
+            title,
+            author,
+            body,
+        }
+    }
     pub fn id(&self) -> String {
         format!("{}", self.id)
     }
/057static_dispatch.cc?h=hlt&id=3929c26cfc6e2dce8bfd119a722e4a8531886f96'>^
795f5244 ^
fb49fd2d ^
bcd33d6d ^




795f5244 ^
c157066c ^
39017bac ^


417db83d ^
fb49fd2d ^
39017bac ^
aba49e6c ^
795f5244 ^
39017bac ^









c55e49b6 ^

39017bac ^
c55e49b6 ^
39017bac ^

c55e49b6 ^

39017bac ^
c55e49b6 ^
39017bac ^














aba49e6c ^
39017bac ^
39017bac ^

















cc6c9e74 ^
17b90929 ^
ebdf923d ^
b2e4056d ^
39017bac ^


0c1791a9 ^
0c1791a9 ^


39017bac ^
0ddcab7b ^
0c1791a9 ^
39017bac ^


0c1791a9 ^
49c274f8 ^
c157066c ^
795f5244 ^
39017bac ^



795f5244 ^
39017bac ^


23692627 ^
39017bac ^


273f8be9 ^
fb49fd2d ^



795f5244 ^
39017bac ^









795f5244 ^
39017bac ^


795f5244 ^
39017bac ^





23692627 ^
795f5244 ^

39017bac ^







5683823a ^
39017bac ^





5683823a ^
39017bac ^




fb49fd2d ^




















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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214




















                                                                           

                                                                                                          
                     


                                                                                                                   
                                                  

   

                                          
                                                

                                                             
                                           
                                                           
                                                         
                                                                                         




                           
                                                          
                                                                                          


       
                                               
                                                                                   
                                                      
                                            
                                                                 









                                                                 

                                                                            
                   
     

                                                         

                                                                         
                   
     














                                                           
                                                 
                       

















                                                                           
                                                                      
                                                                                                                                                  
                                                        
                                                            


                                                
                                         


                                                                                                       
                                
                                                                    
                                              


   
                                                                           
                                                                   
                                                                     
                                                                                 



                                                                                           
                                                   


                                 
                                               


                                                                              
                                                              



                                            
                                                                               









                                                                         
                                                                  


                                                            
                                                                         





                                                                     
                                                                      

                                                                                                     







                                                             
          





                                                               
          




                                  




















                                                     
6'>1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785