-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Cleanup HTTP Handler Code * Fix tavern/ent go generate * fix http ui embedding * cleanup metadata.yml parsing
- Loading branch information
Showing
10 changed files
with
92 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,8 @@ | ||
//go:build ignore | ||
package ent | ||
|
||
package main | ||
|
||
import ( | ||
"log" | ||
|
||
"entgo.io/contrib/entgql" | ||
"entgo.io/ent/entc" | ||
"entgo.io/ent/entc/gen" | ||
) | ||
|
||
//go:generate go run -mod=mod generate.go | ||
//go:generate go run -mod=mod generate_code.go | ||
//go:generate /bin/sh -c "cd ../graphql && go run -mod=mod github.com/99designs/gqlgen" | ||
//go:generate /bin/sh -c "cat ../graphql/schema/* > ../graphql/schema.graphql" | ||
//go:generate /bin/sh -c "cp ../graphql/schema.graphql ../www/schema.graphql" | ||
//go:generate /bin/sh -c "cp ../graphql/schema.graphql ../../../implants/lib/tavern/graphql/schema.graphql" | ||
//go:generate /bin/sh -c "cd ../../../implants/lib/tavern && ./codegen.sh" | ||
|
||
func main() { | ||
log.Println("generating entgo") | ||
extensions, err := entgql.NewExtension( | ||
entgql.WithSchemaGenerator(), | ||
entgql.WithWhereInputs(true), | ||
entgql.WithSchemaPath("../graphql/schema/ent.graphql"), | ||
entgql.WithConfigPath("../graphql/gqlgen.yml"), | ||
) | ||
if err != nil { | ||
log.Fatalf("creating entgql extension: %v", err) | ||
} | ||
opts := []entc.Option{ | ||
entc.Extensions(extensions), | ||
entc.FeatureNames("privacy"), | ||
} | ||
|
||
if err := entc.Generate( | ||
"./schema", | ||
&gen.Config{}, | ||
opts..., | ||
); err != nil { | ||
log.Fatalf("running ent codegen: %v", err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
//go:build ignore | ||
|
||
package main | ||
|
||
import ( | ||
"log" | ||
|
||
"entgo.io/contrib/entgql" | ||
"entgo.io/ent/entc" | ||
"entgo.io/ent/entc/gen" | ||
) | ||
|
||
func main() { | ||
log.Println("generating entgo") | ||
extensions, err := entgql.NewExtension( | ||
entgql.WithSchemaGenerator(), | ||
entgql.WithWhereInputs(true), | ||
entgql.WithSchemaPath("../graphql/schema/ent.graphql"), | ||
entgql.WithConfigPath("../graphql/gqlgen.yml"), | ||
) | ||
if err != nil { | ||
log.Fatalf("creating entgql extension: %v", err) | ||
} | ||
opts := []entc.Option{ | ||
entc.Extensions(extensions), | ||
entc.FeatureNames("privacy"), | ||
} | ||
|
||
if err := entc.Generate( | ||
"./schema", | ||
&gen.Config{}, | ||
opts..., | ||
); err != nil { | ||
log.Fatalf("running ent codegen: %v", err) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,49 @@ | ||
package www | ||
|
||
import ( | ||
"embed" | ||
"fmt" | ||
"log" | ||
"net/http" | ||
"strings" | ||
|
||
"github.com/kcarretto/realm/tavern/internal/www/build" | ||
) | ||
|
||
// FallbackAppHandler is a custom handler for the single page react app - if the path doesn't exist the react app is returned. | ||
type FallbackAppHandler struct{} | ||
// Handler is a custom handler for the single page react app - if the path doesn't exist the react app is returned. | ||
type Handler struct { | ||
logger *log.Logger | ||
} | ||
|
||
func (h *FallbackAppHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
// Extract the path from the http request | ||
path := r.URL.Path[1:] | ||
// Try to read the file requested | ||
resp, err := build.Content.ReadFile(path) | ||
if err != nil { | ||
// If the file doesn't exist | ||
if strings.Contains(err.Error(), "file does not exist") { | ||
// Return our react apps main page and let it handle the route using react router. | ||
resp, err := build.Content.ReadFile("index.html") | ||
if err != nil { | ||
println("Read error") | ||
} | ||
// Our index page will always be html | ||
w.Header().Add("Content-Type", "text/html") | ||
w.Write(resp) | ||
} else { | ||
println("Real error") // Should probably use the logging system. | ||
} | ||
} else { | ||
// If the file does exist then it's a real embeded file and we'll write it's contents back. | ||
// Since `w.Write` isn't aware of the file type we need to manually add the MIME types for files we'll serve. | ||
// These MIME types only need to account for the files we'll be embedding through the `./build/` directory. | ||
// Content embedded from the application's build directory, includes the latest build of the UI. | ||
// | ||
//go:embed build/*.png build/*.html build/*.json build/*.txt build/*.ico | ||
//go:embed build/static/* | ||
var Content embed.FS | ||
|
||
// ServeHTTP provides the Tavern UI, if the requested file does not exist it will serve index.html | ||
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
|
||
// Serve the requested file | ||
path := fmt.Sprintf("build/%s", strings.TrimPrefix(r.URL.Path, "/")) | ||
content, err := Content.ReadFile(path) | ||
if err == nil { | ||
if strings.HasSuffix(path, ".css") { | ||
w.Header().Add("Content-Type", "text/css") | ||
} else if strings.HasSuffix(path, ".js") { | ||
w.Header().Add("Content-Type", "text/javascript") | ||
} else if strings.HasSuffix(path, ".html") { | ||
w.Header().Add("Content-Type", "text/html") | ||
} | ||
w.Write(resp) | ||
w.Write(content) | ||
return | ||
} | ||
|
||
// Otherwise serve index.html | ||
index, err := Content.ReadFile("build/index.html") | ||
if err != nil { | ||
h.logger.Printf("fatal error: failed to read index.html: %v", err) | ||
w.WriteHeader(http.StatusInternalServerError) | ||
return | ||
} | ||
w.Write(index) | ||
} | ||
|
||
// NewHandler creates and returns a handler for the Tavern UI. | ||
func NewHandler(logger *log.Logger) *Handler { | ||
return &Handler{logger} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
name: example | ||
description: An example tome! | ||
paramdefs: | ||
- name: msg | ||
label: Message | ||
- label: Message | ||
name: msg | ||
placeholder: Something to print | ||
type: string | ||
placeholder: Something to print |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters