Skip to content
This repository has been archived by the owner on Oct 22, 2024. It is now read-only.

Commit

Permalink
proxy: make it possible for frontend code to access base/drive (#141)
Browse files Browse the repository at this point in the history
  • Loading branch information
nullptropy authored Aug 21, 2023
1 parent d93dbf4 commit a0fdb72
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 8 deletions.
2 changes: 1 addition & 1 deletion cmd/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ func dev(projectDir string, projectID string, host string, port int, open bool)
}

time.Sleep(3 * time.Second)
proxy := proxy.NewReverseProxy(meta.ID, meta.Name, meta.Alias)
proxy := proxy.NewReverseProxy(projectKey, meta.ID, meta.Name, meta.Alias)
if err := loadMicrosFromDir(proxy, spacefile.Micros, routeDir); err != nil {
return err
}
Expand Down
8 changes: 7 additions & 1 deletion cmd/dev_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,13 @@ func devProxy(projectDir string, host string, port int, open bool) error {
return err
}

reverseProxy := proxy.NewReverseProxy(meta.ID, meta.Name, meta.Alias)
projectKey, err := utils.GenerateDataKeyIfNotExists(meta.ID)
if err != nil {
utils.Logger.Printf("%s Error generating the project key", emoji.ErrorExclamation)
return err
}

reverseProxy := proxy.NewReverseProxy(projectKey, meta.ID, meta.Name, meta.Alias)
if err := loadMicrosFromDir(reverseProxy, spacefile.Micros, microDir); err != nil {
return err
}
Expand Down
62 changes: 56 additions & 6 deletions internal/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,25 @@ package proxy
import (
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
"net/http/httputil"
"net/url"
"regexp"
"strings"

"github.com/deta/space/shared"
)

const (
actionEndpoint = "/__space/actions"
actionEndpoint = "/__space/actions"
clientBaseEndpoint = "/__space/v0/base"
clientDriveEndpoint = "/__space/v0/drive"
)

const (
baseHost = "database.deta.sh"
driveHost = "drive.deta.sh"
)

type ProxyEndpoint struct {
Expand Down Expand Up @@ -52,15 +60,19 @@ type ReverseProxy struct {
instanceAlias string
prefixToProxy map[string]*httputil.ReverseProxy
actionMap map[string]ProxyAction
projectKey string
client *http.Client
}

func NewReverseProxy(appID string, appName string, instanceAlias string) *ReverseProxy {
func NewReverseProxy(projectKey string, appID string, appName string, instanceAlias string) *ReverseProxy {
return &ReverseProxy{
appID: appID,
appName: appName,
instanceAlias: instanceAlias,
prefixToProxy: make(map[string]*httputil.ReverseProxy),
actionMap: make(map[string]ProxyAction),
projectKey: projectKey,
client: &http.Client{},
}
}

Expand Down Expand Up @@ -124,7 +136,47 @@ func extractPrefix(path string) string {
return "/"
}

func (p *ReverseProxy) ServeClientSDKAuth(targetHost string, w http.ResponseWriter, r *http.Request) {
newURL := *r.URL
newURL.Host = targetHost
newURL.Scheme = "https"
newURL.Path = regexp.MustCompile("^/__space/v0/(drive|base)/v1/[^/]+").
ReplaceAllString(newURL.Path, "/v1/"+strings.Split(p.projectKey, "_")[0])

newReq, err := http.NewRequest(r.Method, newURL.String(), r.Body)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

newReq.Header.Add("X-API-Key", p.projectKey)
newReq.Header.Add("Content-Type", "application/json")
resp, err := p.client.Do(newReq)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer resp.Body.Close()

for key, values := range resp.Header {
for _, value := range values {
w.Header().Add(key, value)
}
}
w.WriteHeader(resp.StatusCode)
io.Copy(w, resp.Body)
}

func (p *ReverseProxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if strings.HasPrefix(r.URL.Path, clientBaseEndpoint) {
p.ServeClientSDKAuth(baseHost, w, r)
return
}
if strings.HasPrefix(r.URL.Path, clientDriveEndpoint) {
p.ServeClientSDKAuth(driveHost, w, r)
return
}

if r.URL.Path == actionEndpoint {
switch r.Method {
case http.MethodOptions:
Expand Down Expand Up @@ -190,7 +242,7 @@ func (p *ReverseProxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
defer resp.Body.Close()

body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
Expand Down Expand Up @@ -237,6 +289,4 @@ func (p *ReverseProxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
fallback.ServeHTTP(w, r)
return
}

http.NotFound(w, r)
}

0 comments on commit a0fdb72

Please sign in to comment.