-
Notifications
You must be signed in to change notification settings - Fork 0
/
dartmoney-server.go
76 lines (56 loc) · 1.66 KB
/
dartmoney-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
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
"time"
"github.com/gin-gonic/gin"
"github.com/richscott/dartmoney/db"
)
func main() {
if os.Getenv("ALPHAVANTAGE_API_KEY") == "" {
log.Fatal("Error: environment variable ALPHAVANTAGE_API_KEY is not set")
}
db.CreateSchema()
router := gin.Default()
router.Static("/css", "./webroot/css")
router.Static("/js", "./webroot/js")
router.Static("/html", "./webroot/html")
router.StaticFile("/", "./webroot/html/index.html")
router.StaticFile("/favicon.ico", "./webroot/img/favicon.ico")
router.GET("/api/quotes/:symbols", symbolsQuote)
router.GET("/api/portfolio/:userId", userPositions)
router.Run(":8000")
}
func symbolsQuote(c *gin.Context) {
// Get your free key from https://www.alphavantage.co
apiKey := os.Getenv("ALPHAVANTAGE_API_KEY")
const baseURL = "https://www.alphavantage.co/query"
const fetchTimeout = time.Duration(10 * time.Second)
symbols := strings.Split(c.Param("symbols"), ",")
quoteURL := fmt.Sprintf("%s?function=BATCH_STOCK_QUOTES&symbols=%s&apikey=%s",
baseURL, strings.Join(symbols, ","), apiKey)
httpClient := http.Client{Timeout: fetchTimeout}
resp, err := httpClient.Get(quoteURL)
if err != nil {
log.Print(err)
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Print(fmt.Printf("could not get body when fetching quote: %v\n", err))
return
}
c.Data(http.StatusOK, "application/json", body)
}
func buyEquity(c *gin.Context) {
}
func userPositions(c *gin.Context) {
//userId := c.Param("userId")
userPositions := db.UserPositions("investor@somewhere")
c.JSON(http.StatusOK, userPositions)
}