mirror of
https://github.com/Neur0toxine/demo-web-service-go.git
synced 2024-12-05 02:56:03 +03:00
separate commands for the app
This commit is contained in:
parent
64256a8d2f
commit
02435248ee
2
Makefile
2
Makefile
@ -9,7 +9,7 @@ all: clean build run
|
|||||||
|
|
||||||
run:
|
run:
|
||||||
@echo "==> Running"
|
@echo "==> Running"
|
||||||
@${BIN}
|
@${BIN} run
|
||||||
|
|
||||||
build: deps fmt
|
build: deps fmt
|
||||||
@echo "==> Building for prod"
|
@echo "==> Building for prod"
|
||||||
|
10
cmd/parser.go
Normal file
10
cmd/parser.go
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
package cmd
|
||||||
|
|
||||||
|
import "github.com/jessevdk/go-flags"
|
||||||
|
|
||||||
|
// Parser will parse command line
|
||||||
|
var Parser *flags.Parser
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
Parser = flags.NewParser(nil, flags.Default)
|
||||||
|
}
|
29
cmd/run.go
Normal file
29
cmd/run.go
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
package cmd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/Neur0toxine/demo-web-service-go/internal"
|
||||||
|
"github.com/brianvoe/gofakeit/v6"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
_, err := Parser.AddCommand("run", "Run the application", "", &RunCommand{})
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// RunCommand is used to run the server
|
||||||
|
type RunCommand struct{}
|
||||||
|
|
||||||
|
func (c *RunCommand) Execute(args []string) error {
|
||||||
|
gofakeit.Seed(time.Now().UnixNano())
|
||||||
|
go internal.ProcessSignals()
|
||||||
|
r := internal.Router()
|
||||||
|
if err := r.Run(":8080"); err != nil {
|
||||||
|
return fmt.Errorf("cannot start the server on :8080 > %w", err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
1
go.mod
1
go.mod
@ -5,6 +5,7 @@ go 1.17
|
|||||||
require (
|
require (
|
||||||
github.com/brianvoe/gofakeit/v6 v6.7.1
|
github.com/brianvoe/gofakeit/v6 v6.7.1
|
||||||
github.com/gin-gonic/gin v1.7.4
|
github.com/gin-gonic/gin v1.7.4
|
||||||
|
github.com/jessevdk/go-flags v1.5.0
|
||||||
github.com/stretchr/testify v1.7.0
|
github.com/stretchr/testify v1.7.0
|
||||||
)
|
)
|
||||||
|
|
||||||
|
3
go.sum
3
go.sum
@ -26,6 +26,8 @@ github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiu
|
|||||||
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
|
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
|
||||||
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||||
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
||||||
|
github.com/jessevdk/go-flags v1.5.0 h1:1jKYvbxEjfUl0fmqTCOfonvskHHXMjBySTLW4y9LFvc=
|
||||||
|
github.com/jessevdk/go-flags v1.5.0/go.mod h1:Fw0T6WPc1dYxT4mKEZRfG5kJhaTDP9pj1c2EWnYs/m4=
|
||||||
github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
|
github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
|
||||||
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
|
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
|
||||||
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
|
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
|
||||||
@ -77,6 +79,7 @@ golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5h
|
|||||||
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069 h1:siQdpVirKtzPhKl3lZWozZraCFObP8S1v6PRp0bLrtU=
|
golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069 h1:siQdpVirKtzPhKl3lZWozZraCFObP8S1v6PRp0bLrtU=
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
package main
|
package internal
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
@ -7,6 +7,7 @@ import (
|
|||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// AuthorsHandler will return formatted JSOn response with authors
|
||||||
func AuthorsHandler(c *gin.Context) {
|
func AuthorsHandler(c *gin.Context) {
|
||||||
c.IndentedJSON(http.StatusOK, repository.Authors())
|
c.IndentedJSON(http.StatusOK, repository.Authors())
|
||||||
}
|
}
|
@ -1,4 +1,4 @@
|
|||||||
package main
|
package internal
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
@ -16,7 +16,7 @@ func TestAuthorsHandler(t *testing.T) {
|
|||||||
req, err := http.NewRequest(http.MethodGet, "/authors", nil)
|
req, err := http.NewRequest(http.MethodGet, "/authors", nil)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
r := router()
|
r := Router()
|
||||||
rec := httptest.NewRecorder()
|
rec := httptest.NewRecorder()
|
||||||
r.ServeHTTP(rec, req)
|
r.ServeHTTP(rec, req)
|
||||||
|
|
12
internal/router.go
Normal file
12
internal/router.go
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
package internal
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Router controls the routes
|
||||||
|
func Router() (r *gin.Engine) {
|
||||||
|
r = gin.New()
|
||||||
|
r.GET("/authors", AuthorsHandler)
|
||||||
|
return
|
||||||
|
}
|
22
internal/signals.go
Normal file
22
internal/signals.go
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
package internal
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
"os/signal"
|
||||||
|
"syscall"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ProcessSignals will react to app termination. It doesn't do much for now, but it can be useful later.
|
||||||
|
// For example, we can close active DB or WebSocket connections here, or perform any other actions
|
||||||
|
// related to the graceful shutdown.
|
||||||
|
func ProcessSignals() {
|
||||||
|
c := make(chan os.Signal, 1)
|
||||||
|
signal.Notify(c, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
|
||||||
|
|
||||||
|
for {
|
||||||
|
<-c
|
||||||
|
log.Println("Quitting...")
|
||||||
|
os.Exit(0)
|
||||||
|
}
|
||||||
|
}
|
16
main.go
16
main.go
@ -1,17 +1,17 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
"os"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/brianvoe/gofakeit/v6"
|
"github.com/Neur0toxine/demo-web-service-go/cmd"
|
||||||
|
"github.com/jessevdk/go-flags"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
gofakeit.Seed(time.Now().UnixNano())
|
if _, err := cmd.Parser.Parse(); err != nil {
|
||||||
go processSignals()
|
if flagsErr, ok := err.(*flags.Error); ok && flagsErr.Type == flags.ErrHelp {
|
||||||
r := router()
|
os.Exit(0)
|
||||||
if err := r.Run(":8080"); err != nil {
|
}
|
||||||
log.Fatalf("cannot start the server on :8080 > %s", err)
|
os.Exit(-1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,9 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import "github.com/gin-gonic/gin"
|
|
||||||
|
|
||||||
func router() (r *gin.Engine) {
|
|
||||||
r = gin.New()
|
|
||||||
r.GET("/authors", AuthorsHandler)
|
|
||||||
return
|
|
||||||
}
|
|
19
signals.go
19
signals.go
@ -1,19 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"log"
|
|
||||||
"os"
|
|
||||||
"os/signal"
|
|
||||||
"syscall"
|
|
||||||
)
|
|
||||||
|
|
||||||
func processSignals() {
|
|
||||||
c := make(chan os.Signal, 1)
|
|
||||||
signal.Notify(c, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
|
|
||||||
|
|
||||||
for {
|
|
||||||
<-c
|
|
||||||
log.Println("Quitting...")
|
|
||||||
os.Exit(0)
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user