From b740ccc74e8a09e588a7d80c687ccc0d1ce10272 Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Mon, 12 Oct 2020 14:20:25 -0600 Subject: [PATCH] add /api/version --- README.md | 30 ++++++++++++++++-------------- main.go | 12 ++++++++---- 2 files changed, 24 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 516db71..521ea4f 100644 --- a/README.md +++ b/README.md @@ -30,9 +30,9 @@ The platform, publish URL, and token file can be changed in `.goreleaser.yml`: ```yml env_files: - gitea_token: ~/.config/goreleaser/gitea_token + gitea_token: ~/.config/goreleaser/gitea_token gitea_urls: - api: https://try.gitea.io/api/v1/ + api: https://try.gitea.io/api/v1/ ``` #### Manually @@ -67,9 +67,9 @@ GOOS=linux GOARCH=arm64 go build -mod=vendor -o dist/goserv-linux-arm64 . The example files are located in `./examples` -- Caddyfile (web server config) -- .env (environment variables) -- build.sh +- Caddyfile (web server config) +- .env (environment variables) +- build.sh ```bash export BASE_URL=https://example.com @@ -123,6 +123,8 @@ Authentication: Bearer Here's the API, in brief: +Base URL looks like `https://example.com/api`. + ```txt # Demo Mode Only DELETE /public/reset Drop database and re-initialize @@ -166,9 +168,9 @@ If the documentation is not public hosted you can view it with GoDoc: godoc --http :6060 ``` -- -- -- +- +- +- You can see abbreviated documentation with `go`'s built-in `doc`, for example: @@ -198,10 +200,10 @@ go test -mod=vendor ./... This setup can be run on a VPS, such as Digital Ocean, OVH, or Scaleway for \$5/month with minimal dependencies: -- VPS -- Caddy -- PostgreSQL -- Serviceman +- VPS +- Caddy +- PostgreSQL +- Serviceman **Mac**, **Linux**: @@ -281,8 +283,8 @@ Copyright 2020 The GoServ Authors. All rights reserved. ### Exceptions -- `countries.json` LGPL, taken from -- `flags.json` MIT, taken from +- `countries.json` LGPL, taken from +- `flags.json` MIT, taken from These are probably also in the Public Domain. \ (gathering the official data from any source would yield the same dataset) diff --git a/main.go b/main.go index dcd070e..70b8108 100644 --- a/main.go +++ b/main.go @@ -33,7 +33,7 @@ var ( ) func usage() { - ver() + fmt.Println(ver()) fmt.Println("") fmt.Println("Use 'help '") fmt.Println(" help") @@ -41,8 +41,8 @@ func usage() { fmt.Println(" run") } -func ver() { - fmt.Printf("%s v%s %s (%s)\n", name, version, commit[:7], date) +func ver() string { + return fmt.Sprintf("%s v%s %s (%s)\n", name, version, commit[:7], date) } var defaultAddr = ":3000" @@ -113,7 +113,7 @@ func main() { switch args[1] { case "version": - ver() + fmt.Println(ver()) os.Exit(0) return case "init": @@ -186,6 +186,10 @@ func serve() { r.Use(middleware.Logger) r.Use(middleware.Recoverer) + r.Get("/api/version", func(w http.ResponseWriter, r *http.Request) { + w.Write([]byte(ver() + "\n")) + }) + var pub keypairs.PublicKey = nil if "" != runOpts.pub { var err error