func Test_PostgresCacher(t *testing.T) {
	Convey("Test postgres cache adapter", t, func() {

		Convey("Basic operations", func() {
			t := tango.New()
			t.Use(cache.New(opt))

			t.Get("/", new(CacheAction))

			resp := httptest.NewRecorder()
			req, err := http.NewRequest("GET", "/", nil)
			So(err, ShouldBeNil)
			t.ServeHTTP(resp, req)
		})

		Convey("Increase and decrease operations", func() {
			t := tango.New()
			t.Use(cache.New(opt))

			t.Get("/", new(Cache2Action))

			resp := httptest.NewRecorder()
			req, err := http.NewRequest("GET", "/", nil)
			So(err, ShouldBeNil)
			t.ServeHTTP(resp, req)
		})
	})
}
func Test_MemcacheCacher(t *testing.T) {
	Convey("Test memcache cache adapter", t, func() {
		opt := cache.Options{
			Adapter:       "memcache",
			AdapterConfig: "127.0.0.1:9090",
		}

		Convey("Basic operations", func() {
			t := tango.New()
			t.Use(cache.New(opt))

			t.Get("/", new(CacheAction))

			resp := httptest.NewRecorder()
			req, err := http.NewRequest("GET", "/", nil)
			So(err, ShouldBeNil)
			t.ServeHTTP(resp, req)
		})

		Convey("Increase and decrease operations", func() {
			t := tango.New()
			t.Use(cache.New(opt))
			t.Get("/", new(Cache2Action))

			resp := httptest.NewRecorder()
			req, err := http.NewRequest("GET", "/", nil)
			So(err, ShouldBeNil)
			t.ServeHTTP(resp, req)
		})
	})
}
func Test_RedisCacher(t *testing.T) {
	Convey("Test redis cache adapter", t, func() {
		opt := cache.Options{
			Adapter:       "redis",
			AdapterConfig: "addr=:6379,prefix=cache:",
		}

		Convey("Basic operations", func() {
			t := tango.New()
			t.Use(cache.New(opt))
			t.Get("/", new(CacheAction))

			resp := httptest.NewRecorder()
			req, err := http.NewRequest("GET", "/", nil)
			So(err, ShouldBeNil)
			t.ServeHTTP(resp, req)
		})

		Convey("Increase and decrease operations", func() {
			t := tango.New()
			t.Use(cache.New(opt))
			t.Get("/", new(Cache2Action))

			resp := httptest.NewRecorder()
			req, err := http.NewRequest("GET", "/", nil)
			So(err, ShouldBeNil)
			t.ServeHTTP(resp, req)
		})
	})
}
func Test_LedisCacher(t *testing.T) {
	Convey("Test ledis cache adapter", t, func() {
		opt := cache.Options{
			Adapter:       "ledis",
			AdapterConfig: "data_dir=./tmp.db",
			Interval:      1,
		}

		Convey("Basic operations", func() {
			t := tango.New()
			t.Use(cache.New(opt))

			t.Get("/", new(CacheAction))

			resp := httptest.NewRecorder()
			req, err := http.NewRequest("GET", "/", nil)
			So(err, ShouldBeNil)
			t.ServeHTTP(resp, req)

			t.Get("/id", new(Cache2Action))

			resp = httptest.NewRecorder()
			req, err = http.NewRequest("GET", "/id", nil)
			So(err, ShouldBeNil)
			t.ServeHTTP(resp, req)
		})
	})
}
Beispiel #5
0
func Run() {

	t = tango.New()

	t.Use(tango.Static(tango.StaticOptions{
		RootPath: utils.Abs(settings.Static.VirtualRoot),
	}))
	t.Use(binding.Bind())
	t.Use(new(time.TimeHandler))
	t.Use(tango.ClassicHandlers...)
	t.Use(renders.New(renders.Options{
		Reload:      true,
		Directory:   utils.Abs(settings.Template.Home),
		Charset:     settings.Template.Charset,
		DelimsLeft:  settings.Template.DelimesLeft,
		DelimsRight: settings.Template.DelimesRight,
		Funcs:       utils.DefaultFuncs(),
	}))
	t.Use(events.Events())
	t.Group("/pair", func(g *tango.Group) {
		g.Get("/all", new(pairs.GetAllRouter))
		g.Post("/new", new(pairs.NewPairRouter))
	})
	t.Get("/", new(page.HomeRouter))

	t.Run(settings.Server.Port)
}
Beispiel #6
0
func InitTango(isDebug bool) *tango.Tango {
	t := tango.New()
	if isDebug {
		t.Use(debug.Debug(debug.Options{
			HideResponseBody: true,
			IgnorePrefix:     "/static",
		}))
	}
	t.Use(tango.ClassicHandlers...)
	sess := session.New(session.Options{
		MaxAge: sessionTimeout,
	})
	t.Use(
		binding.Bind(),
		tango.Static(tango.StaticOptions{
			RootPath: filepath.Join(*homeDir, "static"),
			Prefix:   "static",
		}),
		renders.New(renders.Options{
			Reload:    true,
			Directory: filepath.Join(*homeDir, "templates"),
			Funcs: template.FuncMap{
				"isempty": func(s string) bool {
					return len(s) == 0
				},
				"add": func(a, b int) int {
					return a + b
				},
				"isNil": isNil,
				"i18n":  i18n.Tr,
				"Range": func(size int) []struct{} {
					return make([]struct{}, size)
				},
				"multi": func(a, b int) int {
					return a * b
				},
			},
			Vars: renders.T{
				"GoVer":    strings.Trim(runtime.Version(), "go"),
				"TangoVer": tango.Version(),
				"XormVer":  xorm.Version,
				"NodbVer":  nodb.Version,
			},
		}),
		middlewares.Auth("/login", sess),
		flash.Flashes(sess),
		sess,
	)

	t.Any("/", new(actions.Home))
	t.Any("/login", new(actions.Login))
	t.Any("/logout", new(actions.Logout))
	t.Any("/addb", new(actions.Addb))
	t.Any("/view", new(actions.View))
	t.Any("/del", new(actions.Del))
	t.Any("/delRecord", new(actions.DelRecord))
	t.Any("/chgpass", new(actions.ChgPass))
	t.Get("/test", new(actions.Test))
	return t
}
Beispiel #7
0
func ServeAddr(addr string) error {
	InitServer()

	t := tango.New()
	t.Group("/api", func(g *tango.Group) {
		g.Get("/version", versionHandler)
		g.Post("/shutdown", shutdownHandler)
		g.Post("/programs", addHandler)
		g.Get("/programs", statusHandler)
	})

	go t.Run(addr)

	lis, err := net.Listen("unix", filepath.Join(GOSUV_HOME, "gosuv.sock"))
	if err != nil {
		log.Fatal(err)
	}
	handleSignal(lis)

	pbServ := &PbSuvServer{}
	pbProgram := &PbProgram{}

	grpcServ := grpc.NewServer()
	pb.RegisterGoSuvServer(grpcServ, pbServ)
	pb.RegisterProgramServer(grpcServ, pbProgram)

	pbServ.lis = lis
	grpcServ.Serve(lis)
	return fmt.Errorf("Address: %s has been used", addr)
}
Beispiel #8
0
// new server
// set dstDir to make sure read correct static file
func New(dstDir string) *Server {
	t := tango.New([]tango.Handler{
		tango.Return(),
		tango.Param(),
		tango.Contexts(),
		tango.Recovery(true),
	}...)
	t.Logger().(*log.Logger).SetOutputLevel(log.Lerror)
	return &Server{
		Tango:  t,
		dstDir: dstDir,
	}
}
Beispiel #9
0
func (w *Web) Listen() error {
	w.Tango = tango.New([]tango.Handler{
		tango.Return(),
		tango.Param(),
		tango.Contexts(),
	}...)

	addr := w.Host + ":" + w.Port
	listener, err := net.Listen("tcp", addr)
	if err != nil {
		return err
	}
	w.listener = listener
	server := &http.Server{Addr: addr, Handler: w.Tango}
	go server.Serve(tcpKeepAliveListener{listener.(*net.TCPListener)})
	return nil
}
Beispiel #10
0
func (is *BootstrapService) Init(v interface{}) (*Result, error) {
	opt, ok := v.(BootstrapInitOption)
	if !ok {
		return nil, ErrServiceFuncNeedType(is.Init, opt)
	}
	var err error
	if opt.Config {
		core.Cfg = core.NewConfig()
		if err = core.Cfg.Sync(core.ConfigFile); err != nil {
			return nil, err
		}
	}
	if core.Cfg != nil && opt.Database { // database depends on config
		log.SetLevelByString("error")
		tidb.Debug = false // close tidb debug
		core.Db, err = xorm.NewEngine(core.Cfg.Db.Driver, core.Cfg.Db.DSN)
		if err != nil {
			return nil, err
		}
		core.Db.SetLogger(nil)
		// core.Db.ShowDebug = true
		// core.Db.ShowSQL = true
	}
	if core.Cfg != nil && opt.Server { // server depends on config
		core.Server = tango.New([]tango.Handler{
			tango.Return(),
			tango.Param(),
			tango.Contexts(),
		}...)
		core.Server.Use(tango.Static(tango.StaticOptions{
			RootPath: core.StaticDirectory,
			Prefix:   core.StaticPrefix,
		}))
		core.Server.Use(tango.Static(tango.StaticOptions{
			RootPath: core.ThemeDirectory,
			Prefix:   core.ThemePrefix,
		}))
		core.Server.Use(renders.New(renders.Options{
			Reload:     true,
			Directory:  core.ThemeDirectory,
			Extensions: []string{".tmpl"},
			Funcs: template.FuncMap{
				"TimeUnixFormat":  utils.TimeUnixFormat,
				"TimeUnixFriend":  utils.TimeUnixFriend,
				"Mardown2Str":     utils.Markdown2String,
				"Markdown2HTML":   utils.Markdown2HTML,
				"Nl2BrHTML":       utils.Nl2Br,
				"Nl2BrString":     utils.Nl2BrString,
				"BytesSizeFriend": utils.FriendBytesSize,
			},
		}))
		sessions := session.New(session.Options{
			SessionIdName: core.SessionName,
		})
		core.Server.Use(xsrf.New(time.Hour))
		core.Server.Use(binding.Bind())
		core.Server.Use(sessions)
		core.Server.Use(flash.Flashes(sessions))
	}
	return nil, nil
}