From b8e0f33c35cc5b52bc83b6cb80b375052d4c6722 Mon Sep 17 00:00:00 2001 From: tsmethurst Date: Wed, 3 Mar 2021 11:28:28 +0100 Subject: [PATCH] start the service using a cli --- .gitignore | 2 +- cmd/gotosocial/main.go | 63 +++++++++++++++++++++++++++++++ cmd/server/{main.go => server.go} | 28 ++++++++++++-- go.mod | 1 + go.sum | 9 +++++ 5 files changed, 98 insertions(+), 5 deletions(-) create mode 100644 cmd/gotosocial/main.go rename cmd/server/{main.go => server.go} (71%) diff --git a/.gitignore b/.gitignore index 32f8a186e..fe72fe018 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,5 @@ # exclude the compiled binary -/server +/gotosocial # exclude built documentation, since readthedocs will build it for us anyway /docs/_build diff --git a/cmd/gotosocial/main.go b/cmd/gotosocial/main.go new file mode 100644 index 000000000..78c7002a2 --- /dev/null +++ b/cmd/gotosocial/main.go @@ -0,0 +1,63 @@ +/* + GoToSocial + Copyright (C) 2021 GoToSocial Authors admin@gotosocial.org + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . +*/ + +package main + +import ( + "os" + + "github.com/gotosocial/gotosocial/cmd/server" + "github.com/sirupsen/logrus" + + "github.com/urfave/cli/v2" +) + +func main() { + app := &cli.App{ + Flags: []cli.Flag{ + &cli.StringFlag{ + Name: "config", + Aliases: []string{"c"}, + Usage: "Load configuration from `FILE`", + }, + &cli.StringFlag{ + Name: "log-level", + Usage: "Log level to run at: debug, info, warn, fatal", + Value: "info", + }, + }, + Commands: []*cli.Command{ + { + Name: "server", + Usage: "gotosocial server-related tasks", + Subcommands: []*cli.Command{ + { + Name: "start", + Usage: "start the gotosocial server", + Action: server.Run, + }, + }, + }, + }, + } + + err := app.Run(os.Args) + if err != nil { + logrus.Fatal(err) + } +} diff --git a/cmd/server/main.go b/cmd/server/server.go similarity index 71% rename from cmd/server/main.go rename to cmd/server/server.go index 7a509e96f..4f2969993 100644 --- a/cmd/server/main.go +++ b/cmd/server/server.go @@ -16,20 +16,39 @@ along with this program. If not, see . */ -package main +package server import ( "context" + "fmt" "os" "os/signal" "syscall" "github.com/gotosocial/gotosocial/internal/db" "github.com/sirupsen/logrus" + "github.com/urfave/cli/v2" ) -func main() { +// getLog will try to set the logrus log level to the +// desired level specified by the user with the --log-level flag +func getLog(c *cli.Context) (*logrus.Logger, error) { log := logrus.New() + logLevel, err := logrus.ParseLevel(c.String("log-level")) + if err != nil { + return nil, err + } + log.SetLevel(logLevel) + return log, nil +} + +// Run starts the gotosocial server +func Run(c *cli.Context) error { + log, err := getLog(c) + if err != nil { + return fmt.Errorf("error creating logger: %s", err) + } + ctx := context.Background() dbConfig := &db.Config{ Type: "POSTGRES", @@ -42,7 +61,7 @@ func main() { } dbService, err := db.NewService(ctx, dbConfig, log) if err != nil { - panic(err) + return err } // catch shutdown signals from the operating system @@ -53,8 +72,9 @@ func main() { // close down all running services in order if err := dbService.Stop(ctx); err != nil { - log.Errorf("error closing dbservice: %s", err) + return fmt.Errorf("error closing dbservice: %s", err) } log.Info("done! exiting...") + return nil } diff --git a/go.mod b/go.mod index 474914cf6..65d4ccd08 100644 --- a/go.mod +++ b/go.mod @@ -13,6 +13,7 @@ require ( github.com/onsi/gomega v1.10.5 // indirect github.com/sirupsen/logrus v1.8.0 github.com/stretchr/testify v1.7.0 // indirect + github.com/urfave/cli/v2 v2.3.0 // indirect golang.org/x/crypto v0.0.0-20210220033148-5ea612d1eb83 // indirect golang.org/x/net v0.0.0-20210226172049-e18ecbb05110 // indirect golang.org/x/sys v0.0.0-20210301091718-77cc2087c03b // indirect diff --git a/go.sum b/go.sum index 6be8e0d5a..b3350dc78 100644 --- a/go.sum +++ b/go.sum @@ -2,6 +2,8 @@ cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMT github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= +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/dave/jennifer v1.3.0/go.mod h1:fIb+770HOpJ2fmN9EPPKOqm1vMGhB+TwXKMZhrIygKg= 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= @@ -88,6 +90,10 @@ github.com/onsi/gomega v1.10.5/go.mod h1:gza4q3jKQJijlu05nKWRCW/GavJumGt8aNRxWg7 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/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= +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/sirupsen/logrus v1.8.0 h1:nfhvjKcUMhBMVqbKHJlk5RPrrfYr/NMo3692g0dwfWU= github.com/sirupsen/logrus v1.8.0/go.mod h1:4GuYW9TZmE769R5STWrRakJc4UqQ3+QQ95fyz7ENv1A= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= @@ -100,6 +106,8 @@ github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo= github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw= github.com/ugorji/go/codec v1.1.7 h1:2SvQaVZ1ouYrrKKwoSk2pzd4A9evlKJb9oTL+OaLUSs= github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY= +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/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= golang.org/x/crypto v0.0.0-20180527072434-ab813273cd59/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20180910181607-0e37d006457b/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= @@ -189,6 +197,7 @@ gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMy gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ= gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=