Poll API, pass result to chan, pass from chan to Websocket. Panic












1















I'm writing a small package which does a GET request to an external API every 2 seconds. It takes the value from this request and passes it into a channel. I have made this channel available to a http.handler (chi router) which upgrades to a websocket where the front-end will grab the value in realtime. the panic error is a lot of lines but i guess the most important is this:



2018/11/14 16:47:55 http: response.WriteHeader on hijacked connection
2018/11/14 16:47:55 http: response.Write on hijacked connection


Aside from that I'm sure there is a better way of doing this. Any experienced Gophers out there have any pointers to help a noob such as myself improve this?



package currencyticker

import (
"bitbucket.org/special/api/config"
"encoding/json"
"fmt"
"github.com/go-chi/chi"
"github.com/go-chi/render"
"github.com/gorilla/websocket"
"github.com/leekchan/accounting"
"io/ioutil"
"log"
"math/big"
"net/http"
"time"
)

var (
ac = accounting.Accounting{Precision: 2}
from = "USD"
to = "EUR,SWK"
url = "https://min-api.currencyapi.com/data/price?fsym=" + from + "&tsyms=" + to
messages = make(chan float64)
)

var wsupgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
CheckOrigin: func(r *http.Request) bool {
return true // Disable CORS for testing
},
}

// Config - init
type Config struct {
*config.Config
}

type result map[string]float64

// New - init the configs
func New(configuration *config.Config) *Config {
return &Config{configuration}
}

// Routes - api urls
func (config *Config) Routes() *chi.Mux {
router := chi.NewRouter()
router.Use(
render.SetContentType(render.ContentTypeHTML), // Set content-Type headers as application/json
)
router.Get("/", config.GetPrice) // subscribe to new tweets

return router
}

func (config *Config) GetPrice(w http.ResponseWriter, r *http.Request) {

conn, err := wsupgrader.Upgrade(w, r, nil)
if err != nil {
fmt.Println(fmt.Printf("Failed to set websocket upgrade: %+v ", err))
return
}

for {
time.Sleep(1 * time.Second)

price := <-messages

w, err := conn.NextWriter(websocket.TextMessage)
if err != nil {
fmt.Println("ws error", err)
}
currVal := ac.FormatMoneyBigFloat(big.NewFloat(price))

if _, err := w.Write(byte(currVal)); err != nil {
fmt.Printf("w.Write() returned %v", err)
}

w.Close()

}

}

// start getting the price of ether as soon as they ap starts
func init() {
go startPollingPriceAPI()
}

// Go Routine to start polling
func startPollingPriceAPI() {
for {
time.Sleep(2 * time.Second)
go getPriceFromAPI()
}
}

func getPriceFromAPI() {

w := http.Client{
// Timeout: time.Second * 3,
}

req, _ := http.NewRequest(http.MethodGet, url, nil)

res, err := w.Do(req)

if err != nil {
log.Println("err getting price [req]: ", err)
}

body, err := ioutil.ReadAll(res.Body)

if err != nil {
log.Println("err getting price [io-read]: ", err)
}

r := result{}

if jsonErr := json.Unmarshal(body, &r); jsonErr != nil {
log.Println("err getting price [json]: ", jsonErr)
}
fmt.Println("1 Dollar = €", r["EUR"])

messages <- r["EUR"]

}









share|improve this question

























  • I don't know anything about chi, but my guess is that it's writing to the hijacked connection. Separate from this issue, the application should read from the connection. See the last paragraph in section of control message doc for information on this. If the application is panicking, then please share the stack trace fro the panic. The stack trace will likely point directly to the problem.

    – ThunderCat
    Nov 14 '18 at 16:34


















1















I'm writing a small package which does a GET request to an external API every 2 seconds. It takes the value from this request and passes it into a channel. I have made this channel available to a http.handler (chi router) which upgrades to a websocket where the front-end will grab the value in realtime. the panic error is a lot of lines but i guess the most important is this:



2018/11/14 16:47:55 http: response.WriteHeader on hijacked connection
2018/11/14 16:47:55 http: response.Write on hijacked connection


Aside from that I'm sure there is a better way of doing this. Any experienced Gophers out there have any pointers to help a noob such as myself improve this?



package currencyticker

import (
"bitbucket.org/special/api/config"
"encoding/json"
"fmt"
"github.com/go-chi/chi"
"github.com/go-chi/render"
"github.com/gorilla/websocket"
"github.com/leekchan/accounting"
"io/ioutil"
"log"
"math/big"
"net/http"
"time"
)

var (
ac = accounting.Accounting{Precision: 2}
from = "USD"
to = "EUR,SWK"
url = "https://min-api.currencyapi.com/data/price?fsym=" + from + "&tsyms=" + to
messages = make(chan float64)
)

var wsupgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
CheckOrigin: func(r *http.Request) bool {
return true // Disable CORS for testing
},
}

// Config - init
type Config struct {
*config.Config
}

type result map[string]float64

// New - init the configs
func New(configuration *config.Config) *Config {
return &Config{configuration}
}

// Routes - api urls
func (config *Config) Routes() *chi.Mux {
router := chi.NewRouter()
router.Use(
render.SetContentType(render.ContentTypeHTML), // Set content-Type headers as application/json
)
router.Get("/", config.GetPrice) // subscribe to new tweets

return router
}

func (config *Config) GetPrice(w http.ResponseWriter, r *http.Request) {

conn, err := wsupgrader.Upgrade(w, r, nil)
if err != nil {
fmt.Println(fmt.Printf("Failed to set websocket upgrade: %+v ", err))
return
}

for {
time.Sleep(1 * time.Second)

price := <-messages

w, err := conn.NextWriter(websocket.TextMessage)
if err != nil {
fmt.Println("ws error", err)
}
currVal := ac.FormatMoneyBigFloat(big.NewFloat(price))

if _, err := w.Write(byte(currVal)); err != nil {
fmt.Printf("w.Write() returned %v", err)
}

w.Close()

}

}

// start getting the price of ether as soon as they ap starts
func init() {
go startPollingPriceAPI()
}

// Go Routine to start polling
func startPollingPriceAPI() {
for {
time.Sleep(2 * time.Second)
go getPriceFromAPI()
}
}

func getPriceFromAPI() {

w := http.Client{
// Timeout: time.Second * 3,
}

req, _ := http.NewRequest(http.MethodGet, url, nil)

res, err := w.Do(req)

if err != nil {
log.Println("err getting price [req]: ", err)
}

body, err := ioutil.ReadAll(res.Body)

if err != nil {
log.Println("err getting price [io-read]: ", err)
}

r := result{}

if jsonErr := json.Unmarshal(body, &r); jsonErr != nil {
log.Println("err getting price [json]: ", jsonErr)
}
fmt.Println("1 Dollar = €", r["EUR"])

messages <- r["EUR"]

}









share|improve this question

























  • I don't know anything about chi, but my guess is that it's writing to the hijacked connection. Separate from this issue, the application should read from the connection. See the last paragraph in section of control message doc for information on this. If the application is panicking, then please share the stack trace fro the panic. The stack trace will likely point directly to the problem.

    – ThunderCat
    Nov 14 '18 at 16:34
















1












1








1








I'm writing a small package which does a GET request to an external API every 2 seconds. It takes the value from this request and passes it into a channel. I have made this channel available to a http.handler (chi router) which upgrades to a websocket where the front-end will grab the value in realtime. the panic error is a lot of lines but i guess the most important is this:



2018/11/14 16:47:55 http: response.WriteHeader on hijacked connection
2018/11/14 16:47:55 http: response.Write on hijacked connection


Aside from that I'm sure there is a better way of doing this. Any experienced Gophers out there have any pointers to help a noob such as myself improve this?



package currencyticker

import (
"bitbucket.org/special/api/config"
"encoding/json"
"fmt"
"github.com/go-chi/chi"
"github.com/go-chi/render"
"github.com/gorilla/websocket"
"github.com/leekchan/accounting"
"io/ioutil"
"log"
"math/big"
"net/http"
"time"
)

var (
ac = accounting.Accounting{Precision: 2}
from = "USD"
to = "EUR,SWK"
url = "https://min-api.currencyapi.com/data/price?fsym=" + from + "&tsyms=" + to
messages = make(chan float64)
)

var wsupgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
CheckOrigin: func(r *http.Request) bool {
return true // Disable CORS for testing
},
}

// Config - init
type Config struct {
*config.Config
}

type result map[string]float64

// New - init the configs
func New(configuration *config.Config) *Config {
return &Config{configuration}
}

// Routes - api urls
func (config *Config) Routes() *chi.Mux {
router := chi.NewRouter()
router.Use(
render.SetContentType(render.ContentTypeHTML), // Set content-Type headers as application/json
)
router.Get("/", config.GetPrice) // subscribe to new tweets

return router
}

func (config *Config) GetPrice(w http.ResponseWriter, r *http.Request) {

conn, err := wsupgrader.Upgrade(w, r, nil)
if err != nil {
fmt.Println(fmt.Printf("Failed to set websocket upgrade: %+v ", err))
return
}

for {
time.Sleep(1 * time.Second)

price := <-messages

w, err := conn.NextWriter(websocket.TextMessage)
if err != nil {
fmt.Println("ws error", err)
}
currVal := ac.FormatMoneyBigFloat(big.NewFloat(price))

if _, err := w.Write(byte(currVal)); err != nil {
fmt.Printf("w.Write() returned %v", err)
}

w.Close()

}

}

// start getting the price of ether as soon as they ap starts
func init() {
go startPollingPriceAPI()
}

// Go Routine to start polling
func startPollingPriceAPI() {
for {
time.Sleep(2 * time.Second)
go getPriceFromAPI()
}
}

func getPriceFromAPI() {

w := http.Client{
// Timeout: time.Second * 3,
}

req, _ := http.NewRequest(http.MethodGet, url, nil)

res, err := w.Do(req)

if err != nil {
log.Println("err getting price [req]: ", err)
}

body, err := ioutil.ReadAll(res.Body)

if err != nil {
log.Println("err getting price [io-read]: ", err)
}

r := result{}

if jsonErr := json.Unmarshal(body, &r); jsonErr != nil {
log.Println("err getting price [json]: ", jsonErr)
}
fmt.Println("1 Dollar = €", r["EUR"])

messages <- r["EUR"]

}









share|improve this question
















I'm writing a small package which does a GET request to an external API every 2 seconds. It takes the value from this request and passes it into a channel. I have made this channel available to a http.handler (chi router) which upgrades to a websocket where the front-end will grab the value in realtime. the panic error is a lot of lines but i guess the most important is this:



2018/11/14 16:47:55 http: response.WriteHeader on hijacked connection
2018/11/14 16:47:55 http: response.Write on hijacked connection


Aside from that I'm sure there is a better way of doing this. Any experienced Gophers out there have any pointers to help a noob such as myself improve this?



package currencyticker

import (
"bitbucket.org/special/api/config"
"encoding/json"
"fmt"
"github.com/go-chi/chi"
"github.com/go-chi/render"
"github.com/gorilla/websocket"
"github.com/leekchan/accounting"
"io/ioutil"
"log"
"math/big"
"net/http"
"time"
)

var (
ac = accounting.Accounting{Precision: 2}
from = "USD"
to = "EUR,SWK"
url = "https://min-api.currencyapi.com/data/price?fsym=" + from + "&tsyms=" + to
messages = make(chan float64)
)

var wsupgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
CheckOrigin: func(r *http.Request) bool {
return true // Disable CORS for testing
},
}

// Config - init
type Config struct {
*config.Config
}

type result map[string]float64

// New - init the configs
func New(configuration *config.Config) *Config {
return &Config{configuration}
}

// Routes - api urls
func (config *Config) Routes() *chi.Mux {
router := chi.NewRouter()
router.Use(
render.SetContentType(render.ContentTypeHTML), // Set content-Type headers as application/json
)
router.Get("/", config.GetPrice) // subscribe to new tweets

return router
}

func (config *Config) GetPrice(w http.ResponseWriter, r *http.Request) {

conn, err := wsupgrader.Upgrade(w, r, nil)
if err != nil {
fmt.Println(fmt.Printf("Failed to set websocket upgrade: %+v ", err))
return
}

for {
time.Sleep(1 * time.Second)

price := <-messages

w, err := conn.NextWriter(websocket.TextMessage)
if err != nil {
fmt.Println("ws error", err)
}
currVal := ac.FormatMoneyBigFloat(big.NewFloat(price))

if _, err := w.Write(byte(currVal)); err != nil {
fmt.Printf("w.Write() returned %v", err)
}

w.Close()

}

}

// start getting the price of ether as soon as they ap starts
func init() {
go startPollingPriceAPI()
}

// Go Routine to start polling
func startPollingPriceAPI() {
for {
time.Sleep(2 * time.Second)
go getPriceFromAPI()
}
}

func getPriceFromAPI() {

w := http.Client{
// Timeout: time.Second * 3,
}

req, _ := http.NewRequest(http.MethodGet, url, nil)

res, err := w.Do(req)

if err != nil {
log.Println("err getting price [req]: ", err)
}

body, err := ioutil.ReadAll(res.Body)

if err != nil {
log.Println("err getting price [io-read]: ", err)
}

r := result{}

if jsonErr := json.Unmarshal(body, &r); jsonErr != nil {
log.Println("err getting price [json]: ", jsonErr)
}
fmt.Println("1 Dollar = €", r["EUR"])

messages <- r["EUR"]

}






go






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 0:07









dlsniper

3,17611326




3,17611326










asked Nov 14 '18 at 16:00









KravitzKravitz

5714827




5714827













  • I don't know anything about chi, but my guess is that it's writing to the hijacked connection. Separate from this issue, the application should read from the connection. See the last paragraph in section of control message doc for information on this. If the application is panicking, then please share the stack trace fro the panic. The stack trace will likely point directly to the problem.

    – ThunderCat
    Nov 14 '18 at 16:34





















  • I don't know anything about chi, but my guess is that it's writing to the hijacked connection. Separate from this issue, the application should read from the connection. See the last paragraph in section of control message doc for information on this. If the application is panicking, then please share the stack trace fro the panic. The stack trace will likely point directly to the problem.

    – ThunderCat
    Nov 14 '18 at 16:34



















I don't know anything about chi, but my guess is that it's writing to the hijacked connection. Separate from this issue, the application should read from the connection. See the last paragraph in section of control message doc for information on this. If the application is panicking, then please share the stack trace fro the panic. The stack trace will likely point directly to the problem.

– ThunderCat
Nov 14 '18 at 16:34







I don't know anything about chi, but my guess is that it's writing to the hijacked connection. Separate from this issue, the application should read from the connection. See the last paragraph in section of control message doc for information on this. If the application is panicking, then please share the stack trace fro the panic. The stack trace will likely point directly to the problem.

– ThunderCat
Nov 14 '18 at 16:34














0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53304243%2fpoll-api-pass-result-to-chan-pass-from-chan-to-websocket-panic%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53304243%2fpoll-api-pass-result-to-chan-pass-from-chan-to-websocket-panic%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

List item for chat from Array inside array React Native

Thiostrepton

Caerphilly