-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
117 lines (102 loc) · 2.65 KB
/
server.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
package main
import (
"bytes"
"database/sql"
"encoding/json"
_ "github.com/jackc/pgx/stdlib"
"go.stockscraper/reddit"
"html/template"
"log"
"net/http"
"os"
"strconv"
"time"
)
var subreddits = []string{"wallstreetbets", "pennystocks", "CanadianInvestor", "StockMarket", "Daytrading", "stocks"}
var sorts = []string{"hot", "new", "top"}
const insertSQL = "INSERT INTO requests(date, ip, requested_limit) VALUES($1, $2, $3)"
var DB *sql.DB
var connectedToDatabase = true
func main(){
db, err := sql.Open("pgx", os.Getenv("DATABASE_URL"))
if err != nil {
log.Print("Database not connected")
connectedToDatabase = false
} else {
if err = db.Ping(); err != nil {
log.Print("Lost connection to database")
connectedToDatabase = false
} else {
DB = db
}
}
http.HandleFunc("/", serveIndex)
http.HandleFunc("/json", getJson)
log.Println("Running...")
if err := http.ListenAndServe(":"+os.Getenv("PORT"), nil); err != nil {
log.Fatal(err)
}
}
func getJson (w http.ResponseWriter, r *http.Request){
var subArray []reddit.SubReddits
limit, err := strconv.Atoi(r.URL.Query().Get("limit"))
if err != nil {
limit = 10
}
subChannel := make(chan reddit.SubReddits)
defer close(subChannel)
for _, subreddit := range subreddits {
for _, sort := range sorts {
go func(r, s string, l int) {
subChannel <- reddit.GetTickers(r, s, l)
}(subreddit, sort, limit)
}
}
for i:= 0; i < len(subreddits) * len(sorts); i++ {
subArray = append(subArray, <-subChannel)
}
subArray = append(subArray, reddit.SumTotal(subArray))
buf := &bytes.Buffer{}
enc := json.NewEncoder(buf)
enc.SetEscapeHTML(true)
if err = enc.Encode(subArray); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(200)
_, err = w.Write(buf.Bytes())
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func serveIndex (w http.ResponseWriter, r *http.Request){
limit, err := strconv.Atoi(r.URL.Query().Get("limit"))
if err != nil {
limit = 10
}
if connectedToDatabase {
ip := getIP(r)
go func(limit int, ip string) {
t := time.Now()
query, err := DB.Prepare(insertSQL)
defer query.Close()
if err == nil {
_, err = query.Exec(t.Format("2006-01-02"), ip, limit)
}
}(limit, ip)
}
tmpl := template.Must(template.ParseFiles("templates/index.html"))
err = tmpl.Execute(w, limit)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
func getIP(r *http.Request) string {
forwarded := r.Header.Get("X-FORWARDED-FOR")
if forwarded != "" {
return forwarded
}
return r.RemoteAddr
}