From 3bf0821c34a1301f85329b6073fed82bfb779d60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torjus=20H=C3=A5kestad?= Date: Sat, 15 Jan 2022 22:19:35 +0100 Subject: [PATCH] Add serve action --- cmd/server/server.go | 80 ++++++++++++++++++++++++++++++++++++++++++-- go.mod | 10 +++++- go.sum | 13 +++++++ 3 files changed, 100 insertions(+), 3 deletions(-) diff --git a/cmd/server/server.go b/cmd/server/server.go index 5d4b70e..e60fb49 100644 --- a/cmd/server/server.go +++ b/cmd/server/server.go @@ -1,7 +1,83 @@ package main -import "fmt" +import ( + "context" + "fmt" + "net/http" + "os" + "os/signal" + "time" + + "git.t-juice.club/torjus/gpaste" + "github.com/urfave/cli/v2" +) + +var ( + version = "dev" + commit = "none" + date = "unknown" +) func main() { - fmt.Println("Starting gpaste server") + cli.VersionFlag = &cli.BoolFlag{Name: "version"} + + app := cli.App{ + Name: "gpaste-server", + Version: fmt.Sprintf("gpaste-server %s-%s (%s)", version, commit, date), + Flags: []cli.Flag{ + &cli.StringFlag{ + Name: "config", + Usage: "Path to config-file.", + }, + }, + Action: ActionServe, + } + + app.Run(os.Args) +} + +func ActionServe(c *cli.Context) error { + configPath := "gpaste-server.toml" + if c.IsSet("config") { + configPath = c.String("config") + } + + f, err := os.Open(configPath) + if err != nil { + return cli.Exit(err, 1) + } + defer f.Close() + cfg, err := gpaste.ServerConfigFromReader(f) + if err != nil { + return cli.Exit(err, 1) + } + + rootCtx, rootCancel := signal.NotifyContext(context.Background(), os.Interrupt) + defer rootCancel() + + httpCtx, httpCancel := context.WithCancel(rootCtx) + defer httpCancel() + + httpShutdownCtx, httpShutdownCancel := context.WithCancel(context.Background()) + defer httpShutdownCancel() + + go func() { + srv := gpaste.NewHTTPServer(cfg) + srv.Addr = cfg.ListenAddr + + // Wait for cancel + go func() { + <-httpCtx.Done() + timeoutCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second) + defer cancel() + srv.Shutdown(timeoutCtx) + }() + if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed { + fmt.Printf("Error: %s\n", err) + } + httpShutdownCancel() + }() + <-httpShutdownCtx.Done() + + return nil } diff --git a/go.mod b/go.mod index e00123f..0d38a58 100644 --- a/go.mod +++ b/go.mod @@ -6,4 +6,12 @@ require github.com/google/uuid v1.3.0 require github.com/go-chi/chi/v5 v5.0.7 -require github.com/pelletier/go-toml v1.9.4 +require ( + github.com/pelletier/go-toml v1.9.4 + github.com/urfave/cli/v2 v2.3.0 +) + +require ( + github.com/cpuguy83/go-md2man/v2 v2.0.1 // indirect + github.com/russross/blackfriday/v2 v2.1.0 // indirect +) diff --git a/go.sum b/go.sum index af8ffdc..981cdc7 100644 --- a/go.sum +++ b/go.sum @@ -1,6 +1,19 @@ +github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= +github.com/cpuguy83/go-md2man/v2 v2.0.1 h1:r/myEWzV9lfsM1tFLgDyu0atFtJ1fXn261LKYj/3DxU= +github.com/cpuguy83/go-md2man/v2 v2.0.1/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= github.com/go-chi/chi/v5 v5.0.7 h1:rDTPXLDHGATaeHvVlLcR4Qe0zftYethFucbjVQ1PxU8= github.com/go-chi/chi/v5 v5.0.7/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8= github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/pelletier/go-toml v1.9.4 h1:tjENF6MfZAg8e4ZmZTeWaWiT2vXtsoO6+iuOjFhECwM= github.com/pelletier/go-toml v1.9.4/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk= +github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +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= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=