Skip to content

Commit

Permalink
dx
Browse files Browse the repository at this point in the history
  • Loading branch information
GuyARoss committed Oct 5, 2024
1 parent 97ad5ee commit 17bddc8
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 16 deletions.
8 changes: 4 additions & 4 deletions examples/basic-react/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,26 @@ func main() {
panic(err)
}

orb.HandleFunc("/", func(c *orbitgen.Request) {
orb.SetPageProps(orbitgen.ExampleTwoPage, func() map[string]interface{} {
now := time.Now()

props := make(map[string]interface{})
props["day"] = now.Day()
props["month"] = now.Month()
props["year"] = now.Year()

c.RenderPage(orbitgen.ExamplePage, props)
return props
})

orb.HandleFunc("/second", func(c *orbitgen.Request) {
orb.HandleFunc("/", func(c *orbitgen.Request) {
now := time.Now()

props := make(map[string]interface{})
props["day"] = now.Day()
props["month"] = now.Month()
props["year"] = now.Year()

c.RenderPage(orbitgen.ExampleTwoPage, props)
c.RenderPage(orbitgen.ExamplePage, props)
})

err = http.ListenAndServe(":3030", orb.Serve())
Expand Down
29 changes: 19 additions & 10 deletions examples/basic-react/orbitgen/orb_env.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
)

func reactCSR(ctx context.Context, bundleKey string, data []byte, doc *htmlDoc) (*htmlDoc, context.Context) {
if v := ctx.Value(OrbitManifest); v == nil {
doc.Head = append(doc.Head, fmt.Sprintf(`<script id="orbit_manifest" type="application/json">%s</script>`, data))
Expand All @@ -20,28 +21,33 @@ func reactCSR(ctx context.Context, bundleKey string, data []byte, doc *htmlDoc)
}
return doc, ctx
}

var staticResourceMap = map[PageRender]bool{
ExampleTwoPage: true,
ExamplePage: false,
ExamplePage: false,
}
var serverStartupTasks = []func(){}

type RenderFunction func(context.Context, string, []byte, *htmlDoc) (*htmlDoc, context.Context)

var wrapDocRender = map[PageRender]*DocumentRenderer{
ExampleTwoPage: {fn: reactCSR, version: "reactCSR"},
ExamplePage: {fn: reactCSR, version: "reactCSR"},
ExamplePage: {fn: reactCSR, version: "reactCSR"},
}

type DocumentRenderer struct {
fn RenderFunction
fn RenderFunction
version string
}

var bundleDir string = ".orbit/dist"

var publicDir string = "./public/index.html"
var hotReloadPort int = 0

type PageRender string

const (
const (
// orbit:page .//pages/example2.jsx
ExampleTwoPage PageRender = "fe9faa2750e8559c8c213c2c25c4ce73"
// orbit:page .//pages/example.jsx
Expand All @@ -50,14 +56,13 @@ const (

var pageDependencies = map[PageRender][]string{
ExampleTwoPage: {`<script src="/p/fc38086145547d465be97fec2e412a16.js"></script>`,
`<script src="/p/a63649d90703a7b09f22aed8d310be5b.js"></script>`,
},
`<script src="/p/a63649d90703a7b09f22aed8d310be5b.js"></script>`,
},
ExamplePage: {`<script src="/p/fc38086145547d465be97fec2e412a16.js"></script>`,
`<script src="/p/a63649d90703a7b09f22aed8d310be5b.js"></script>`,
},
`<script src="/p/a63649d90703a7b09f22aed8d310be5b.js"></script>`,
},
}


type HydrationCtxKey string

const (
Expand All @@ -71,4 +76,8 @@ const (
ProdBundleMode BundleMode = 1
)

var CurrentDevMode BundleMode = ProdBundleMode
var CurrentDevMode BundleMode = ProdBundleMode

var routeTable = map[PageRender]string{
ExampleTwoPage: "/second",
}
41 changes: 39 additions & 2 deletions examples/basic-react/orbitgen/orb_http.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"strings"
"sync"
)

// Request is the standard request payload for the orbit page handler
// this is just a fancy wrapper around the http request & response that will also assist
// the rendering of bundled pages & incoming path slugs
Expand All @@ -22,15 +23,18 @@ type Request struct {
Response http.ResponseWriter
Slugs map[string]string
}

// DefaultPage defines the standard behavior for a orbit page handler
type DefaultPage interface {
Handle(*Request)
}

// htmlDoc represents a basic document model that will be rendered upon build request
type htmlDoc struct {
Head []string
Body []string
}

// render renders the document out to a single string
func (s *htmlDoc) render() string {
return fmt.Sprintf(`
Expand All @@ -45,6 +49,7 @@ func (s *htmlDoc) merge(doc *htmlDoc) *htmlDoc {
s.Head = append(doc.Head, s.Head...)
return s
}

// parseStaticDocument attempts to find the specified document and return it as a string
func parseStaticDocument(path string) (string, error) {
_, err := os.Stat(path)
Expand All @@ -54,6 +59,7 @@ func parseStaticDocument(path string) (string, error) {
}
return "", fmt.Errorf("static document does not exist for '%s'", publicDir)
}

// build buildHTMLPages creates the htmldocument given data for orbits manifest and the page's
func buildHTMLPages(data []byte, pages ...PageRender) *htmlDoc {
body := make([]string, 0)
Expand Down Expand Up @@ -93,11 +99,13 @@ func buildHTMLPages(data []byte, pages ...PageRender) *htmlDoc {
}
return html
}

// innerHTML is a utility function that assists with the parsing the content of html tags
// it does this by returning the subset of the two provided strings "subStart" & "subEnd"
func innerHTML(str string, subStart string, subEnd string) string {
return strings.Split(strings.Join(strings.Split(str, subStart)[1:], ""), subEnd)[0]
}

// defaultHTMLDoc builds a standard html doc for orbit that also verifies the public directory
// if override data exits, then it will use that as a base for the HTML document
func defaultHTMLDoc(override string) *htmlDoc {
Expand All @@ -116,6 +124,7 @@ func defaultHTMLDoc(override string) *htmlDoc {
}
return base
}

// parseSlug will parse slugs from the incoming path provided initial slugKeys and return a map of the slugs
// in map[string]string form where the key is the slug name & value is the value as it appears in the path
func parseSlug(slugKeys map[int]string, path string) map[string]string {
Expand All @@ -131,6 +140,7 @@ func parseSlug(slugKeys map[int]string, path string) map[string]string {
}
return slugs
}

// parsePathSlugs will parse initial slugs found in a path, these slugs can be identified with
// the "{" char prepended & "}" appended to the path/subpath e.g "/path/{thing}" will represent "thing" as a slug key.
func parsePathSlugs(path string) (map[int]string, string) {
Expand All @@ -155,6 +165,7 @@ func parsePathSlugs(path string) (map[int]string, string) {
}
return slugKeys, finalPath
}

// muxHandle is used to inject the base mux handler behavior
type MuxHandler interface {
HandleFunc(string, func(http.ResponseWriter, *http.Request))
Expand All @@ -164,7 +175,15 @@ type MuxHandler interface {
type Serve struct {
mux MuxHandler
doc *htmlDoc

pagePropHandlers map[PageRender]func() map[string]interface{}
}

func (s *Serve) SetPageProps(page PageRender, handler func() map[string]interface{}) {
// TODO: ensure that this is in the route table
s.pagePropHandlers[page] = handler
}

// HandleFunc attaches a handler to the specified pattern, this handler will be
// ran upon a match of the request path during an incoming http request.
func (s *Serve) HandleFunc(path string, handler func(c *Request)) {
Expand Down Expand Up @@ -222,22 +241,26 @@ func (s *Serve) HandleFunc(path string, handler func(c *Request)) {
handler(ctx)
})
}

// HandlePage attaches an orbit page to the specified pattern, this handler will be
// ran upon a match of the request path during an incoming http request
func (s *Serve) HandlePage(path string, dp DefaultPage) {
s.HandleFunc(path, dp.Handle)
}

type gzipResponseWriter struct {
io.Writer
http.ResponseWriter
}

func (w *gzipResponseWriter) WriteHeader(status int) {
w.Header().Del("Content-Length")
w.ResponseWriter.WriteHeader(status)
}
func (w *gzipResponseWriter) Write(b []byte) (int, error) {
return w.Writer.Write(b)
}

// setupMuxRequirements creates the required mux handlers for orbit, these include
// - fileserver for the bundle directory bound to the "/p/" directory
func (s *Serve) setupMuxRequirements() *Serve {
Expand Down Expand Up @@ -268,8 +291,21 @@ func (s *Serve) setupMuxRequirements() *Serve {
})
return s
}

// Serve returns the mux server
func (s *Serve) Serve() MuxHandler {
// rebind data from the route table
for k, v := range routeTable {
s.HandleFunc(v, func(c *Request) {
props := make(map[string]interface{})
if s.pagePropHandlers[k] != nil {
props = s.pagePropHandlers[k]()
}

c.RenderPage(k, props)
})
}

return s.mux
}
func setupDoc() *htmlDoc {
Expand All @@ -287,7 +323,8 @@ func New() (*Serve, error) {
sfn()
}
return (&Serve{
mux: http.NewServeMux(),
doc: setupDoc(),
mux: http.NewServeMux(),
doc: setupDoc(),
pagePropHandlers: map[PageRender]func() map[string]interface{}{},
}).setupMuxRequirements(), nil
}

0 comments on commit 17bddc8

Please sign in to comment.