PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, June 25, 2022

[FIXED] How can I integrate javascript hot reload into golang routing with https for development?

 June 25, 2022     go, reverse-proxy, svelte-3     No comments   

Issue

I would like to set up the next simple development setup for golang + svelte

Frontend part

$ npx degit sveltejs/template frontend
$ yarn
$ yarn dev # it start the frontend env on http://localhost:5000

So I have a running frontend on http://localhost:5000

Backend part

  • go net/http router with https on :443 with self signed cert created with mkcert
  • https://localhost/hello -> go handlers for show normal go handlers
  • https://localhost/ -> reverse proxied http://localhost:5000
package main

import (
   "bytes"
   "io/ioutil"
   "log"
   "net/http"
   "net/http/httputil"
   "net/url"
   "strconv"
)

func newDirector(origin url.URL) func(*http.Request) {
   return func(req *http.Request) {
       req.Header.Add("X-Forwarded-Host", req.Host)
       req.Header.Add("X-Origin-Host", origin.Host)
       req.URL.Scheme = "http"
       req.URL.Host = origin.Host
   }
}

func newReplacer(orig, replace string) func(resp *http.Response) error {
   return func(resp *http.Response) error {
       b, err := ioutil.ReadAll(resp.Body)
       if err != nil {
           return err
       }

       err = resp.Body.Close()
       if err != nil {
           return err
       }

       b = bytes.Replace(b, []byte(orig), []byte(replace), -1)
       body := ioutil.NopCloser(bytes.NewReader(b))

       resp.Body = body
       resp.ContentLength = int64(len(b))
       resp.Header.Set("Content-Length", strconv.Itoa(len(b)))

       return nil
   }
}

func Frontend(w http.ResponseWriter, r *http.Request) {
   origin, _ := url.Parse("http://localhost:5000/")
   director := newDirector(*origin)
   proxy := &httputil.ReverseProxy{Director: director}
   proxy.ServeHTTP(w, r)
}

func liverload_js(w http.ResponseWriter, r *http.Request) {
   origin, _ := url.Parse("http://localhost:35729/")
   director := newDirector(*origin)
   modifier := newReplacer("this.port = 35729;", "this.port = 443;")
   proxy := &httputil.ReverseProxy{Director: director, ModifyResponse: modifier}
   proxy.ServeHTTP(w, r)
}

func liverload_ws(w http.ResponseWriter, r *http.Request) {
   origin, _ := url.Parse("http://localhost:35729/")
   director := newDirector(*origin)
   proxy := &httputil.ReverseProxy{Director: director}
   proxy.ServeHTTP(w, r)
}

func Bundle_js(w http.ResponseWriter, r *http.Request) {
   origin, _ := url.Parse("http://localhost:5000/")
   director := newDirector(*origin)
   modifier := newReplacer(":35729/livereload.js?snipver=1", ":443/livereload.js?snipver=1")
   proxy := &httputil.ReverseProxy{Director: director, ModifyResponse: modifier}
   proxy.ServeHTTP(w, r)
}

func main() {
   http.HandleFunc("/build/bundle.js", Bundle_js)
   http.HandleFunc("/livereload.js", liverload_js)
   http.HandleFunc("/livereload", liverload_ws)
   http.HandleFunc("/", Frontend)
   log.Fatal(http.ListenAndServeTLS(":443", "cert.pem", "key.pem", nil))
}

It is reload with pushing the F5 but the hot reload not goes trough the ws proxy.

How can it be included into the proxy ?


Solution

Freelancer helped me to find the answer. The trick was that I used Scheme="http" in each proxied request even httputil.ReverseProxy support websocket natively.

func newDirector(origin url.URL) func(*http.Request) {
   return func(req *http.Request) {
       req.Header.Add("X-Forwarded-Host", req.Host)
       req.Header.Add("X-Origin-Host", origin.Host)
       req.URL.Scheme = "http"
       req.URL.Host = origin.Host
   }
}

should be

func newDirector(origin url.URL) func(*http.Request) {
   return func(req *http.Request) {
       req.Header.Add("X-Forwarded-Host", req.Host)
       req.Header.Add("X-Origin-Host", origin.Host)
       req.URL.Scheme = origin.Scheme
       req.URL.Host = origin.Host
   }
}

The complete code became

package main

import (
    "bytes"
    "io/ioutil"
    "log"
    "net/http"
    "net/http/httputil"
    "net/url"
    "strconv"
)

func newDirector(origin url.URL) func(*http.Request) {
    return func(req *http.Request) {
        req.Header.Add("X-Forwarded-Host", req.Host)
        req.Header.Add("X-Origin-Host", origin.Host)
        req.URL.Scheme = origin.Scheme
        req.URL.Host = origin.Host
    }
}

func newReplacer(orig, replace string) func(resp *http.Response) error {
    return func(resp *http.Response) error {
        b, err := ioutil.ReadAll(resp.Body)
        if err != nil {
            return err
        }

        err = resp.Body.Close()
        if err != nil {
            return err
        }

        b = bytes.Replace(b, []byte(orig), []byte(replace), -1)
        body := ioutil.NopCloser(bytes.NewReader(b))

        resp.Body = body
        resp.ContentLength = int64(len(b))
        resp.Header.Set("Content-Length", strconv.Itoa(len(b)))

        return nil
    }
}

func Frontend(w http.ResponseWriter, r *http.Request) {
    origin, _ := url.Parse("http://localhost:5000/")
    director := newDirector(*origin)
    proxy := &httputil.ReverseProxy{Director: director}
    proxy.ServeHTTP(w, r)
}

func liverload_js(w http.ResponseWriter, r *http.Request) {
    origin, _ := url.Parse("http://localhost:35729/")
    director := newDirector(*origin)
    modifier := newReplacer("this.port = 35729;", "this.port = 443;")
    proxy := &httputil.ReverseProxy{Director: director, ModifyResponse: modifier}
    proxy.ServeHTTP(w, r)
}

func liverload_ws(w http.ResponseWriter, r *http.Request) {
    origin, _ := url.Parse("http://localhost:35729/")
    director := newDirector(*origin)
    proxy := &httputil.ReverseProxy{Director: director}
    proxy.ServeHTTP(w, r)
}

func Bundle_js(w http.ResponseWriter, r *http.Request) {
    origin, _ := url.Parse("http://localhost:5000/")
    director := newDirector(*origin)
    modifier := newReplacer(":35729/livereload.js?snipver=1", ":443/livereload.js?snipver=1")
    proxy := &httputil.ReverseProxy{Director: director, ModifyResponse: modifier}
    proxy.ServeHTTP(w, r)
}

func main() {
    http.HandleFunc("/build/bundle.js", Bundle_js)
    http.HandleFunc("/livereload.js", liverload_js)
    http.HandleFunc("/livereload", liverload_ws)
    http.HandleFunc("/", Frontend)
    log.Fatal(http.ListenAndServeTLS(":443", "cert.pem", "key.pem", nil))
}


Answered By - jshepherd
Answer Checked By - Timothy Miller (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing