Skip to content

Commit

Permalink
Merge pull request #5 from aosasona/add-docs
Browse files Browse the repository at this point in the history
Added docs and init functionality
  • Loading branch information
aosasona committed Mar 30, 2024
2 parents 7b7b283 + bb4b45d commit 2108e58
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 2 deletions.
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Vanity

> [!WARNING]
> This was mostly made to suit my needs, some things may or may not work for you and I do not spend a lot of time maintaining it, use at your own risk.
A vanity URL service for Go packages (modules, projects and executables)

## Installation

You can install Vanity with Go:

```sh
go install go.trulyao.dev/vanity@latest
```

## Usage

Run the following command to create a config file:
```sh
vanity --init
```

Optionally, you can use `--config=[path]` flag to customize your config file location and name. You can now run Vanity by using the following command:

```sh
vanity --config=path/to/config.json
```

## Docker

Docker is the recommended way to use Vanity but I haven't taken time to setup the workflow yet (there aren't even versions yet at this point) but you can use the unofficially supported image or build from source.

```sh
docker pull trulyao/vanity:latest
```
35 changes: 35 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,48 @@ import (
"os"
)

const defaultConfig = `{
"$schema": "https://go.trulyao.dev/schemas/config.json",
"port": 8080,
"domain": "example.com",
"maxCacheAge": 3600,
"packages": [
{
"name": "foo",
"repo": {
"host": "github.com",
"owner": "user",
"name": "foo"
},
"type": "module",
"readme": "https://raw.githubusercontent.com/user/foo/master/README.md"
}
]
}
`

type Config struct {
Domain string `json:"domain"`
Port uint `json:"port"`
MaxCacheAge int64 `json:"maxCacheAge"`
Packages Packages `json:"packages"`
}

func CreateDefaultConfig(path string) error {
f, err := os.Create(path)
if err != nil {
return err
}

defer f.Close()

if _, err := f.WriteString(defaultConfig); err != nil {
return err
}

return nil
}

func Load(path string) (Config, error) {
var (
c Config
Expand Down
10 changes: 9 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,19 @@ import (
)

func main() {
initConfig := flag.Bool("init", false, "Initialize a new configuration file")
configPath := flag.String("config", "config.json", "Path to the configuration file")
flag.Parse()

zerolog.TimeFieldFormat = zerolog.TimeFormatUnix

if *initConfig {
if err := config.CreateDefaultConfig(*configPath); err != nil {
fmt.Printf("Failed to create default configuration: %s", err.Error())
}
return
}

r := chi.NewRouter()

config, err := config.Load(*configPath)
Expand All @@ -35,8 +43,8 @@ func main() {
r.Use(middleware.Heartbeat("/ping"))

r.Get("/", web.Index)
r.Get("/{package}", web.ServePackage)
r.Get("/schemas/{schema}", web.Schemas)
r.Get("/{package}", web.ServePackage)

srv := &http.Server{
Addr: fmt.Sprintf(":%d", config.Port),
Expand Down
4 changes: 3 additions & 1 deletion web/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ type PackageVars struct {
}

func ServePackage(w http.ResponseWriter, r *http.Request) {
isGoGet := r.URL.Query().Get("go-get") == "1"
pkg := chi.URLParam(r, "package")

if pkg == "" {
http.Error(w, "No package specified", http.StatusBadRequest)
return
Expand Down Expand Up @@ -51,7 +53,7 @@ func ServePackage(w http.ResponseWriter, r *http.Request) {
if p.Type == config.Project {
http.Redirect(w, r, repoURL, http.StatusFound)
return
} else if p.Type == config.Executable {
} else if p.Type == config.Executable && !isGoGet {
http.Redirect(w, r, fmt.Sprintf("%s/releases", repoURL), http.StatusFound)
return
}
Expand Down

0 comments on commit 2108e58

Please sign in to comment.