Exemple #1
0
// 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
}
Exemple #2
0
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
}
Exemple #3
0
// 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
}