コード例 #1
0
ファイル: datasource.go プロジェクト: astrata/tango
// Returns a db.DataSource that you can use to connect to a database.
func Config(name string) (string, db.DataSource) {

	var driver string

	data := config.Get(fmt.Sprintf("datasource/%s", name))

	if data == nil {
		panic(fmt.Sprintf("tango: Cannot find %s datasource.", name))
	}

	source := db.DataSource{}

	for key, val := range data.(sugar.Map) {
		switch key {
		case "host":
			source.Host = fmt.Sprintf("%v", val)
		case "database":
			source.Database = fmt.Sprintf("%v", val)
		case "user":
			source.User = fmt.Sprintf("%v", val)
		case "password":
			source.Password = fmt.Sprintf("%v", val)
		case "port":
			port, _ := strconv.Atoi(fmt.Sprintf("%v", val))
			source.Port = port
		case "driver":
			driver = fmt.Sprintf("%v", val)
		}
	}

	return driver, source
}
コード例 #2
0
ファイル: context.go プロジェクト: astrata/tango
func newContext(server *Server, writer http.ResponseWriter, request *http.Request) *Context {

	context := &Context{}

	switch request.Method {
	case "GET":
		context.GET = true
	case "POST":
		context.POST = true
	case "DELETE":
		context.DELETE = true
	case "PUT":
		context.PUT = true
	}

	context.cookieMap = make(map[string]*http.Cookie)

	context.Server = server
	context.Request = request
	context.Writer = writer

	maxSize := to.Int64(config.Get("server/request_max_size"))

	request.ParseMultipartForm(maxSize)

	context.Params = context.getParams()
	context.Cookies = context.getCookies()
	context.Files = context.getFiles()

	server.Context = context

	return context
}
コード例 #3
0
ファイル: server.go プロジェクト: astrata/tango
// Starts a fastcgi/http server.
func (server *Server) Run() error {

	var err error

	server.serveMux.Handle("/", server)

	domain := "unix"
	addr := to.String(config.Get("server/socket"))

	if addr == "" {
		domain = "tcp"
		addr = fmt.Sprintf("%s:%d", to.String(config.Get("server/bind")), to.Int(config.Get("server/port")))
	}

	server.listener, err = net.Listen(domain, addr)

	if err != nil {
		log.Fatalf("Failed to bind on %s: %s", addr, err.Error())
	}

	defer server.listener.Close()

	log.Printf("%s is ready to dance.\n", server.listener.Addr())
	log.Printf("Stop server with ^C.\n")

	fmt.Fprintf(os.Stderr, "\n")

	switch to.String(config.Get("server/type")) {
	case "fastcgi":
		if err == nil {
			fcgi.Serve(server.listener, server.serveMux)
		} else {
			log.Fatalf("Failed to start FastCGI server.")
		}
	default:
		if err == nil {
			http.Serve(server.listener, server.serveMux)
		} else {
			log.Fatalf("Failed to start HTTP server.")
		}
	}

	return nil
}