-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from nrfta/feat/server-air
Add start/serve command to start Go app using air
- Loading branch information
Showing
6 changed files
with
210 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.