diff options
| author | Xe Iaso <me@xeiaso.net> | 2023-07-26 23:35:26 -0400 |
|---|---|---|
| committer | Xe Iaso <me@xeiaso.net> | 2023-07-26 23:35:26 -0400 |
| commit | 5867c7aa614025e1516bbcea57e6763015182d52 (patch) | |
| tree | c28100a0aec5ff181445116adc7cf3c5d2cd2eaa /cmd | |
| parent | 319d0244c9418a02c4abeaeec79054320807fd7f (diff) | |
| download | x-5867c7aa614025e1516bbcea57e6763015182d52.tar.xz x-5867c7aa614025e1516bbcea57e6763015182d52.zip | |
cmd/within.website: update to use TySON for config, make things better overall
Signed-off-by: Xe Iaso <me@xeiaso.net>
Diffstat (limited to 'cmd')
| -rw-r--r-- | cmd/within.website/config.ts | 37 | ||||
| -rw-r--r-- | cmd/within.website/main.go | 172 | ||||
| -rw-r--r-- | cmd/within.website/static/gruvbox.css | 64 | ||||
| -rw-r--r-- | cmd/within.website/tmpl/404.tmpl | 5 | ||||
| -rw-r--r-- | cmd/within.website/tmpl/base.tmpl | 21 | ||||
| -rw-r--r-- | cmd/within.website/tmpl/botinfo.tmpl | 7 | ||||
| -rw-r--r-- | cmd/within.website/tmpl/index.tmpl | 23 |
7 files changed, 157 insertions, 172 deletions
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 = `<!DOCTYPE html> -<html> - <head> - <title>within.website Go Packages</title> - <link rel="stylesheet" href="/static/gruvbox.css"> - <meta name="viewport" content="width=device-width, initial-scale=1.0" /> - </head> - <body id="top"> - <main> - <h1><code>within.website</code> Go Packages</h1> - - <ul> - <li><a href="https://within.website/confyg">confyg</a> - A generic configuration file parser based on the go modfile parser</li> - <li><a href="https://within.website/derpigo">derpigo</a> - A simple wrapper to the <a href="https://derpibooru.org">Derpibooru</a> API</li> - <li><a href="https://within.website/eclier">eclier</a> - A go+lua command line application framework</li> - <li><a href="https://within.website/gopher">gopher</a> - A simple Gopher client/server framework based on net/http</li> - <li><a href="https://within.website/gluanetrc">gluanetrc</a> - A GopherLua binding for <a href="https://github.com/dickeyxxx/netrc">netrc</a> file management</li> - <li><a href="https://within.website/gorqlite">gorqlite</a> - A driver for <a href="https://github.com/rqlite/rqlite">rqlite</a></li> - <li><a href="https://within.website/johaus">johaus</a> - <a href="http://lojban.org">Lojban</a> parsing</li> - <li><a href="https://within.website/ln">ln</a> - Key->value based logging made context-aware and simple</li> - <li><a href="https://within.website/olin">olin</a> - WebAssembly on the server</li> - <li><a href="https://within.website/x">x</a> - Experiments, toys and tinkering (many subpackages)</li> - </ul> - - <hr /> - - <footer class="is-text-center"> - <p>Need help with these packages? Inquire <a href="https://github.com/Xe">Within</a>.</p> - </footer> - </main> - </body> -</html>` - -const botInfoPage = `<link rel="stylesheet" href="/static/gruvbox.css"> -<title>x repo bots</title> -<main> -<h1>x repo bots</h1> - -<p>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 <a href="https://christine.website/contact">contact me</a> or open an issue <a href="https://github.com/Xe/x">here</a>.</p> -</main>` 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" .}} + +<p>The URL you requested could not be found. Please check your URL and hang up to try your call again.</p> + +{{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"}} +<!DOCTYPE html> +<html> + <head> + <title>{{.Title}}</title> + <link rel="stylesheet" href="https://cdn.xeiaso.net/static/pkg/xess/xess.css" /> + <meta name="viewport" content="width=device-width, initial-scale=1.0" /> + </head> + <body id="top"> + <main> + <h1>{{.Title}}</h1> +{{end}} + +{{define "footer"}} + <footer> + <p>Need help with these packages? Inquire <a href="https://github.com/Xe">Within</a>.</p> + </footer> + </main> + </body> +</html> +{{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" .}} + +<p>Hello, if you are reading this, you have found this URL in your access logs.</p> + +<p>If one of these programs is doing something you don't want them to do, please <a href="https://xeiaso.net/contact">contact me</a> or open an issue <a href="https://github.com/Xe/x">here</a>.</p> + +{{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 <a href="https://xeiaso.net">Xe Iaso's</a> custom Go packages. Here is a list of all of the packages currently tracked: + +<ul> +{{range .Repos}} + <li><a href="#{{.Repo}}">{{.Repo}}</a></li> +{{end}} +</ul> + +<img src="https://pride-badges.pony.workers.dev/static/v1?label=enbyware&labelColor=%23555&stripeWidth=8&stripeColors=FCF434%2CFFFFFF%2C9C59D1%2C2C2C2C" /> + +{{range .Repos}} +<h2>within.website/{{.Repo}}</h2> + +<p><a href="https://pkg.go.dev/within.website/{{.Repo}}"><img src="https://pkg.go.dev/badge/within.website/{{.Repo}}.svg" alt="Go Reference"></a> <a href="https://{{.Domain}}/{{.User}}/{{.Repo}}"><img alt="Source code link" src="https://img.shields.io/badge/source-link-green"></a></p> + +<p>{{.Description}}</p> + +<code><pre>go get within.website/{{.Repo}}</pre></code> +{{end}} + +{{template "footer" .}} |
