コード例 #1
0
ファイル: server.go プロジェクト: AndrewJHart/irlmoji
func Main() {
	// Read in any environment variables we care about
	readEnv()

	var err error

	AWS_AUTH, err = aws.EnvAuth()
	if err != nil {
		panic(err.Error())
	}

	// Open database connection
	db, err := models.OpenDB("postgres", IRLMOJI_DBURI)
	if err != nil {
		log.Fatalf("Error opening database connection: %v", err.Error())
		return
	}
	if err = createAllTables(db); err != nil {
		log.Fatalf("Error creating database table: %v", err.Error())
	}

	// Start setting up martini
	m := martini.Classic()

	// Set up the middleware
	m.Use(gzip.All())
	m.Use(render.Renderer())
	m.Use(BackchannelAuth(IRLMOJI_API_BASIC_USER))

	// Inject the database
	m.Map(db)

	// Map the URL routes
	m.Get("/", HandleIndex)

	// User routes (see handlers_user.go)
	m.Get("/api/v1/users/current.json", HandleGetCurrentUser)
	m.Post("/api/v1/users/twitter.json", binding.Json(models.UserForm{}), HandleCreateUserByTwitter)

	// IRLMoji routes (see handlers_irlmoji.go)
	m.Get("/api/v1/timelines/home.json", binding.Form(Limit{}), HandleGetHomeTimeline)
	m.Get("/api/v1/timelines/user/username/:username.json", binding.Form(Limit{}), HandleGetUserTimeline)
	m.Get("/api/v1/timelines/emoji/:emoji.json", binding.Form(Limit{}), HandleGetEmojiTimeline)
	m.Get("/api/v1/irlmoji/id/:irlmojiId.json", binding.Form(Limit{}), HandleGetIRLMoji)
	m.Delete("/api/v1/irlmoji/id/:irlmojiId.json", HandleDeleteIRLMoji)
	m.Post("/api/v1/irlmoji.json", binding.Json(models.IRLMoji{}), HandleCreateIRLMoji)
	m.Post("/api/v1/irlmoji/id/:irlmojiId/heart.json", binding.Json(models.Heart{}), HandleToggleHeart)

	m.Post("/upload", HandleUpload)

	m.NotFound(HandleNotFound)

	m.Run()
}
コード例 #2
0
ファイル: app.go プロジェクト: hackzone/goshop
// Start server and connection to database
func (gs *Shop) Start() *Shop {
	// don't connect if is connected
	if gs.databaseSession == nil {
		gs.databaseSession = database.Connect("localhost", "heramodas")
	}

	// create martini instance
	gs.Server = martini.Classic()

	// gzip all requests
	gs.Server.Use(gzip.All())

	return gs
}
コード例 #3
0
ファイル: app.go プロジェクト: kelsieflynn/flynn-11
func main() {
	// Read the configuration file
	file, _ := os.Open("conf/app.json")
	decoder := json.NewDecoder(file)
	configuration := &Configuration{}
	decoder.Decode(&configuration)

	// Initialize background processing
	workers.Configure(map[string]string{
		// Location of the redis instance.
		"server": configuration.RedisURI,

		// The number of connections to keep open with Redis.
		"pool": configuration.RedisConn,

		// Unique process id for this instance of workers; helps with proper
		// recovery of inprogress jobs on a crash.
		"process": "1",
	})

	m := martini.Classic()
	m.Use(gzip.All())
	m.Use(render.Renderer())
	m.Use(martini.Static("assets"))
	m.Use(DB())

	m.Get("/", func(r render.Render) {
		r.HTML(200, "welcome", nil)
	})

	m.Post("/join", binding.Form(Account{}), func(account Account, r render.Render, db *mgo.Database) {
		u, err := uuid.NewV4()
		if err != nil {
			panic(err)
		}
		token := strings.Replace(u.String(), "-", "", -1)
		reflect.ValueOf(&account).Elem().FieldByName("EmailValidationToken").SetString(token)
		db.C("accounts").Insert(account)

		// Send the activation email

		r.HTML(200, "join_success", nil)
	})

	m.Get("/accounts/activate/:token", func(r render.Render) {
		r.HTML(200, "registration", nil)
	})

	/*
	  m.Post("/accounts/new", binding.Form(Account{}), func(account Account, r render.Render, db *mgo.Database) {
	    colQuerier := bson.M{"email": ""}
	    change := bson.M{"$set": bson.M{}}
	    err = db.C("accounts").Update(colQuerier, change)
	    if err != nil {
	      log.Fatal(err)
	    }
	  })*/

	// Handle 404.
	m.NotFound(func(r render.Render) {
		r.HTML(404, "notfound", nil)
	})

	s := &http.Server{
		Addr:           ":8080",
		Handler:        m,
		ReadTimeout:    10 * time.Second,
		WriteTimeout:   10 * time.Second,
		MaxHeaderBytes: 1 << 20,
	}
	log.Fatal(s.ListenAndServe())
}
コード例 #4
0
ファイル: wigo.go プロジェクト: root-gg/wigo
func threadHttp(config *wigo.HttpConfig) {
	apiAddress := config.Address
	apiPort := config.Port

	m := martini.New()

	if wigo.GetLocalWigo().GetConfig().Global.Debug {
		// Log requests
		m.Use(martini.Logger())
	}

	// Compress http responses with gzip
	if config.Gzip {
		log.Println("Http server : gzip compression enabled")
		m.Use(gzip.All())
	}

	// Add some basic security checks
	m.Use(secure.Secure(secure.Options{}))

	// Http basic auth
	if config.Login != "" && config.Password != "" {
		log.Println("Http server : basic auth enabled")
		m.Use(auth.Basic(config.Login, config.Password))
	}

	// Serve static files
	m.Use(martini.Static("public"))

	// Handle errors // TODO is this even working ?
	m.Use(martini.Recovery())

	// Define the routes.

	r := martini.NewRouter()

	r.Get("/api", func() (int, string) {
		json, err := wigo.GetLocalWigo().ToJsonString()
		if err != nil {
			return 500, fmt.Sprintf("%s", err)
		}
		return 200, json
	})

	r.Get("/api/status", func() string { return strconv.Itoa((wigo.GetLocalWigo().GlobalStatus)) })
	r.Get("/api/logs", wigo.HttpLogsHandler)
	r.Get("/api/logs/indexes", wigo.HttpLogsIndexesHandler)
	r.Get("/api/groups", wigo.HttpGroupsHandler)
	r.Get("/api/groups/:group", wigo.HttpGroupsHandler)
	r.Get("/api/groups/:group/logs", wigo.HttpLogsHandler)
	r.Get("/api/groups/:group/probes/:probe/logs", wigo.HttpLogsHandler)
	r.Get("/api/hosts", wigo.HttpRemotesHandler)
	r.Get("/api/hosts/:hostname", wigo.HttpRemotesHandler)
	r.Get("/api/hosts/:hostname/status", wigo.HttpRemotesStatusHandler)
	r.Get("/api/hosts/:hostname/logs", wigo.HttpLogsHandler)
	r.Get("/api/hosts/:hostname/probes", wigo.HttpRemotesProbesHandler)
	r.Get("/api/hosts/:hostname/probes/:probe", wigo.HttpRemotesProbesHandler)
	r.Get("/api/hosts/:hostname/probes/:probe/status", wigo.HttpRemotesProbesStatusHandler)
	r.Get("/api/hosts/:hostname/probes/:probe/logs", wigo.HttpLogsHandler)
	r.Get("/api/probes/:probe/logs", wigo.HttpLogsHandler)
	r.Get("/api/authority/hosts", wigo.HttpAuthorityListHandler)
	r.Post("/api/authority/hosts/:uuid/allow", wigo.HttpAuthorityAllowHandler)
	r.Post("/api/authority/hosts/:uuid/revoke", wigo.HttpAuthorityRevokeHandler)

	m.Use(func(c martini.Context, w http.ResponseWriter, r *http.Request) {
		if strings.HasPrefix(r.URL.Path, "/api") {
			w.Header().Set("Content-Type", "application/json")
		}
	})

	m.Action(r.Handle)

	// Create a listner and serv connections forever.
	if config.SslEnabled {
		address := apiAddress + ":" + strconv.Itoa(apiPort)
		log.Println("Http server : starting tls server @ " + address)
		tlsConfig := &tls.Config{MinVersion: tls.VersionTLS10}
		server := &http.Server{Addr: address, Handler: m, TLSConfig: tlsConfig}
		err := server.ListenAndServeTLS(config.SslCert, config.SslKey)
		if err != nil {
			log.Fatalf("Failed to start http server : %s", err)
		}
	} else {
		address := apiAddress + ":" + strconv.Itoa(apiPort)
		log.Println("Http server : starting plain http server @ " + address)
		if err := http.ListenAndServe(address, m); err != nil {
			log.Fatalf("Failed to start http server : %s", err)
		}
	}
}