first version
This commit is contained in:
+28
@@ -0,0 +1,28 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"rpirelay/cmd/server"
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var (
|
||||
rootCmd *cobra.Command
|
||||
)
|
||||
|
||||
func init() {
|
||||
godotenv.Load()
|
||||
}
|
||||
|
||||
func main() {
|
||||
rootCmd = &cobra.Command{
|
||||
Use: "rpi-relay",
|
||||
}
|
||||
|
||||
rootCmd.AddCommand(server.NewCommand())
|
||||
|
||||
if err := rootCmd.Execute(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"os/signal"
|
||||
"rpirelay/config"
|
||||
"rpirelay/internal/api"
|
||||
"sync"
|
||||
"syscall"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var cfgPaths []string
|
||||
|
||||
func NewCommand() *cobra.Command {
|
||||
cmd := &cobra.Command{
|
||||
Use: "server",
|
||||
Run: startServer,
|
||||
}
|
||||
|
||||
cmd.Flags().StringSliceVarP(&cfgPaths, "config", "c", []string{}, "config file paths")
|
||||
|
||||
return cmd
|
||||
}
|
||||
|
||||
func startServer(cmd *cobra.Command, args []string) {
|
||||
cfg, err := config.Load(cfgPaths...)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
lock := make(chan os.Signal, 1)
|
||||
signal.Notify(lock, syscall.SIGTERM, syscall.SIGINT)
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
svcCtx, svcCancel := context.WithCancel(ctx)
|
||||
|
||||
wg := &sync.WaitGroup{}
|
||||
|
||||
wg.Add(1)
|
||||
|
||||
// get system signal
|
||||
go func() {
|
||||
<-lock
|
||||
svcCancel()
|
||||
}()
|
||||
|
||||
go func() {
|
||||
err := <-api.Start(svcCtx, cfg)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
wg.Done()
|
||||
}()
|
||||
|
||||
// wait all service shutdown
|
||||
wg.Wait()
|
||||
}
|
||||
Reference in New Issue
Block a user