-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
232 additions
and
2 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
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)) | ||
} |
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,9 @@ | ||
package main | ||
|
||
const banner = ` | ||
______ __ ____ ____ | ||
/ ____/ / / ___ _ __ / __ \ / __ ) | ||
/ /_ / / / _ \ | |/_/ / / / / / __ | | ||
/ __/ / / / __/ _> < / /_/ / / /_/ / | ||
/_/ /_/ \___/ /_/|_| /_____/ /_____/ | ||
` |
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,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() | ||
} |
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,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) | ||
} |
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,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 | ||
} |