Skip to content

Commit

Permalink
Merge pull request #4 from nrfta/feat/server-air
Browse files Browse the repository at this point in the history
Add start/serve command to start Go app using air
  • Loading branch information
josemarluedke authored Aug 4, 2021
2 parents 69cf872 + aa1dd0a commit 080499f
Show file tree
Hide file tree
Showing 6 changed files with 210 additions and 32 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: CI
on:
push:
branches:
- '**'
- "**"
release:
types: [published]

Expand All @@ -14,8 +14,8 @@ jobs:

strategy:
matrix:
go-version: [1.12.x, 1.13.x, 1.15.x]
os: [ubuntu-latest, macos-latest, windows-latest]
go-version: [1.13.x, 1.15.x, 1.16.x]
os: [ubuntu-latest, macos-latest]

env:
ENV: test
Expand Down
2 changes: 2 additions & 0 deletions cmd/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/nrfta/go-log"
"github.com/nrfta/go-tiger/cmd/cli/db"
"github.com/nrfta/go-tiger/cmd/cli/generator"
"github.com/nrfta/go-tiger/cmd/cli/start"
)

// RootCmd represents the base command when called without any subcommands
Expand All @@ -24,6 +25,7 @@ func Execute() {
}

func init() {
RootCmd.AddCommand(start.StartCmd)
RootCmd.AddCommand(db.DBCmd)
RootCmd.AddCommand(generator.GenerateCmd)
}
16 changes: 16 additions & 0 deletions cmd/cli/start/air_template.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package start

import "fmt"

func defaultAirConfig(appName string) string {
template := `
[build]
bin = "./bin/%s"
cmd = "go build -o ./bin/%s ."
exclude_dir = ["bin", "tests", "templates", "scripts", "db/migrations", "pkg/schemas"]
kill_delay = 500
send_interrupt = true
`

return fmt.Sprintf(template, appName, appName)
}
109 changes: 109 additions & 0 deletions cmd/cli/start/start.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package start

import (
"io/ioutil"
"os"
"os/signal"
"path"
"runtime"
"syscall"

"github.com/cosmtrek/air/runner"
"github.com/nrfta/go-log"
"github.com/nrfta/go-tiger/helpers"
"github.com/spf13/cobra"
)

var (
cfgPath string
debugMode bool
noUlimit bool
)

func setUlimit() error {
if runtime.GOOS == "windows" {
return nil
}

var rLimit syscall.Rlimit
if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit); err != nil {
return err
}
rLimit.Max = 2048

rLimit.Cur = rLimit.Max
return syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rLimit)
}

var StartCmd = &cobra.Command{
Use: "start",
Aliases: []string{"s", "serve"},
Short: "Start serving a Go app (Air)",
Run: func(_ *cobra.Command, _ []string) {
if debugMode {
log.Info("[debug] mode")
}

if !noUlimit {
if debugMode {
log.Info("[debug] set ulimit")
}
setUlimit()
}

if cfgPath == "" {
defaultAirPath := path.Join(helpers.FindRootPath(), ".air.toml")

if _, err := os.Stat(defaultAirPath); err == nil {
cfgPath = defaultAirPath
} else if os.IsNotExist(err) {
appName := helpers.LoadConfig().Meta.ServiceName

file, err := ioutil.TempFile(os.TempDir(), appName+".*.toml")
if err != nil {
log.Fatal(err)
}
defer os.Remove(file.Name())

file.WriteString(defaultAirConfig(appName))
if err := file.Close(); err != nil {
log.Fatal(err)
}

cfgPath = file.Name()
}
}

if debugMode {
log.Info("[debug] Using Config Path:", cfgPath)
}

sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)

var err error
r, err := runner.NewEngine(cfgPath, debugMode)
if err != nil {
log.Fatal(err)
return
}
go func() {
<-sigs
r.Stop()
}()

defer func() {
if e := recover(); e != nil {
log.Fatalf("PANIC: %+v", e)
}
}()

r.Run()
},
}

func init() {
StartCmd.Flags().BoolVarP(&debugMode, "debug", "d", false, "debug mode")
StartCmd.Flags().StringVarP(&cfgPath, "config", "c", "", "config path")
StartCmd.Flags().BoolVar(&noUlimit, "no-ulimit", false, "do not set ulimit")
}
9 changes: 8 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,21 @@ module github.com/nrfta/go-tiger
go 1.12

require (
github.com/cosmtrek/air v1.27.3
github.com/creack/pty v1.1.14 // indirect
github.com/fatih/color v1.12.0 // indirect
github.com/gobuffalo/here v0.6.0
github.com/gobuffalo/packr v1.30.1
github.com/golang-migrate/migrate/v4 v4.7.1
github.com/lib/pq v1.3.0
github.com/markbates/grift v1.5.0
github.com/mattn/go-isatty v0.0.13 // indirect
github.com/neighborly/go-config v0.2.1
github.com/neighborly/go-errors v0.2.0
github.com/neighborly/go-pghelpers v0.4.0
github.com/nrfta/go-log v0.0.0-20200111010408-b08d9985f078
github.com/nrfta/go-log v0.9.1
github.com/pelletier/go-toml v1.9.3 // indirect
github.com/sirupsen/logrus v1.8.1 // indirect
github.com/spf13/cobra v0.0.5
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect
)
Loading

0 comments on commit 080499f

Please sign in to comment.