func (g *gorai) initialize() { g.config = loadConfig() // Logger if isConsole() { g.logger = log551.New(&g.config.Framework.CommandLog) } else { g.logger = log551.New(&g.config.Framework.SystemLog) } g.logger.Open() defer g.logger.Close() // Router g.router = router551.Load() // ModelManager g.modelManager = model551.Load() // Add Auth Model g.modelManager.Add(auth551.NewUserModel, auth551.NewUserModelPointer) g.modelManager.Add(auth551.NewUserTokenModel, auth551.NewUserTokenModelPointer) // Auth g.auth = auth551.Load(&g.config.Framework.Auth) }
func rootFunc(w http.ResponseWriter, r *http.Request) { g := Load() l := log551.New(&g.config.Framework.SystemLog) l.Open() defer l.Close() cookie := cookie551.New(w, r) sid := g.sid(cookie) l.Debugf("%s SID: %s", sid[:10], sid) session := memcache551.New(&g.config.Framework.Session.Server, sid) route := g.router.FindRouteByPathMatch(r.Method, r.URL.Path) response551.UrlFunction = g.router.Url var data interface{} = nil if route != nil { mysql := mysql551.New(&g.config.Framework.Database) mysql.Open() defer mysql.Close() l.Debugf("%s --[ Routing ]--", sid[:10]) l.Debugf("%s Path: %s", sid[:10], r.URL.Path) l.Debugf("%s Neme: %s", sid[:10], route.Name()) c := container551.New() c.SetSID(sid) c.SetResponseWriter(w) c.SetRequest(r) c.SetLogger(l) c.SetCookie(cookie) c.SetDb(mysql) c.SetSession(session) c.SetModel(g.modelManager) c.SetAuth(g.auth) c.SetUrlFunc(g.router.Url) baseUrl := "https://" + g.config.Framework.WebServerSSL.Host if g.config.Framework.WebServerSSL.Port != "443" { baseUrl = baseUrl + ":" + g.config.Framework.WebServerSSL.Port } c.SetBaseURL(baseUrl) response551.BaseUrl = baseUrl action := route.Action() data = action(c) response551.Response(w, r, data, route.PackageName(), route.Name(), c.User(), g.config.Application) } else { l.Errorf("%s --[ Route not found ]--", sid[:10]) l.Errorf("%s UA: %s", sid[:10], r.UserAgent()) l.Errorf("%s Method: %s", sid[:10], r.Method) l.Errorf("%s Path: %s", sid[:10], r.URL.Path) l.Errorf("%s --[/Route not found ]--", sid[:10]) data = response551.Error(404, "Route not found.") response551.Response(w, r, data, "", "", nil, g.config.Application) } }
func consoleHandler() { g := Load() l := log551.New(&g.config.Framework.CommandLog) l.Open() defer l.Close() sid := secure551.Hash() // Routing name := os.Args[1] l.Debugf("%s [ Command ] %s", sid[:10], name) route := g.router.FindRouteByName(router551.COMMAND.String(), name) if route == nil { l.Errorf("%s %s", sid[:10], "Action not found...") return } // Options optionArgs := os.Args[2:] if len(optionArgs)%2 == 1 { l.Errorf("%s %s", sid[:10], "Missing options.") } options := make(map[string]string, len(optionArgs)/2) for i := 0; i < len(optionArgs); i += 2 { options[optionArgs[i][1:]] = optionArgs[i+1] } mysql := mysql551.New(&g.config.Framework.Database) mysql.Open() defer mysql.Close() session := memcache551.New(&g.config.Framework.Session.Server, sid) c := container551.New() c.SetSID(sid) c.SetResponseWriter(nil) c.SetRequest(nil) c.SetLogger(l) c.SetCookie(nil) c.SetDb(mysql) c.SetSession(session) c.SetModel(g.modelManager) c.SetCommandOptions(options) if g.config.Framework.WebServerSSL.Port == "443" { c.SetBaseURL("https://" + g.config.Framework.WebServerSSL.Host) } else { c.SetBaseURL("https://" + g.config.Framework.WebServerSSL.Host + ":" + g.config.Framework.WebServerSSL.Port) } action := route.Action() action(c) }
func TestNew(t *testing.T) { log1 := log551.New(nil) log2 := log551.New(nil) if log1 == nil { t.Error("インスタンス生成に失敗しました。") } if log2 == nil { t.Error("インスタンス生成に失敗しました。") } t.Logf("log1: [%p] %#v", &log1, log1) t.Logf("log2: [%p] %#v", &log2, log2) if &log1 == &log2 { t.Error("インスタンス生成に失敗しました。") } }
func BenchmarkCache(b *testing.B) { // b.SkipNow() conf := log551.Config{ Debug: log551.ConfigDetail{ EachTime: false, Cache: false, }, Information: log551.ConfigDetail{ EachTime: false, Cache: true, }, Warning: log551.ConfigDetail{ EachTime: false, Cache: true, }, Error: log551.ConfigDetail{ EachTime: false, Cache: true, }, Critical: log551.ConfigDetail{ EachTime: false, Cache: true, }, Path: "/var/log/gorai/test.log", } l := log551.New(&conf) b.ResetTimer() for i := 0; i < b.N; i++ { l.Open() l.Debug("test string") l.Debugf("%s %s", "test", "string") l.Debugln("test string") l.Information("test string") l.Informationf("%s %s", "test", "string") l.Informationln("test string") l.Warning("test string") l.Warningf("%s %s", "test", "string") l.Warningln("test string") l.Error("test string") l.Errorf("%s %s", "test", "string") l.Errorln("test string") l.Critical("test string") l.Criticalf("%s %s", "test", "string") l.Criticalln("test string") l.Close() } }
func TestInformationEachTime(t *testing.T) { conf := log551.Config{ Debug: log551.ConfigDetail{ EachTime: false, Cache: false, }, Information: log551.ConfigDetail{ EachTime: true, Cache: false, }, Warning: log551.ConfigDetail{ EachTime: false, Cache: false, }, Error: log551.ConfigDetail{ EachTime: false, Cache: false, }, Critical: log551.ConfigDetail{ EachTime: false, Cache: false, }, Path: "/var/log/gorai/test.log", } l := log551.New(&conf) l.Open() defer l.Close() l.Debug("test string") l.Debugf("%s %s", "test", "string") l.Debugln("test string") l.Information("test string") l.Informationf("%s %s", "test", "string") l.Informationln("test string") l.Warning("test string") l.Warningf("%s %s", "test", "string") l.Warningln("test string") l.Error("test string") l.Errorf("%s %s", "test", "string") l.Errorln("test string") l.Critical("test string") l.Criticalf("%s %s", "test", "string") l.Criticalln("test string") }
func BenchmarkNew(b *testing.B) { b.ResetTimer() for i := 0; i < b.N; i++ { _ = log551.New(nil) } }