From 5867c7aa614025e1516bbcea57e6763015182d52 Mon Sep 17 00:00:00 2001 From: Xe Iaso Date: Wed, 26 Jul 2023 23:35:26 -0400 Subject: cmd/within.website: update to use TySON for config, make things better overall Signed-off-by: Xe Iaso --- cmd/within.website/config.ts | 37 ++++++++ cmd/within.website/main.go | 172 +++++++++++++--------------------- cmd/within.website/static/gruvbox.css | 64 ------------- cmd/within.website/tmpl/404.tmpl | 5 + cmd/within.website/tmpl/base.tmpl | 21 +++++ cmd/within.website/tmpl/botinfo.tmpl | 7 ++ cmd/within.website/tmpl/index.tmpl | 23 +++++ 7 files changed, 157 insertions(+), 172 deletions(-) create mode 100644 cmd/within.website/config.ts delete mode 100644 cmd/within.website/static/gruvbox.css create mode 100644 cmd/within.website/tmpl/404.tmpl create mode 100644 cmd/within.website/tmpl/base.tmpl create mode 100644 cmd/within.website/tmpl/botinfo.tmpl create mode 100644 cmd/within.website/tmpl/index.tmpl (limited to 'cmd/within.website') diff --git a/cmd/within.website/config.ts b/cmd/within.website/config.ts new file mode 100644 index 0000000..e79434d --- /dev/null +++ b/cmd/within.website/config.ts @@ -0,0 +1,37 @@ +export interface Repo { + kind: "gitea" | "github"; + domain: string; + user: string; + repo: string; + description: string; +} + +const githubRepo = (name: string, description: string): Repo => { + return { + kind: "github", + domain: "github.com", + user: "Xe", + repo: name, + description, + }; +}; + +const giteaRepo = (name: string, description: string): Repo => { + return { + kind: "gitea", + domain: "tulpa.dev", + user: "cadey", + repo: name, + description, + }; +}; + +const repos: Repo[] = [ + githubRepo("derpigo", "A Derpibooru/Furbooru API client in Go. This is used to monitor Derpibooru/Furbooru for images by artists I care about and archive them."), + githubRepo("eclier", "A command router for Go programs that implements every command in Lua. This was an experiment for making extensible command-line applications with Lua for extending them."), + giteaRepo("gopher", "A Gopher (RFC 1436) client/server stack for Go applications. This allows users to write custom Gopher clients and servers."), + githubRepo("ln", "The natural log function for Go: an easy package for structured logging. This is the logging stack that I use for most of my personal projects."), + githubRepo("x", "Various experimental things. /x/ is my monorepo of side projects, hobby programming, and other explorations of how programming in Go can be."), +]; + +export default repos; diff --git a/cmd/within.website/main.go b/cmd/within.website/main.go index cea40d6..b87653d 100644 --- a/cmd/within.website/main.go +++ b/cmd/within.website/main.go @@ -2,147 +2,103 @@ package main import ( - "context" "embed" "flag" + "html/template" "net/http" + "os" - "github.com/mmikulicic/stringlist" - "within.website/ln" - "within.website/ln/ex" - "within.website/ln/opname" + "go.jetpack.io/tyson" + "golang.org/x/exp/slog" "within.website/x/internal" "within.website/x/web/vanity" ) -//go:generate go-bindata -pkg main static - var ( - domain = flag.String("domain", "within.website", "domain this is run on") - githubUsername = flag.String("github-user", "Xe", "GitHub username for GitHub repos") - gogsDomain = flag.String("gogs-url", "tulpa.dev", "Gogs domain to use") - gogsUsername = flag.String("gogs-username", "cadey", "Gogs username for above Gogs instance") - port = flag.String("port", "2134", "HTTP port to listen on") - goProxyServer = flag.String("go-proxy-server", "https://cache.greedo.xeserv.us", "go proxy server to point to for go module clients") - - githubRepos = stringlist.Flag("github-repo", "list of GitHub repositories to use") - gogsRepos = stringlist.Flag("gogs-repo", "list of Gogs repositories to use") -) + domain = flag.String("domain", "within.website", "domain this is run on") + port = flag.String("port", "2134", "HTTP port to listen on") + tysonConfig = flag.String("tyson-config", "./config.ts", "TySON config file") -var ( - //go:embed static - staticFS embed.FS + //go:embed tmpl/* + templateFiles embed.FS ) -var githubReposDefault = []string{ - "ln", - "x", - "eclier", - "gluanetrc", - "xultybau", - "johaus", - "confyg", - "derpigo", - "olin", +type Repo struct { + Kind string `json:"kind"` + Domain string `json:"domain"` + User string `json:"string"` + Repo string `json:"repo"` + Description string `json:"description"` } -var gogsReposDefault = []string{ - "gorqlite", - "gopher", - "mi", +func (r Repo) LogValue() slog.Value { + return slog.GroupValue( + slog.String("kind", r.Kind), + slog.String("domain", r.Domain), + slog.String("user", r.User), + slog.String("repo", r.Repo), + ) +} + +func (r Repo) RegisterHandlers(lg *slog.Logger) { + switch r.Kind { + case "gitea": + http.Handle("/"+r.Repo, vanity.GogsHandler(*domain+"/"+r.Repo, r.Domain, r.User, r.Repo, "https")) + http.Handle("/"+r.Repo+"/", vanity.GogsHandler(*domain+"/"+r.Repo, r.Domain, r.User, r.Repo, "https")) + case "github": + http.Handle("/"+r.Repo, vanity.GitHubHandler(*domain+"/"+r.Repo, r.User, r.Repo, "https")) + http.Handle("/"+r.Repo+"/", vanity.GitHubHandler(*domain+"/"+r.Repo, r.User, r.Repo, "https")) + } + lg.Debug("registered repo handler", "repo", r) } func main() { internal.HandleStartup() - ctx := opname.With(context.Background(), "main") - ctx = ln.WithF(ctx, ln.F{ - "domain": *domain, - "proxy_server": *goProxyServer, - }) - - if len(*githubRepos) == 0 { - *githubRepos = githubReposDefault - } - if len(*gogsRepos) == 0 { - *gogsRepos = gogsReposDefault - } + lg := slog.Default().With("domain", *domain, "configPath", *tysonConfig) - for _, repo := range *githubRepos { - http.Handle("/"+repo, vanity.GitHubHandler(*domain+"/"+repo, *githubUsername, repo, "https")) - http.Handle("/"+repo+"/", vanity.GitHubHandler(*domain+"/"+repo, *githubUsername, repo, "https")) + tmpls := template.Must(template.ParseFS(templateFiles, "tmpl/*.tmpl")) - ln.Log(ctx, ln.F{"github_repo": repo, "github_user": *githubUsername}, ln.Info("adding github repo")) + var repos []Repo + if err := tyson.Unmarshal(*tysonConfig, &repos); err != nil { + lg.Error("can't unmarshal config", "err", err) + os.Exit(1) } - for _, repo := range *gogsRepos { - http.Handle("/"+repo, vanity.GogsHandler(*domain+"/"+repo, *gogsDomain, *gogsUsername, repo, "https")) - http.Handle("/"+repo+"/", vanity.GogsHandler(*domain+"/"+repo, *gogsDomain, *gogsUsername, repo, "https")) - - ln.Log(ctx, ln.F{"gogs_domain": *gogsDomain, "gogs_username": *gogsUsername, "gogs_repo": repo}, ln.Info("adding gogs repo")) + for _, repo := range repos { + repo.RegisterHandlers(lg) } - http.Handle("/static/", http.FileServer(http.FS(staticFS))) - http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + w.Header().Add("Content-Type", "text/html") if r.URL.Path != "/" { - http.NotFound(w, r) + w.WriteHeader(http.StatusNotFound) + tmpls.ExecuteTemplate(w, "404.tmpl", struct { + Title string + }{ + Title: "Not found: " + r.URL.Path, + }) + return } - - w.Header().Add("Content-Type", "text/html") - w.Write([]byte(indexTemplate)) + tmpls.ExecuteTemplate(w, "index.tmpl", struct { + Title string + Repos []Repo + }{ + Title: "within.website Go packages", + Repos: repos, + }) }) http.HandleFunc("/.x.botinfo", func(w http.ResponseWriter, r *http.Request) { w.Header().Add("Content-Type", "text/html") - w.Write([]byte(botInfoPage)) + tmpls.ExecuteTemplate(w, "botinfo.tmpl", struct { + Title string + }{ + Title: "x repo bots", + }) }) - ln.Log(ctx, ln.F{"port": *port}, ln.Info("Listening on HTTP")) - http.ListenAndServe(":"+*port, ex.HTTPLog(http.DefaultServeMux)) - + lg.Info("listening", "port", *port) + http.ListenAndServe(":"+*port, http.DefaultServeMux) } - -const indexTemplate = ` - - - within.website Go Packages - - - - -
-

within.website Go Packages

- -
    -
  • confyg - A generic configuration file parser based on the go modfile parser
  • -
  • derpigo - A simple wrapper to the Derpibooru API
  • -
  • eclier - A go+lua command line application framework
  • -
  • gopher - A simple Gopher client/server framework based on net/http
  • -
  • gluanetrc - A GopherLua binding for netrc file management
  • -
  • gorqlite - A driver for rqlite
  • -
  • johaus - Lojban parsing
  • -
  • ln - Key->value based logging made context-aware and simple
  • -
  • olin - WebAssembly on the server
  • -
  • x - Experiments, toys and tinkering (many subpackages)
  • -
- -
- -
-

Need help with these packages? Inquire Within.

-
-
- -` - -const botInfoPage = ` -x repo bots -
-

x repo bots

- -

Hello, if you are reading this, you have found this URL in your access logs. - -If one of these programs is doing something you don't want them to do, please contact me or open an issue here.

-
` diff --git a/cmd/within.website/static/gruvbox.css b/cmd/within.website/static/gruvbox.css deleted file mode 100644 index f391bf5..0000000 --- a/cmd/within.website/static/gruvbox.css +++ /dev/null @@ -1,64 +0,0 @@ -main { - font-family: monospace, monospace; - max-width: 38rem; - padding: 2rem; - margin: auto; -} - -::selection { - background: #d3869b; -} - -body { - background: #282828; - color: #ebdbb2; -} - -pre { - background-color: #3c3836; - padding: 1em; - border: 0; -} - -a, a:active, a:visited { - color: #b16286; - background-color: #1d2021; -} - -h1, h2, h3, h4, h5 { - margin-bottom: .1rem; -} - -blockquote { - border-left: 1px solid #bdae93; - margin: 0.5em 10px; - padding: 0.5em 10px; -} - -@media (prefers-color-scheme: light) { - body { - background: #fbf1c7; - color: #3c3836; - } - - pre { - background-color: #ebdbb2; - padding: 1em; - border: 0; - } - - a, a:active, a:visited { - color: #b16286; - background-color: #f9f5d7; - } - - h1, h2, h3, h4, h5 { - margin-bottom: .1rem; - } - - blockquote { - border-left: 1px solid #655c54; - margin: 0.5em 10px; - padding: 0.5em 10px; - } -} diff --git a/cmd/within.website/tmpl/404.tmpl b/cmd/within.website/tmpl/404.tmpl new file mode 100644 index 0000000..c2517bc --- /dev/null +++ b/cmd/within.website/tmpl/404.tmpl @@ -0,0 +1,5 @@ +{{template "header" .}} + +

The URL you requested could not be found. Please check your URL and hang up to try your call again.

+ +{{template "footer" .}} diff --git a/cmd/within.website/tmpl/base.tmpl b/cmd/within.website/tmpl/base.tmpl new file mode 100644 index 0000000..78e188d --- /dev/null +++ b/cmd/within.website/tmpl/base.tmpl @@ -0,0 +1,21 @@ +{{define "header"}} + + + + {{.Title}} + + + + +
+

{{.Title}}

+{{end}} + +{{define "footer"}} +
+

Need help with these packages? Inquire Within.

+
+
+ + +{{end}} diff --git a/cmd/within.website/tmpl/botinfo.tmpl b/cmd/within.website/tmpl/botinfo.tmpl new file mode 100644 index 0000000..5b80e4d --- /dev/null +++ b/cmd/within.website/tmpl/botinfo.tmpl @@ -0,0 +1,7 @@ +{{template "header" .}} + +

Hello, if you are reading this, you have found this URL in your access logs.

+ +

If one of these programs is doing something you don't want them to do, please contact me or open an issue here.

+ +{{template "footer" .}} diff --git a/cmd/within.website/tmpl/index.tmpl b/cmd/within.website/tmpl/index.tmpl new file mode 100644 index 0000000..d9ad01e --- /dev/null +++ b/cmd/within.website/tmpl/index.tmpl @@ -0,0 +1,23 @@ +{{template "header" .}} + +This vanity domain houses a lot of Xe Iaso's custom Go packages. Here is a list of all of the packages currently tracked: + + + + + +{{range .Repos}} +

within.website/{{.Repo}}

+ +

Go Reference Source code link

+ +

{{.Description}}

+ +
go get within.website/{{.Repo}}
+{{end}} + +{{template "footer" .}} -- cgit v1.2.3