Initial commit

Signed-off-by: jolheiser <john.olheiser@gmail.com>
pull/1/head v0.1.0
jolheiser 2020-02-20 22:39:03 -06:00
commit db641970e7
Signed by: jolheiser
GPG Key ID: B853ADA5DA7BBF7A
20 changed files with 691 additions and 0 deletions

6
.gitignore vendored 100644
View File

@ -0,0 +1,6 @@
# GoLand
.idea
# Binaries
/vanity*
!vanity.service

86
Makefile 100644
View File

@ -0,0 +1,86 @@
DIST := dist
GO ?= go
SHASUM ?= shasum -a 256
ifneq ($(DRONE_TAG),)
VERSION ?= $(subst v,,$(DRONE_TAG))
LONG_VERSION ?= $(VERSION)
else
ifneq ($(DRONE_BRANCH),)
VERSION ?= $(subst release/v,,$(DRONE_BRANCH))
else
VERSION ?= master
endif
LONG_VERSION ?= $(shell git describe --tags --always | sed 's/-/+/' | sed 's/^v//')
endif
LDFLAGS := $(LDFLAGS) -X "go.jolheiser.com/vanity/modules/config.Version=$(LONG_VERSION)"
.PHONY: build
build:
$(GO) build -ldflags '-s -w $(LDFLAGS)'
.PHONY: lint
lint:
@hash golangci-lint > /dev/null 2>&1; if [ $$? -ne 0 ]; then \
export BINARY="golangci-lint"; \
curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b $(go env GOPATH)/bin v1.23.1; \
fi
golangci-lint run --timeout 5m
.PHONY: fmt
fmt:
$(GO) fmt ./...
.PHONY: test
test:
$(GO) test -race ./...
.PHONY: release
release: release-dirs check-xgo release-windows release-linux release-darwin release-copy release-compress release-check
.PHONY: check-xgo
check-xgo:
@hash xgo > /dev/null 2>&1; if [ $$? -ne 0 ]; then \
$(GO) get -u src.techknowlogick.com/xgo; \
fi
.PHONY: release-dirs
release-dirs:
mkdir -p $(DIST)/binaries $(DIST)/release
.PHONY: release-windows
release-windows:
xgo -dest $(DIST)/binaries -ldflags '-linkmode external -extldflags "-static" $(LDFLAGS)' -targets 'windows/*' -out vanity-$(VERSION) .
ifeq ($(CI),drone)
cp /build/* $(DIST)/binaries
endif
.PHONY: release-linux
release-linux:
xgo -dest $(DIST)/binaries -ldflags '-linkmode external -extldflags "-static" $(LDFLAGS)' -targets 'linux/amd64,linux/386,linux/arm-5,linux/arm-6,linux/arm64,linux/mips64le,linux/mips,linux/mipsle' -out vanity-$(VERSION) .
ifeq ($(CI),drone)
cp /build/* $(DIST)/binaries
endif
.PHONY: release-darwin
release-darwin:
xgo -dest $(DIST)/binaries -tags 'netgo osusergo $(TAGS)' -ldflags '$(LDFLAGS)' -targets 'darwin/*' -out vanity-$(VERSION) .
ifeq ($(CI),drone)
cp /build/* $(DIST)/binaries
endif
.PHONY: release-copy
release-copy:
cd $(DIST); for file in `find /build -type f -name "*"`; do cp $${file} ./release/; done;
.PHONY: release-check
release-check:
cd $(DIST)/release/; for file in `find . -type f -name "*"`; do echo "checksumming $${file}" && $(SHASUM) `echo $${file} | sed 's/^..//'` > $${file}.sha256; done;
.PHONY: release-compress
release-compress:
@hash gxz > /dev/null 2>&1; if [ $$? -ne 0 ]; then \
$(GO) get -u github.com/ulikunitz/xz/cmd/gxz; \
fi
cd $(DIST)/release/; for file in `find . -type f -name "*"`; do echo "compressing $${file}" && gxz -k -9 $${file}; done;

7
README.md 100644
View File

@ -0,0 +1,7 @@
# Vanity
A simple web service to serve vanity Go imports. Feel free to check it out using [my instance](https://go.jolheiser.com/).
## Configuration
See [the sample](config.sample.toml).
Vanity also supports [git-import](https://gitea.com/jolheiser/git-import).

79
cmd/add.go 100644
View File

@ -0,0 +1,79 @@
package cmd
import (
"github.com/AlecAivazis/survey/v2"
"github.com/urfave/cli/v2"
"go.jolheiser.com/beaver"
"go.jolheiser.com/vanity/modules/config"
)
var Add = cli.Command{
Name: "add",
Usage: "Add a package",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "force",
Aliases: []string{"f"},
Usage: "Overwrite existing package without prompt",
},
},
Action: doAdd,
}
func doAdd(ctx *cli.Context) error {
questions := []*survey.Question{
{
Name: "name",
Prompt: &survey.Input{Message: "Name"},
Validate: survey.Required,
},
{
Name: "path",
Prompt: &survey.Input{Message: "Path"},
Validate: survey.Required,
},
{
Name: "repo",
Prompt: &survey.Input{Message: "Repository HTTP(S) URL"},
Validate: survey.Required,
},
{
Name: "ssh",
Prompt: &survey.Input{Message: "Repository SSH URL"},
Validate: survey.Required,
},
{
Name: "description",
Prompt: &survey.Input{Message: "Description"},
Validate: survey.Required,
},
}
answers := struct {
Name string
Path string
Repo string
SSH string
Description string
}{}
if err := survey.Ask(questions, &answers); err != nil {
return err
}
pkg := config.Package{
Name: answers.Name,
Path: answers.Path,
Repo: answers.Repo,
SSH: answers.SSH,
Description: answers.Description,
}
config.AddPackages(ctx.Bool("force"), pkg)
if err := config.Save(); err != nil {
return err
}
beaver.Infof("Added `%s` to vanity.", pkg.Name)
return nil
}

35
cmd/config.go 100644
View File

@ -0,0 +1,35 @@
package cmd
import (
"github.com/AlecAivazis/survey/v2"
"github.com/urfave/cli/v2"
"go.jolheiser.com/beaver"
"go.jolheiser.com/vanity/modules/config"
)
var Config = cli.Command{
Name: "config",
Aliases: []string{"cfg"},
Usage: "Configure vanity",
Action: doConfig,
}
func doConfig(ctx *cli.Context) error {
urlQuestion := &survey.Input{
Message: "domain",
Default: "go.domain.tld",
}
var urlAnswer string
if err := survey.AskOne(urlQuestion, &urlAnswer); err != nil {
return err
}
config.Domain = urlAnswer
if err := config.Save(); err != nil {
return err
}
beaver.Info("domain saved!")
return nil
}

21
cmd/list.go 100644
View File

@ -0,0 +1,21 @@
package cmd
import (
"github.com/urfave/cli/v2"
"go.jolheiser.com/beaver"
"go.jolheiser.com/vanity/modules/config"
)
var List = cli.Command{
Name: "list",
Aliases: []string{"l"},
Usage: "List packages",
Action: doList,
}
func doList(ctx *cli.Context) error {
for _, pkg := range config.Packages {
beaver.Infof("%s (%s) -> %s | %s", pkg.Name, pkg.Path, pkg.Repo, pkg.SSH)
}
return nil
}

56
cmd/remove.go 100644
View File

@ -0,0 +1,56 @@
package cmd
import (
"fmt"
"github.com/AlecAivazis/survey/v2"
"github.com/urfave/cli/v2"
"go.jolheiser.com/beaver"
"go.jolheiser.com/vanity/modules/config"
"strings"
)
var Remove = cli.Command{
Name: "remove",
Aliases: []string{"rm"},
Usage: "Remove a package",
Action: doRemove,
}
func doRemove(ctx *cli.Context) error {
pkgQuestion := &survey.Input{
Message: "Package name",
}
var pkgAnswer string
if err := survey.AskOne(pkgQuestion, &pkgAnswer); err != nil {
return err
}
for idx, p := range config.Packages {
if strings.EqualFold(p.Name, pkgAnswer) {
confirm := &survey.Confirm{
Message: fmt.Sprintf("Are you sure you want to remove %s (%s) ?", p.Name, p.Repo),
Default: false,
}
var answer bool
if err := survey.AskOne(confirm, &answer); err != nil {
return err
}
if answer {
config.Packages = append(config.Packages[:idx], config.Packages[idx+1:]...)
if err := config.Save(); err != nil {
return err
}
beaver.Infof("Removed `%s` from vanity.", p.Name)
break
}
beaver.Infof("Did not remove `%s` from vanity.", p.Name)
break
}
}
return nil
}

31
cmd/server.go 100644
View File

@ -0,0 +1,31 @@
package cmd
import (
"fmt"
"github.com/urfave/cli/v2"
"go.jolheiser.com/beaver"
"go.jolheiser.com/vanity/modules/router"
"net/http"
)
var Server = cli.Command{
Name: "server",
Usage: "Start the vanity server",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "port",
Aliases: []string{"p"},
Usage: "Port to run the vanity server on",
Value: "3333",
},
},
Action: doServer,
}
func doServer(ctx *cli.Context) error {
beaver.Infof("Running vanity server at http://localhost:%s", ctx.String("port"))
if err := http.ListenAndServe(fmt.Sprintf(":%s", ctx.String("port")), router.Init()); err != nil {
return err
}
return nil
}

12
config.sample.toml 100644
View File

@ -0,0 +1,12 @@
# For each package, an entry like the following
[[package]]
# The name of the package (can be anything)
name = "Go-Vanity"
# The path to the package (this NEEDS to match the import path)
path = "vanity"
# The repository to direct go-import to
repo = "https://gitea.com/jolheiser/vanity.git"
# git-import for SSH (optional)
ssh = "git@gitea.com:jolheiser/vanity.git"
# A description of the project (optional)
description = "The code responsible for hosting this service!"

13
go.mod 100644
View File

@ -0,0 +1,13 @@
module go.jolheiser.com/vanity
go 1.12
require (
github.com/AlecAivazis/survey/v2 v2.0.5
github.com/BurntSushi/toml v0.3.1
github.com/go-chi/chi v4.0.3+incompatible
github.com/mitchellh/go-homedir v1.1.0
github.com/stretchr/testify v1.3.0 // indirect
github.com/urfave/cli/v2 v2.1.1
go.jolheiser.com/beaver v1.0.1
)

59
go.sum 100644
View File

@ -0,0 +1,59 @@
github.com/AlecAivazis/survey/v2 v2.0.5 h1:xpZp+Q55wi5C7Iaze+40onHnEkex1jSc34CltJjOoPM=
github.com/AlecAivazis/survey/v2 v2.0.5/go.mod h1:WYBhg6f0y/fNYUuesWQc0PKbJcEliGcYHB9sNT3Bg74=
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/Netflix/go-expect v0.0.0-20180615182759-c93bf25de8e8 h1:xzYJEypr/85nBpB11F9br+3HUrpgb+fcm5iADzXXYEw=
github.com/Netflix/go-expect v0.0.0-20180615182759-c93bf25de8e8/go.mod h1:oX5x61PbNXchhh0oikYAH+4Pcfw5LKv21+Jnpr6r6Pc=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSYgptZMwQh2aRr3LuazLJIa+Pg3Kc1ylSYVY=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-chi/chi v4.0.3+incompatible h1:gakN3pDJnzZN5jqFV2TEdF66rTfKeITyR8qu6ekICEY=
github.com/go-chi/chi v4.0.3+incompatible/go.mod h1:eB3wogJHnLi3x/kFX2A+IbTBlXxmMeXJVKy9tTv1XzQ=
github.com/hinshun/vt10x v0.0.0-20180616224451-1954e6464174 h1:WlZsjVhE8Af9IcZDGgJGQpNflI3+MJSBhsgT5PCtzBQ=
github.com/hinshun/vt10x v0.0.0-20180616224451-1954e6464174/go.mod h1:DqJ97dSdRW1W22yXSB90986pcOyQ7r45iio1KN2ez1A=
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs=
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:CzGEWj7cYgsdH8dAjBGEr58BoE7ScuLd+fwFZ44+/x8=
github.com/kr/pty v1.1.4 h1:5Myjjh3JY/NaAi4IsUbHADytDyl1VE1Y9PXDlL+P/VQ=
github.com/kr/pty v1.1.4/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/mattn/go-colorable v0.1.2 h1:/bC9yWikZXAL9uJdulbSfyVNIR3n3trXl+v8+1sx8mU=
github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
github.com/mattn/go-isatty v0.0.8 h1:HLtExJ+uU2HOZ+wI0Tt5DtUDrx8yhUqDcp7fYERX4CE=
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b h1:j7+1HpAFS1zy5+Q4qx1fWh90gTKwiN4QCGoY9TWyyO4=
github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b/go.mod h1:01TrycV0kFyexm33Z7vhZRXopbI8J3TDReVlkTgMUxE=
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
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/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/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.1/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/urfave/cli/v2 v2.1.1 h1:Qt8FeAtxE/vfdrLmR3rxR6JRE0RoVmbXu8+6kZtYU4k=
github.com/urfave/cli/v2 v2.1.1/go.mod h1:SE9GqnLQmjVa0iPEY0f1w3ygNIYcIJ0OKPMoW2caLfQ=
go.jolheiser.com/beaver v1.0.1 h1:gt3aGEr5Bj4ZjDF1g8t8OYOGRCRXGaanGR9CmXUxez8=
go.jolheiser.com/beaver v1.0.1/go.mod h1:7X4F5+XOGSC3LejTShoBdqtRCnPWcnRgmYGmG3EKW8g=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2 h1:VklqNMn3ovrHsnt90PveolxSbWFaJdECFbxSq0Mqo2M=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190530122614-20be4c3c3ed5 h1:8dUaAV7K4uHsF56JQWkprecIQKdPHtR9jCHF5nB8uzc=
golang.org/x/crypto v0.0.0-20190530122614-20be4c3c3ed5/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3 h1:0GoQqolDA55aaLxZyTzK/Y2ePZzZTUrRacwib7cNsYQ=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a h1:1BGLXjeY4akVXGgbC9HugT3Jv3hCI0z56oJR5vAMgBU=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223 h1:DH4skfRX4EBpamg7iV4ZlCpblAHI6s6TDM39bFZumv8=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190530182044-ad28b68e88f1/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a h1:aYOabOQFp6Vj6W1F80affTUvO9UxmJRx8K0gsfABByQ=
golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a/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=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

31
main.go 100644
View File

@ -0,0 +1,31 @@
package main
import (
"github.com/urfave/cli/v2"
"go.jolheiser.com/beaver"
"go.jolheiser.com/vanity/cmd"
"go.jolheiser.com/vanity/modules/config"
"os"
)
func main() {
// config loads on init
app := cli.NewApp()
app.Name = "vanity"
app.Usage = "Vanity Go Imports"
app.Version = config.Version
app.Commands = []*cli.Command{
&cmd.Add,
&cmd.Remove,
&cmd.List,
&cmd.Config,
&cmd.Server,
}
app.EnableBashCompletion = true
err := app.Run(os.Args)
if err != nil {
beaver.Error(err)
}
}

View File

@ -0,0 +1,133 @@
package config
import (
"fmt"
"github.com/AlecAivazis/survey/v2"
"github.com/BurntSushi/toml"
"github.com/mitchellh/go-homedir"
"go.jolheiser.com/beaver"
"os"
"path"
"strings"
)
var (
configPath string
cfg *Config
Version = "develop"
// Config items
Domain string
Packages []Package
)
type Config struct {
Domain string `toml:"domain"`
Packages []Package `toml:"package" json:"packages"`
}
type Package struct {
Name string `toml:"name"`
Path string `toml:"path"`
Repo string `toml:"repo"`
SSH string `toml:"ssh"`
Description string `toml:"description"`
}
func (pkg Package) Module() string {
return fmt.Sprintf("%s/%s", Domain, pkg.Path)
}
// Load on init so that CLI contexts are correctly populated
func init() {
home, err := homedir.Dir()
if err != nil {
beaver.Fatalf("could not locate home directory: %v", err)
}
configPath = fmt.Sprintf("%s/.vanity/config.toml", home)
if _, err := os.Stat(configPath); os.IsNotExist(err) {
if err := os.MkdirAll(path.Dir(configPath), os.ModePerm); err != nil {
beaver.Fatalf("could not create vanity home: %v", err)
}
if _, err := os.Create(configPath); err != nil {
beaver.Fatalf("could not create vanity config: %v", err)
}
}
if _, err := toml.DecodeFile(configPath, &cfg); err != nil {
beaver.Fatalf("could not decode vanity config: %v", err)
}
dupe := make(map[string]bool)
for _, pkg := range cfg.Packages {
name := strings.ToLower(pkg.Name)
if ok := dupe[name]; ok {
beaver.Fatalf("duplicate package for %s", pkg.Name)
}
dupe[name] = true
}
Domain = cfg.Domain
Packages = cfg.Packages
}
func Save() error {
cfg.Domain = Domain
cfg.Packages = Packages
fi, err := os.Create(configPath)
if err != nil {
return err
}
defer fi.Close()
if err := toml.NewEncoder(fi).Encode(cfg); err != nil {
return err
}
return nil
}
func PackageMap() map[string]Package {
pkgs := make(map[string]Package)
for _, pkg := range Packages {
pkgs[pkg.Path] = pkg
}
return pkgs
}
func AddPackages(force bool, pkgs ...Package) {
for _, pkg := range pkgs {
for idx, p := range Packages {
if strings.EqualFold(p.Name, pkg.Name) {
if force {
Packages[idx] = pkg
break
}
forceQuestion := &survey.Confirm{
Message: fmt.Sprintf("Package `%s` (%s) already exists. Overwrite with `%s`?", p.Name, p.Repo, p.Repo),
Default: false,
}
var forceAnswer bool
if err := survey.AskOne(forceQuestion, &forceAnswer); err != nil {
beaver.Error(err)
break
}
if !forceAnswer {
beaver.Errorf("leaving package `%s` as-is", pkg.Name)
break
}
Packages[idx] = pkg
break
}
}
Packages = append(Packages, pkg)
}
}

View File

@ -0,0 +1,59 @@
package router
import (
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"go.jolheiser.com/beaver"
"go.jolheiser.com/vanity/modules/config"
"go.jolheiser.com/vanity/modules/router/templates"
"html/template"
"net/http"
"runtime"
"strings"
"time"
)
var (
index = template.Must(template.New("index").Parse(templates.Head + templates.Index + templates.Info + templates.Foot))
vanity = template.Must(template.New("vanity").Parse(templates.Head + templates.Vanity + templates.Info + templates.Foot))
cache = config.PackageMap()
)
func Init() *chi.Mux {
r := chi.NewRouter()
r.Use(middleware.RedirectSlashes)
r.Use(middleware.Recoverer)
r.Use(middleware.Timeout(30 * time.Second))
r.Get("/", doGet)
r.Get("/*", doPackage)
return r
}
func doGet(res http.ResponseWriter, req *http.Request) {
if err := index.Execute(res, map[string]interface{}{
"Packages": config.Packages,
"AppVer": config.Version,
"GoVer": runtime.Version(),
}); err != nil {
beaver.Error(err)
}
}
func doPackage(res http.ResponseWriter, req *http.Request) {
key := chi.URLParam(req, "*")
pkg, ok := cache[strings.Split(key, "/")[0]]
if !ok {
http.NotFound(res, req)
return
}
if err := vanity.Execute(res, map[string]interface{}{
"Package": pkg,
"AppVer": config.Version,
"GoVer": runtime.Version(),
}); err != nil {
beaver.Error(err)
}
}

View File

@ -0,0 +1,6 @@
package templates
var Foot = `
</body>
</html>
`

View File

@ -0,0 +1,15 @@
package templates
var Head = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
{{if .Package}}
<meta name="go-import" content="{{.Package.Module}} git {{.Package.Repo}}"/>
<meta name="git-import" content="{{.Package.Path}} {{.Package.Repo}} {{.Package.SSH}}"/>
{{end}}
<title>Vanity - {{if .Package}}{{.Package.Name}}{{else}}Index{{end}}</title>
</head>
<body>
`

View File

@ -0,0 +1,12 @@
package templates
var Index = `
<h1><a href=".">Index</a></h1>
<hr/>
<h3>Imports:</h3>
<ul>
{{range $path, $package := .Packages}}
<li><a href="{{$package.Path}}">{{$package.Name}}</a></li>
{{end}}
</ul>
`

View File

@ -0,0 +1,5 @@
package templates
var Info = `
Version: {{.AppVer}} | {{.GoVer}}
`

View File

@ -0,0 +1,9 @@
package templates
var Vanity = `
<h1><a href="../">Index</a></h1>
<hr/>
<p><strong>Name: </strong>{{.Package.Name}}</p>
<p><strong>Source: </strong><a href="{{.Package.Repo}}">{{.Package.Repo}}</a></p>
<p><strong>Description: </strong>{{.Package.Description}}</p>
`

16
vanity.service 100644
View File

@ -0,0 +1,16 @@
[Unit]
Description=Vanity Go Imports
After=syslog.target
After=network.target
[Service]
RestartSec=2s
Type=simple
User=vanity
Group=vanity
ExecStart=/usr/local/bin/vanity server -p 7777
Restart=always
Environment=USER=vanity HOME=/var/lib/vanity
[Install]
WantedBy=multi-user.target