Skip to content

Commit

Permalink
sorry, forgot the main entry point
Browse files Browse the repository at this point in the history
  • Loading branch information
funkygao committed May 30, 2023
1 parent 5284a4d commit e3e2fdb
Show file tree
Hide file tree
Showing 6 changed files with 232 additions and 2 deletions.
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@
# Dependency directories (remove the comment below to include it)
# vendor/

flexdb
cmd/flexdb/flexdb
cmd/lx/lx
lx

node_modules/
Expand Down
29 changes: 29 additions & 0 deletions cmd/flexdb/apis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package main

import (
"fmt"

"github.com/agile-app/flexdb/internal/router"
"github.com/funkygao/columnize"
"github.com/pmylund/sortutil"
)

func showAPIs() {
type api struct {
path string
method string
handler interface{}
}
routes := router.Offer().Routes()
apis := make([]api, 0, len(routes))
for _, route := range router.Offer().Routes() {
apis = append(apis, api{method: route.Method, path: route.Path, handler: route.Handler})
}
sortutil.AscByField(apis, "path")

lines := []string{"Resource|Verb"}
for _, api := range apis {
lines = append(lines, fmt.Sprintf("%s|%s", api.path, api.method))
}
fmt.Println(columnize.SimpleFormat(lines))
}
9 changes: 9 additions & 0 deletions cmd/flexdb/banner.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package main

const banner = `
______ __ ____ ____
/ ____/ / / ___ _ __ / __ \ / __ )
/ /_ / / / _ \ | |/_/ / / / / / __ |
/ __/ / / / __/ _> < / /_/ / / /_/ /
/_/ /_/ \___/ /_/|_| /_____/ /_____/
`
43 changes: 43 additions & 0 deletions cmd/flexdb/flag.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package main

import (
"flag"
"fmt"
"os"
)

var (
configFn string

options struct {
logfile string
loglevel string

printVersion bool
showAPIsAndExit bool
migrateDB bool
}
)

const (
usage = `flexdb
Flags:
`
)

func parseFlags() {
flag.StringVar(&configFn, "cf", "", "config file name")

flag.BoolVar(&options.showAPIsAndExit, "apis", false, "show RESTful APIs and exit")
flag.BoolVar(&options.migrateDB, "migrate", false, "reset database(Dangerous!)")
flag.BoolVar(&options.printVersion, "ver", false, "show version and exit")
flag.StringVar(&options.logfile, "logfile", "", "master log file path, default stdout")
flag.StringVar(&options.loglevel, "loglevel", "trace", "log level")
flag.Usage = func() {
fmt.Fprint(os.Stderr, usage)
flag.PrintDefaults()
}

flag.Parse()
}
44 changes: 44 additions & 0 deletions cmd/flexdb/logging.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package main

import (
"fmt"
"io/ioutil"
"log"
"os"
"syscall"
"time"

"github.com/funkygao/golib/version"
"github.com/funkygao/log4go"
)

func setupLogging() {
// keep the world silent
log.SetOutput(ioutil.Discard)

level := log4go.ToLogLevel(options.loglevel, log4go.TRACE)
log4go.SetLevel(level)

if options.logfile == "" {
// stdout
return
}

log4go.DeleteFilter("stdout")
rotateEnabled, discardWhenDiskFull := true, false
filer := log4go.NewFileLogWriter(options.logfile, rotateEnabled, discardWhenDiskFull, 0644)
filer.SetFormat("[%d %T] [%L] (%S) %M")
filer.SetRotateKeepDuration(time.Hour * 24 * 30)
filer.SetRotateLines(0)
filer.SetRotateDaily(true)
log4go.AddFilter("file", level, filer)

f, err := os.OpenFile("panic.log", os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0600)
if err != nil {
panic(err)
}
syscall.Dup2(int(f.Fd()), int(os.Stdout.Fd()))
syscall.Dup2(int(f.Fd()), int(os.Stderr.Fd()))
fmt.Fprintf(os.Stderr, "\n%s %s (build: %s)\n===================\n",
time.Now(), version.Version, version.Revision)
}
107 changes: 107 additions & 0 deletions cmd/flexdb/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package main

import (
"database/sql"
"fmt"
"os"
"runtime"
"runtime/debug"
"strings"
"sync"
"syscall"
"time"

"github.com/agile-app/flexdb/internal/diagnostics"
"github.com/agile-app/flexdb/internal/profile"
"github.com/agile-app/flexdb/internal/server"
"github.com/agile-app/flexdb/pkg/store"
"github.com/funkygao/golib/daemon"
"github.com/funkygao/golib/signal"
"github.com/funkygao/golib/version"
"github.com/funkygao/log4go"
)

var (
shutdownOnce sync.Once
shutdownCh = make(chan struct{})
)

func main() {
parseFlags()
setupLogging()

if options.printVersion {
fmt.Printf("FlexDB(%s-%s) built at %s\n", version.Version, version.Revision, version.BuildDate)
fmt.Printf("built with %s\n", version.GoVersion)
os.Exit(0)
}

if err := profile.LoadFrom(configFn); err != nil {
panic(err)
}

if options.migrateDB {
db, err := sql.Open("mysql", profile.P.MetaDSN)
if err != nil {
panic(err.Error())
}
defer db.Close()

store.MigrateDB(db)
return
}

defer func() {
if err := recover(); err != nil {
fmt.Println(err)
debug.PrintStack()
}
}()

t0 := time.Now()

s := server.New(shutdownCh)
s.PublishAPIs()

if options.showAPIsAndExit {
showAPIs()
os.Exit(0)
}

// will run as daemon

if runtime.GOOS == "linux" && !options.showAPIsAndExit {
daemon.EnsureServerUlimit()
}

fmt.Fprintln(os.Stderr, banner)

log4go.Info("pprof started at %s", profile.P.PprofEndpoint)
diagnostics.Start(profile.P.PprofEndpoint)
go func() {
log4go.Error(<-diagnostics.Errors)
}()

// TODO graceful shutdown
if false {
signal.RegisterHandler(func(sig os.Signal) {
shutdownOnce.Do(func() {
log4go.Info("FlexDB(%s-%s) received signal: %s, start graceful shutdown...", version.Version, version.Revision,
strings.ToUpper(sig.String()))

close(shutdownCh)
})
}, /*syscall.SIGINT, */ syscall.SIGTERM) // yes we ignore HUP
}

s.Prepare()
log4go.Trace("%+v", *profile.P)
log4go.Info("FlexDB(%s-%s) started", version.Version, version.Revision)
if err := s.ServeForever(); err != nil {
fmt.Fprintf(os.Stderr, err.Error())
return
}

log4go.Info("%s-%s, %s, bye!", version.Version, version.Revision, time.Since(t0))
log4go.Close() // flush
}

0 comments on commit e3e2fdb

Please sign in to comment.