Ejemplo n.º 1
0
func BqLog() func(*wcg.Response, *wcg.Request) {
	return func(res *wcg.Response, req *wcg.Request) {
		fun := middleware.AccessLog(os.Stderr, "")
		fun(res, req)

		p := req.URL().Path
		if req.URL().RawQuery != "" {
			p = fmt.Sprintf("%s?%s", p, req.URL().RawQuery)
		}
		req.Logger.InfoData(&AccessLog{
			RequestTime:  res.StartTime,
			Ip:           req.RemoteHost(),
			Method:       req.Method(),
			Path:         p,
			Proto:        req.Proto(),
			Status:       res.StatusCode,
			Size:         res.WrittenBytes(),
			Referer:      req.Referer(),
			UserAgent:    req.UserAgent(),
			ResponseTime: res.ResponseTime().Seconds(),
		})
		if LogSinkConfig.IsBigQueryEnabled() {
			if err := sendToBigQuery(req); err != nil {
				req.Logger.Error("Failed to send logs to bigquery: %v", err)
			}
		} else {
			req.Logger.Debug("BigQuery Logging is disabled.")
		}
		req.Logger.Debug("All of log data should be processed before this line.")
	}
}
Ejemplo n.º 2
0
func main() {
	s := wcg.NewLocalServer()
	// middleware configuration
	// s.Routes.Before(...)

	// Application Routes
	s.Routes().Get("/", func(res *wcg.Response, req *wcg.Request) {
		res.WriteString("Hello World\n")
	})
	// middleware configuration
	s.Routes().After(middleware.AccessLog(os.Stdout, ""))

	// starting server
	s.Run()
	fmt.Printf("[%d] Server started at %s\n", os.Getpid(), s.Addr)

	// Wait for SIGINT (Ctrl-c) or SIGTERM(kill pid) to quit.
	c := make(chan os.Signal, 1)
	signal.Notify(c)
	for {
		sig := <-c
		if sig == os.Interrupt || sig == syscall.SIGTERM {
			s.Stop(true)
			fmt.Printf("Stopped.\n")
			os.Exit(0)
		}
	}
}
Ejemplo n.º 3
0
func startServer() {
	app := gae.NewServer()
	routes := app.Routes()

	dir, _ := os.Getwd()
	wcg.ViewConfig.BaseDir = filepath.Join(dir, "app/templates")

	routes.Before(func(res *wcg.Response, req *wcg.Request) {
		if req.URL().Path == "/favicon.ico" {
			res.WriteHeader(404)
			res.End()
		}
	})

	routes.Before(middleware.StaticFile("/static", filepath.Join(dir, "app/static")))
	before, after := middleware.SessionSupport(SessionConfig)

	routes.Before(before)
	routes.Before(func(res *wcg.Response, req *wcg.Request) {
		res.SetLocal("title", AppConfig.SiteTitle)
		res.SetLocal("copyright", AppConfig.Copyright)
	})

	routes.Post("/*", middleware.CSRFSupport())
	routes.Put("/*", middleware.CSRFSupport())
	routes.Delete("/*", middleware.CSRFSupport())

	configureOAuth(routes)
	routes.Get("/", TopHandler)
	configureBlogs(routes)
	configurePosts(routes)

	routes.After(after)
	routes.After(middleware.AccessLog(os.Stderr, ""))

	app.Run()
}