Browse Source

Move to urfav/cli

main
Matt Baer 3 years ago
parent
commit
7681ea5a05
5 changed files with 100 additions and 21 deletions
  1. +27
    -18
      app.go
  2. +25
    -1
      cmd/publicbio/main.go
  3. +36
    -0
      cmd/publicbio/web.go
  4. +1
    -0
      go.mod
  5. +11
    -2
      go.sum

+ 27
- 18
app.go View File

@@ -2,7 +2,6 @@ package publicbio

import (
"encoding/json"
"flag"
"fmt"
"github.com/gorilla/mux"
"github.com/gorilla/schema"
@@ -13,9 +12,19 @@ import (
"os"
)

const (
serverSoftware = "Public Bio"
softwareURL = "https://publicb.io"
)

var (
// Software version can be set from git env using -ldflags
softwareVer = "0.1.0"
)

type app struct {
router *mux.Router
cfg *config
cfg *Config

singleUser *Profile
}
@@ -24,26 +33,21 @@ func (app *app) multiUser() bool {
return app.singleUser == nil
}

type config struct {
host string
port int
type Config struct {
Host string
Port int
static bool

UserFile string
}

func Serve() {
func Serve(cfg *Config) {
app := &app{
cfg: &config{},
cfg: cfg,
}

flag.IntVar(&app.cfg.port, "p", 8080, "Port to start server on")
flag.StringVar(&app.cfg.host, "h", "https://public.bio", "Site's base URL")
var userFile string
flag.StringVar(&userFile, "u", "", "Configuration file for single-user site")
flag.BoolVar(&app.cfg.static, "s", false, "Generate static page instead of serving the site")
flag.Parse()

if userFile != "" {
f, err := ioutil.ReadFile(userFile)
if cfg.UserFile != "" {
f, err := ioutil.ReadFile(cfg.UserFile)
if err != nil {
log.Fatal("File error: %v\n", err)
}
@@ -67,11 +71,16 @@ func Serve() {
initRoutes(app)

http.Handle("/", app.router)
log.Printf("Serving on localhost:%d", app.cfg.port)
http.ListenAndServe(fmt.Sprintf(":%d", app.cfg.port), nil)
log.Printf("Serving on http://localhost:%d", app.cfg.Port)
http.ListenAndServe(fmt.Sprintf(":%d", app.cfg.Port), nil)
}

func initConverter() {
formDecoder := schema.NewDecoder()
formDecoder.RegisterConverter(converter.NullJSONString{}, converter.JSONNullString)
}

// FormatVersion constructs the version string for the application
func FormatVersion() string {
return serverSoftware + " " + softwareVer
}

+ 25
- 1
cmd/publicbio/main.go View File

@@ -2,8 +2,32 @@ package main

import (
"github.com/abunchtell/publicbio"
"github.com/urfave/cli/v2"
"github.com/writeas/web-core/log"
"os"
)

func main() {
publicbio.Serve()
app := &cli.App{
Name: "Public Bio",
Usage: "A public bio and link page builder.",
Version: publicbio.FormatVersion(),
Action: serveAction,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "u",
Usage: "Configuration file for single-user site",
},
},
}

app.Commands = []*cli.Command{
&cmdServe,
}

err := app.Run(os.Args)
if err != nil {
log.Error(err.Error())
os.Exit(1)
}
}

+ 36
- 0
cmd/publicbio/web.go View File

@@ -0,0 +1,36 @@
package main

import (
"github.com/abunchtell/publicbio"
"github.com/urfave/cli/v2"
)

var (
cmdServe = cli.Command{
Name: "serve",
Aliases: []string{"web"},
Usage: "Run web application",
Action: serveAction,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "host",
Usage: "Site's base URL",
},
&cli.IntFlag{
Name: "p",
Value: 8080,
Usage: "Port to start server on",
},
},
}
)

func serveAction(c *cli.Context) error {
cfg := &publicbio.Config{
Host: c.String("host"),
Port: c.Int("p"),
UserFile: c.String("u"),
}
publicbio.Serve(cfg)
return nil
}

+ 1
- 0
go.mod View File

@@ -8,6 +8,7 @@ require (
github.com/gorilla/schema v1.2.0
github.com/gorilla/sessions v1.2.1
github.com/microcosm-cc/bluemonday v1.0.5
github.com/urfave/cli/v2 v2.3.0
github.com/writeas/impart v1.1.1
github.com/writeas/saturday v1.7.2-0.20210208194904-b20e8379b9db
github.com/writeas/web-core v1.3.0


+ 11
- 2
go.sum View File

@@ -1,7 +1,10 @@
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/aymerick/douceur v0.2.0 h1:Mv+mAeH1Q+n9Fr+oyamOlAkUNPWPlA8PPGR0QAaYuPk=
github.com/aymerick/douceur v0.2.0/go.mod h1:wlT5vV2O3h55X9m7iVYN0TBM0NH/MmbLnd30/FjWUq4=
github.com/chris-ramon/douceur v0.2.0 h1:IDMEdxlEUUBYBKE4z/mJnFyVXox+MjuEVDJNN27glkU=
github.com/chris-ramon/douceur v0.2.0/go.mod h1:wDW5xjJdeoMm1mRt4sD4c/LbF/mWdEpRXQKjTR8nIBE=
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/embedas/go-embed v1.0.0 h1:cu3fQFT0q6l9YqZQy/pD0XHIa6T33E1U31OMx6XSm34=
github.com/embedas/go-embed v1.0.0/go.mod h1:vI9OozfD8cJOtxkhW4mklPqsW5TVeZLg5+8zkCAKvdk=
github.com/go-sql-driver/mysql v1.5.0 h1:ozyZYNQW3x3HtqT1jira07DN2PArx2v7/mN66gGcHOs=
@@ -24,8 +27,14 @@ github.com/kylemcc/twitter-text-go v0.0.0-20180726194232-7f582f6736ec/go.mod h1:
github.com/microcosm-cc/bluemonday v1.0.2/go.mod h1:iVP4YcDBq+n/5fb23BhYFvIMq/leAFZyRl6bYmGDlGc=
github.com/microcosm-cc/bluemonday v1.0.5 h1:cF59UCKMmmUgqN1baLvqU/B1ZsMori+duLVTLpgiG3w=
github.com/microcosm-cc/bluemonday v1.0.5/go.mod h1:8iwZnFn2CDDNZ0r6UXhF4xawGvzaqzCRa1n3/lO3W2w=
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/urfave/cli/v2 v2.3.0 h1:qph92Y649prgesehzOrQjdWyxFOp/QVM+6imKHad91M=
github.com/urfave/cli/v2 v2.3.0/go.mod h1:LJmUH05zAU44vOAcrfzZQKsZbVcdbOG8rtL3/XcUArI=
github.com/writeas/impart v1.1.0/go.mod h1:g0MpxdnTOHHrl+Ca/2oMXUHJ0PcRAEWtkCzYCJUXC9Y=
github.com/writeas/impart v1.1.1 h1:RyA9+CqbdbDuz53k+nXCWUY+NlEkdyw6+nWanxSBl5o=
github.com/writeas/impart v1.1.1/go.mod h1:g0MpxdnTOHHrl+Ca/2oMXUHJ0PcRAEWtkCzYCJUXC9Y=
@@ -33,12 +42,12 @@ github.com/writeas/openssl-go v1.0.0/go.mod h1:WsKeK5jYl0B5y8ggOmtVjbmb+3rEGqSD2
github.com/writeas/saturday v1.6.0/go.mod h1:ETE1EK6ogxptJpAgUbcJD0prAtX48bSloie80+tvnzQ=
github.com/writeas/saturday v1.7.2-0.20210208194904-b20e8379b9db h1:rFNr1KZd6q2Mk5UbpBi0xx2fh91PjhNmwHsGhr90M7c=
github.com/writeas/saturday v1.7.2-0.20210208194904-b20e8379b9db/go.mod h1:ESndWfThOIwykK+HoDn2p45YPDRlUC0gzmQWxivqEVc=
github.com/writeas/saturday v2.0.0+incompatible h1:YKdG8l62u26YM7PGP9SJ2Pff3Z0ueGZeo/gipZj5c7c=
github.com/writeas/saturday v2.0.0+incompatible/go.mod h1:ETE1EK6ogxptJpAgUbcJD0prAtX48bSloie80+tvnzQ=
github.com/writeas/web-core v1.3.0 h1:oEVJBPZhrFMf9eya+DvFUX1i8NZGZe1eu5sQXnhuzKw=
github.com/writeas/web-core v1.3.0/go.mod h1:DzNxa0YLV/wNeeWeHFPNa/nHmyJBFIIzXN/m9PpDm5c=
golang.org/x/crypto v0.0.0-20190131182504-b8fe1690c613/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/net v0.0.0-20181220203305-927f97764cc3 h1:eH6Eip3UpmR+yM/qI9Ijluzb1bNv/cAU/n+6l8tRSis=
golang.org/x/net v0.0.0-20181220203305-927f97764cc3/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v1 v1.0.0-20140924161607-9f9df34309c0/go.mod h1:WDnlLJ4WF5VGsH/HVa3CI79GS0ol3YnhVnKP89i0kNg=
gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

Loading…
Cancel
Save