This commit is contained in:
parent
8f0f0e715b
commit
6ec2179931
|
@ -0,0 +1,7 @@
|
|||
module app
|
||||
|
||||
go 1.18
|
||||
|
||||
require gfx.cafe/open/gun v0.0.3
|
||||
|
||||
require gopkg.in/yaml.v3 v3.0.1 // indirect
|
|
@ -0,0 +1,6 @@
|
|||
gfx.cafe/open/gun v0.0.3 h1:gHED0923ITIb+jD/8+g6sahK/7w4E722p9QwnYOeUmU=
|
||||
gfx.cafe/open/gun v0.0.3/go.mod h1:bz41aOGPVKhSRXOEvaNcgkL7fjrl4XZPdy/VybZbKoM=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
|
@ -0,0 +1,94 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"io"
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"gfx.cafe/open/gun"
|
||||
)
|
||||
|
||||
var Config struct {
|
||||
Port string
|
||||
Root string
|
||||
}
|
||||
|
||||
func init() {
|
||||
gun.Load(&Config)
|
||||
if Config.Port == "" {
|
||||
Config.Port = "8080"
|
||||
}
|
||||
if Config.Root == "" {
|
||||
Config.Root = "./dist"
|
||||
}
|
||||
}
|
||||
|
||||
var hopHeaders = []string{
|
||||
"Connection",
|
||||
"Keep-Alive",
|
||||
"Proxy-Authenticate",
|
||||
"Proxy-Authorization",
|
||||
"Te",
|
||||
"Trailers",
|
||||
"Transfer-Encoding",
|
||||
"Upgrade",
|
||||
}
|
||||
|
||||
func appendHostToXForwardHeader(header http.Header, host string) {
|
||||
if prior, ok := header["X-Forwarded-For"]; ok {
|
||||
host = strings.Join(prior, ", ") + ", " + host
|
||||
}
|
||||
header.Set("X-Forwarded-For", host)
|
||||
}
|
||||
|
||||
func copyHeader(dst, src http.Header) {
|
||||
for k, vv := range src {
|
||||
for _, v := range vv {
|
||||
dst.Add(k, v)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func delHopHeaders(header http.Header) {
|
||||
for _, h := range hopHeaders {
|
||||
header.Del(h)
|
||||
}
|
||||
}
|
||||
|
||||
type Handler struct {
|
||||
c http.Client
|
||||
}
|
||||
|
||||
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
if strings.HasPrefix(strings.TrimPrefix("/", r.URL.Path), "lifeto") {
|
||||
h.handleProxy(w, r)
|
||||
return
|
||||
}
|
||||
h.handleProxy(w, r)
|
||||
}
|
||||
func (h *Handler) handleProxy(w http.ResponseWriter, r *http.Request) {
|
||||
r.RequestURI = ""
|
||||
if clientIP, _, err := net.SplitHostPort(r.RemoteAddr); err == nil {
|
||||
appendHostToXForwardHeader(r.Header, clientIP)
|
||||
}
|
||||
resp, err := h.c.Do(r)
|
||||
if err != nil {
|
||||
http.Error(w, "Server Error", http.StatusInternalServerError)
|
||||
log.Println("ServeHTTP:", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
delHopHeaders(resp.Header)
|
||||
copyHeader(w.Header(), resp.Header)
|
||||
w.WriteHeader(resp.StatusCode)
|
||||
io.Copy(w, resp.Body)
|
||||
}
|
||||
|
||||
func (h *Handler) handleSite(w http.ResponseWriter, r *http.Request) {
|
||||
http.StripPrefix(Config.Root, http.FileServer(http.Dir(Config.Root))).ServeHTTP(w, r)
|
||||
}
|
||||
|
||||
func main() {
|
||||
http.ListenAndServe(":"+Config.Port, &Handler{})
|
||||
}
|
|
@ -5,7 +5,6 @@ import vue from '@vitejs/plugin-vue'
|
|||
export default defineConfig({
|
||||
plugins: [vue()],
|
||||
server: {
|
||||
https: true,
|
||||
proxy: {
|
||||
// with options
|
||||
'/lifeto': {
|
||||
|
|
Loading…
Reference in New Issue