-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
167 additions
and
17 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
-- main.go -- | ||
package server | ||
|
||
import ( | ||
"context" | ||
"embed" | ||
"html/template" | ||
"log" | ||
"net/http" | ||
) | ||
|
||
//go:embed *.gohtml | ||
var templateFiles embed.FS | ||
|
||
var templates = template.Must(template.ParseFS(templateFiles, "*")) | ||
|
||
//go:generate muxt generate --receiver-static-type=Server | ||
|
||
func main() { | ||
srv := Server{} | ||
mux := http.NewServeMux() | ||
routes(mux, &srv) | ||
log.Fatal(http.ListenAndServe(":8080", mux)) | ||
} | ||
|
||
type Server struct{} | ||
|
||
type IndexData struct{} | ||
|
||
func (srv *Server) Index(ctx context.Context) IndexData { | ||
return IndexData{} | ||
} | ||
-- index.gohtml -- | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
{{block "head" "Welcome!"}} | ||
<head> | ||
<meta charset='UTF-8'/> | ||
<title>{{.}}</title> | ||
<script src='https://unpkg.com/htmx.org@2.0.3/dist/htmx.min.js' | ||
crossorigin='anonymous' | ||
integrity='sha384-0895/pl2MU10Hqc6jd4RvrthNlDiE9U1tWmX7WRESftEDRosgxNsQG/Ze9YMRzHq'></script> | ||
|
||
<link rel='stylesheet' | ||
href='https://unpkg.com/browse/@picocss/pico@2.0.6/css/pico.min.css' | ||
integrity='sha384-7P0NVe9LPDbUCAF+fH2R8Egwz1uqNH83Ns/bfJY0fN2XCDBMUI2S9gGzIOIRBKsA'> | ||
</head> | ||
{{end}} | ||
<body> | ||
<main class='container'> | ||
|
||
<h1>Hello, friend!</h1> | ||
|
||
{{- define "GET /{$} Index(ctx)" -}} | ||
{{template "index.gohtml" .}} | ||
{{- end -}} | ||
|
||
</main> | ||
{{block "footer" 0}} | ||
<footer> | ||
<ul> | ||
<li> | ||
<a href='/'>Welcome</a> | ||
</li> | ||
<li> | ||
<a href='/about'>About</a> | ||
</li> | ||
</ul> | ||
</footer> | ||
{{end}} | ||
</body> | ||
</html> | ||
|
||
{{define "About"}} | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
{{template "head" "About"}} | ||
<body> | ||
<main class='container'> | ||
|
||
<h1>About Page</h1> | ||
|
||
</main> | ||
</body> | ||
</html> | ||
{{end}}{{- define "GET /about" -}} | ||
{{template "About" .}} | ||
{{- end -}} | ||
|
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
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
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,11 +1,60 @@ | ||
package main | ||
|
||
import ( | ||
"embed" | ||
"flag" | ||
"fmt" | ||
"io" | ||
"io/fs" | ||
"os" | ||
"path" | ||
"slices" | ||
"strings" | ||
|
||
"golang.org/x/tools/txtar" | ||
) | ||
|
||
func newCommand(_ []string, _ string, _ func(string) string, stdout io.Writer) error { | ||
_, err := fmt.Fprintln(stdout, "Coming soon...") | ||
//go:embed data/new/*.txtar | ||
var projectTemplates embed.FS | ||
|
||
func newCommand(args []string, workingDirectory string, _ func(string) string, stdout, stderr io.Writer) error { | ||
templateFilePaths, err := fs.Glob(projectTemplates, "data/new/*.txtar") | ||
if err != nil { | ||
return fmt.Errorf("failed to load new project templates") | ||
} | ||
var newProjectTemplateNames []string | ||
for _, filePath := range templateFilePaths { | ||
name := strings.TrimSuffix(path.Base(filePath), path.Ext(filePath)) | ||
newProjectTemplateNames = append(newProjectTemplateNames, name) | ||
} | ||
var ( | ||
templateName string | ||
) | ||
flagSet := flag.NewFlagSet("new", flag.ContinueOnError) | ||
flagSet.SetOutput(stderr) | ||
flagSet.StringVar(&templateName, "template", "default", fmt.Sprintf("new project template name one of: %s", strings.Join(newProjectTemplateNames, ", "))) | ||
if err := flagSet.Parse(args); err != nil { | ||
return fmt.Errorf("failed to parse arguments for new command: %w", err) | ||
} | ||
i := slices.Index(newProjectTemplateNames, templateName) | ||
if i < 0 { | ||
return fmt.Errorf("unknown new project tamplate name: %q", templateName) | ||
} | ||
selectedTemplateName := templateFilePaths[i] | ||
|
||
buf, err := fs.ReadFile(projectTemplates, selectedTemplateName) | ||
if err != nil { | ||
return fmt.Errorf("failed to read new project template: %w", err) | ||
} | ||
|
||
archive := txtar.Parse(buf) | ||
dir, err := txtar.FS(archive) | ||
if err != nil { | ||
return fmt.Errorf("failed to use new project template as directory: %w", err) | ||
} | ||
if err := os.CopyFS(workingDirectory, dir); err != nil { | ||
return fmt.Errorf("failed to copy new project template files to output directory %q: %w", workingDirectory, err) | ||
} | ||
_, err = fmt.Fprintf(stdout, "new project generated\nnow run:\n\n\tgo generate") | ||
return 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,12 @@ | ||
exec go mod init example.com/my-project | ||
|
||
muxt new | ||
|
||
stdout 'new project generated' | ||
stdout 'now run:' | ||
stdout 'go generate' | ||
|
||
exec go generate -x | ||
stderr 'muxt generate --receiver-static-type=Server' | ||
|
||
muxt generate --receiver-static-type=Server |
This file was deleted.
Oops, something went wrong.