Skip to content

Commit

Permalink
Merge pull request #1 from johnsiilver/README
Browse files Browse the repository at this point in the history
Readme and examples
  • Loading branch information
element-of-surprise authored Mar 29, 2021
2 parents b49744d + da8f044 commit 81e6ac5
Show file tree
Hide file tree
Showing 46 changed files with 1,894 additions and 150 deletions.
561 changes: 561 additions & 0 deletions README.md

Large diffs are not rendered by default.

5 changes: 2 additions & 3 deletions component/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,6 @@ var scriptTemplateTxt = `
document.body.replaceChild(newcomp, old);
}
}
console.log("hello world");
{{.Self.LoaderName}}();
</script>
{{ end }}
Expand Down Expand Up @@ -231,7 +230,7 @@ func New(name string, doc *html.Doc, options ...Option) (*Gear, error) {
for walked := range html.Walker(walkCtx, doc.Body) {
if g, ok := walked.Element.(*Gear); ok {
cancel()
return nil, fmt.Errorf("WebGear Component(%s) had another component(%s) added directly to the passed *html.Doc," +
return nil, fmt.Errorf("WebGear Component(%s) had another component(%s) added directly to the passed *html.Doc,"+
"this can only be added using the component.AddGear() option to allow correct rendered ordering", name, g.name)
}
}
Expand Down Expand Up @@ -263,7 +262,7 @@ func (g *Gear) TagType() template.HTMLAttr {
// to HTML with this name (output with TemplateName() and LoaderName()), but we still need a way
// to reference them in our Doc tree. This is that ID.
func (g *Gear) GearID() string {
return "gear-"+ g.name
return "gear-" + g.name
}

// LoaderName is the name of the JS function that defines the HTML custom element and causes the custom element to render.
Expand Down
2 changes: 1 addition & 1 deletion component/viewer/viewer.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,4 @@ func (v *Viewer) Run() {
log.Printf("http server serving on :%d", v.port)

log.Fatal(server.ListenAndServe())
}
}
24 changes: 12 additions & 12 deletions component/viewer/wasm/wasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import (
"fmt"
"log"
"net/http"
"time"
"net/url"
"os"
"os/exec"
"path/filepath"
"net/url"
"sync"
"time"

"github.com/johnsiilver/webgear/handlers"
wasmHTTP "github.com/johnsiilver/webgear/wasm/http"
Expand All @@ -18,8 +18,8 @@ import (
// Viewer provides an HTTP server that will run to view an individual component that
// is inside a wasm binary.
type Viewer struct {
port int
binName string
port int
binName string
useModules bool

mu sync.Mutex
Expand Down Expand Up @@ -56,9 +56,9 @@ func UseModules() Option {
// relaunched by simply hitting 'r' in the terminal.
func New(port int, binName string, options ...Option) *Viewer {
v := &Viewer{
port: port,
port: port,
binName: binName,
h: handlers.New(handlers.DoNotCache()),
h: handlers.New(handlers.DoNotCache()),
}

for _, o := range options {
Expand All @@ -69,7 +69,7 @@ func New(port int, binName string, options ...Option) *Viewer {
panic(err)
}

u, err := url.Parse("/static/viewer/main/"+binName)
u, err := url.Parse("/static/viewer/main/" + binName)
if err != nil {
panic(err)
}
Expand All @@ -94,7 +94,7 @@ func (v *Viewer) build() error {

if v.useModules {
os.Setenv("GO111MODULE", "on")
}else{
} else {
os.Setenv("GO111MODULE", "off")
}
os.Setenv("GOOS", "js")
Expand All @@ -114,18 +114,18 @@ func (v *Viewer) build() error {
// Run runs the viewer and will block forever.
func (v *Viewer) Run() {
// disable input buffering
exec.Command("stty", "-F", "/dev/tty", "cbreak", "min", "1").Run()
exec.Command("stty", "-F", "/dev/tty", "cbreak", "min", "1").Run()
go func() {
var b []byte = make([]byte, 1)
for {
os.Stdin.Read(b)
if string(b) == "r" {
if err := v.build(); err == nil {
fmt.Println("wasm rebuild SUCCEEDED, reload web browser")
}else{
} else {
fmt.Println("wasm rebuild FAILED: ", err)
}
}else{
} else {
fmt.Printf("I don't know what %q is\n", string(b))
}
}
Expand All @@ -142,4 +142,4 @@ func (v *Viewer) Run() {
log.Printf("http server serving on :%d", v.port)

log.Fatal(server.ListenAndServe())
}
}
3 changes: 0 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ require (
github.com/grpc-ecosystem/grpc-gateway v1.14.8
github.com/johnsiilver/go_basics v0.0.0-20200612183708-9254a13bbede
github.com/kylelemons/godebug v1.1.0
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826
github.com/ulule/deepcopier v0.0.0-20200430083143-45decc6639b6
golang.org/x/net v0.0.0-20200602114024-627f9648deb9
google.golang.org/genproto v0.0.0-20200513103714-09dca8ec2884
google.golang.org/grpc v1.31.1
google.golang.org/protobuf v1.23.0
Expand Down
9 changes: 5 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.4.0 h1:xsAVV57WRhGj6kEIi8ReJzQlHHqcBYCElAvkovg3B/4=
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/grpc-ecosystem/grpc-gateway v1.14.8 h1:hXClj+iFpmLM8i3lkO6i4Psli4P2qObQuQReiII26U8=
github.com/grpc-ecosystem/grpc-gateway v1.14.8/go.mod h1:NZE8t6vs6TnwLL/ITkaK8W3ecMLGAbh2jXTclvpiwYo=
Expand All @@ -36,17 +37,14 @@ github.com/johnsiilver/go_basics v0.0.0-20200612183708-9254a13bbede/go.mod h1:ro
github.com/johnsiilver/webgear v0.0.0-20200612182948-28632aac6009/go.mod h1:cMq7KURJVuft5RBIFcKdgbFiB74D4JXL6PDD5kZNHZ0=
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 h1:RWengNIwukTxcDr9M+97sNutRR1RKhG96O6jWumTTnw=
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826/go.mod h1:TaXosZuwdSHYgviHp1DAtfrULt5eUgsSMsZf+YrPgl8=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/ulule/deepcopier v0.0.0-20200430083143-45decc6639b6 h1:TtyC78WMafNW8QFfv3TeP3yWNDG+uxNkk9vOrnDu6JA=
github.com/ulule/deepcopier v0.0.0-20200430083143-45decc6639b6/go.mod h1:h8272+G2omSmi30fBXiZDMkmHuOgonplfKIKjQWzlfs=
github.com/yosssi/gohtml v0.0.0-20200519115854-476f5b4b8047/go.mod h1:+ccdNT0xMY1dtc5XBxumbYfOUhmduiGudqaDgD2rVRE=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
Expand All @@ -70,13 +68,15 @@ golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd h1:xhmwyvizuTgC2qz7ZlMluP20uW+C3Rm0FD/WLDX8884=
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY=
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
Expand All @@ -97,6 +97,7 @@ google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miE
google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo=
google.golang.org/protobuf v1.23.0 h1:4MY060fB1DLGMB/7MBTLnwQUY6+F09GEiz6SsrNqyzM=
google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
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.v2 v2.2.3 h1:fvjTMHxHEw/mxHbtzPi3JCcKXQRAnQTBRo6YCJSVHKI=
gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
Expand Down
2 changes: 1 addition & 1 deletion html/a.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ var aTmpl = template.Must(template.New("a").Parse(strings.TrimSpace(`
// A defines a hyperlink, which is used to link from one page to another.
type A struct {
// Href specifies the URL of the page the link goes to.
Href string
Href *url.URL
// Download specifies that the target will be downloaded when a user clicks on the hyperlink.
Download bool `html:"attr"`
// HrefLang specifies the language of the linked document.
Expand Down
2 changes: 1 addition & 1 deletion html/a_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestA(t *testing.T) {
GlobalAttrs: GlobalAttrs{
AccessKey: "key",
},
Href: "/subpage",
Href: URLParse("/subpage"),
Download: true,
HrefLang: "english",
Media: "query",
Expand Down
8 changes: 8 additions & 0 deletions html/body.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,13 @@ func (b *Body) validate() error {
if b == nil {
return fmt.Errorf("Body element is not defined")
}
for _, e := range b.Elements {
v, ok := e.(validator)
if ok {
if err := v.validate(); err != nil {
return err
}
}
}
return nil
}
2 changes: 1 addition & 1 deletion html/body_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestBody(t *testing.T) {
Elements: []Element{
&Div{
Elements: []Element{
&A{Href: "/subpage", Elements: []Element{TextElement("hello")}},
&A{Href: URLParse("/subpage"), Elements: []Element{TextElement("hello")}},
},
},
},
Expand Down
2 changes: 1 addition & 1 deletion html/div_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestDiv(t *testing.T) {
},
Events: (&Events{}).AddScript(OnError, "handleError"),
Elements: []Element{
&A{Href: "/subpage", Elements: []Element{TextElement("hello")}},
&A{Href: URLParse("/subpage"), Elements: []Element{TextElement("hello")}},
},
},

Expand Down
75 changes: 75 additions & 0 deletions html/examples/basic/basic.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package main

import (
"flag"
"fmt"
"log"
"net/http"
"time"

"github.com/johnsiilver/webgear/handlers"

. "github.com/johnsiilver/webgear/html"
)

var (
dev = flag.Bool("dev", false, "Prevents the browser from caching content when doing development")
port = flag.Int("port", 9568, "The port to server on")
)

func main() {
flag.Parse()

doc := &Doc{
Head: &Head{
Elements: []Element{
&Meta{Charset: "utf-8"},
&Title{TagValue: TextElement("Hello World")},
&Link{
Rel: "stylesheet",
Href: URLParse("/static/index/index.css"),
},
},
},
Body: &Body{
Elements: []Element{
&H{
GlobalAttrs: GlobalAttrs{
Class: "pageText",
},
Level: 1,
Elements: []Element{
TextElement("Hello World"),
},
},
},
},
}

opts := []handlers.Option{}
if *dev {
opts = append(
opts,
handlers.DoNotCache(),
)
}

h := handlers.New(opts...)

// Serves up files ending with .css from /static/...
h.ServeFilesWorkingDir([]string{".css"})

// Our doc will now be served at the index page.
h.MustHandle("/", doc)

// Serve the content using the http.Server.
server := &http.Server{
Addr: fmt.Sprintf(":%d", *port),
Handler: h.ServerMux(),
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
}
log.Printf("http server serving on :%d", *port)
log.Fatal(server.ListenAndServe())
}
4 changes: 4 additions & 0 deletions html/examples/basic/index/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.pageText{
font-family: "Cursive";
color: blue;
}
Loading

0 comments on commit 81e6ac5

Please sign in to comment.