-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
71 lines (56 loc) · 1.2 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
package main
import (
"log"
"net/http"
"os"
"time"
"github.com/gorilla/mux"
"gorm.io/gorm"
)
const intervalFreq = 30
const filename = "promotions.csv"
type App struct {
db *gorm.DB
}
func main() {
host := getenv("DB_HOST", "localhost")
user := getenv("DB_USER", "postgres")
pass := getenv("DB_PASS", "postgres")
name := getenv("DB_NAME", "casestudy")
port := getenv("DB_PORT", "5433")
db, err := ConnectToDB(host, user, pass, name, port)
if err != nil {
log.Fatal(err)
}
app := &App{db: db}
if err := db.AutoMigrate(&Promotion{}); err != nil {
log.Fatalf("error automigrating table: %v", err.Error())
}
parseCaseStudyCSV(db, filename)
ticker := time.NewTicker(intervalFreq * time.Minute)
defer ticker.Stop()
go func() {
for {
select {
case <-ticker.C:
parseCaseStudyCSV(db, filename)
}
}
}()
r := mux.NewRouter()
r.HandleFunc("/promotions/{id}", app.GetPromotionByID)
srv := &http.Server{
Handler: r,
Addr: "localhost:8080",
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
}
log.Fatal(srv.ListenAndServe())
}
func getenv(envname, dflt string) string {
env := os.Getenv(envname)
if env == "" {
return dflt
}
return env
}