-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
131 lines (105 loc) · 2.69 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
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
package main
import (
"context"
"database/sql"
"flag"
"fmt"
"log"
"net/http"
"time"
"github.com/go-playground/form/v4"
"github.com/julienschmidt/httprouter"
_ "github.com/lib/pq"
)
type TemplateData struct {
StockAssets *[]StockAsset
}
type StockAsset struct {
ID int
Ticker string
Quantity int // for now
Price int
}
type StockRepo struct {
stockAssets *[]StockAsset
nextID int
}
type config struct {
port int
db struct {
dsn string
maxOpenConns int
maxIdleConns int
maxIdleTime string
}
}
type application struct {
stockRepo *StockRepo
decoder *form.Decoder
config config
}
func main() {
router := httprouter.New()
var cfg config
flag.IntVar(&cfg.port, "port", 4000, "Web app server port")
flag.StringVar(&cfg.db.dsn,
"db-dsn",
"postgres://foliage:[email protected]/foliage?port=54043&sslmode=disable",
"PostgreSQL DSN")
flag.IntVar(&cfg.db.maxOpenConns, "db-max-open-conns", 25, "PostgreSQL max open connections")
flag.IntVar(&cfg.db.maxIdleConns, "db-max-i-conns", 25, "PostgreSQL max open connections")
flag.StringVar(&cfg.db.maxIdleTime, "db-max-i-time", "15m", "PostgreSQL max connection idle time")
app := &application{
stockRepo: initStockAssets(),
decoder: form.NewDecoder(),
config: cfg,
}
router.HandlerFunc(http.MethodGet, "/", app.home)
router.HandlerFunc(http.MethodPost, "/assets/", app.assetsCreate)
_, err := openDB(app.config)
if err != nil {
fmt.Println("DB misconfigured")
return
}
server := &http.Server{
Addr: ":4000",
Handler: router,
}
log.Fatal(server.ListenAndServe())
}
func initStockAssets() *StockRepo {
return &StockRepo{
stockAssets: &[]StockAsset{
{ID: 1, Ticker: "AAPL", Price: 2990, Quantity: 100},
{ID: 2, Ticker: "TSLA", Price: 19901, Quantity: 100},
{ID: 3, Ticker: "JPM", Price: 19190, Quantity: 100},
}, nextID: 4,
}
}
func (stockRepo *StockRepo) insert(ticker string, quantity, price int) StockAsset {
time.Sleep(1 * time.Second) // to simulate though work
asset := StockAsset{ID: stockRepo.nextID, Ticker: ticker, Quantity: quantity, Price: price}
*stockRepo.stockAssets = append(*stockRepo.stockAssets, asset)
stockRepo.nextID += 1
return asset
}
func openDB(cfg config) (*sql.DB, error) {
db, err := sql.Open("postgres", cfg.db.dsn)
if err != nil {
return nil, err
}
db.SetMaxOpenConns(cfg.db.maxOpenConns)
db.SetMaxIdleConns(cfg.db.maxIdleConns)
duration, err := time.ParseDuration(cfg.db.maxIdleTime)
if err != nil {
return nil, err
}
db.SetConnMaxIdleTime(duration)
ctx, cancel := context.WithTimeout(context.Background(), 6*time.Second)
defer cancel()
err = db.PingContext(ctx)
if err != nil {
return nil, err
}
return db, nil
}