Example #1
1
func main() {
	r := gin.Default()

	// if Allow DirectoryIndex
	r.Use(static.Serve("/request/", static.LocalFile("./app", true)))
	r.Use(static.Serve("/todo/", static.LocalFile("./todo", true)))

	// set prefix
	r.Use(static.Serve("/static", static.LocalFile("./app", true)))
	r.Use(static.Serve("/static", static.LocalFile("./todo", true)))

	r.GET("/ping", func(c *gin.Context) {
		c.String(200, "test")
	})
	r.GET("/index", index, index)

	// Setup Socket.io server and related activity fetching
	socketServer, err := SetupSocketIO()
	if err != nil {
		logging.ErrorWithTags([]string{"socket.io"}, "Error on socket.io server", err.Error())

	}

	r.GET("/socket.io/", func(c *gin.Context) {
		socketServer.ServeHTTP(c.Writer, c.Request)
	})
	// redis configuration
	flag.Parse()

	// Listen and Server in 0.0.0.0:8080
	r.Run(":8080")

}
Example #2
0
func ValidateConditional(c string, p ActivityPayload) bool {
	if c == "" {
		return true
	}

	buf := new(bytes.Buffer)

	tmpl, err := template.New("condition").Parse(fmt.Sprintf("{{%s}}", c))
	if err != nil {
		logging.ErrorWithTags([]string{"condition"}, "Failed to parse template condition", err.Error())
	}

	err = tmpl.Execute(buf, p)
	if err != nil {
		logging.ErrorWithTags([]string{"condition"}, "Failed to execute template condition", err.Error())
	}

	resultStr := buf.String()
	result, err := strconv.ParseBool(resultStr)
	if err != nil {
		logging.ErrorWithTags([]string{"condition"}, "Failed to parse bool from condition return", err.Error())
	}

	return result
}
Example #3
0
func setupDb() *gorp.DbMap {
	// connect to db using standard Go database/sql API
	// use whatever database/sql driver you wish
	db, err := sql.Open("sqlite3", "./feedbag.bin")
	if err != nil {
		logging.ErrorWithTags([]string{"sqlite", "db"}, "Failed to open sqlite database", err.Error())
	}

	// construct a gorp DbMap
	dbmap := &gorp.DbMap{Db: db, Dialect: gorp.SqliteDialect{}}

	dbmap.TypeConverter = TypeConverter{}

	// add a table, setting the table name to 'posts' and
	// specifying that the Id property is an auto incrementing PK
	dbmap.AddTableWithName(User{}, "users").SetKeys(true, "Id")
	dbmap.AddTableWithName(Activity{}, "activities").SetKeys(true, "Id")
	dbmap.AddTableWithName(ActivityPayload{}, "events").SetKeys(true, "Id").ColMap("GithubId").SetUnique(true)

	// create the table. in a production system you'd generally
	// use a migration tool, or create the tables via scripts
	err = dbmap.CreateTablesIfNotExists()
	if err != nil {
		logging.ErrorWithTags([]string{"sql"}, "Create tables failed", err.Error())
	}

	return dbmap
}
Example #4
0
func providerCallback(c *gin.Context) {
	// Run user auth using the gothic library
	user, err := gothic.CompleteUserAuth(c.Writer, c.Request)
	if err != nil {
		logging.ErrorWithTags([]string{"github", "api"}, "Failed to create user from callback", err.Error())
	}

	u := User{}

	err = u.GetByUsername(user.RawData["login"].(string))
	if err != nil {
		if err != sql.ErrNoRows {
			logging.ErrorWithTags([]string{"api"}, "Failed to read from user table", err.Error())
			return
		}
	}

	//Add user to the user table
	u.Name = user.Name
	u.Username = user.RawData["login"].(string)
	u.AvatarUrl = user.AvatarURL
	u.AccessToken = user.AccessToken
	u.ProfileUrl = user.RawData["url"].(string)
	u.Email = user.Email
	u.Joined = user.RawData["created_at"].(string)
	u.Raw = user.RawData

	if u.Id != 0 {
		u.UpdateTime()
		_, err = dbmap.Update(&u)
		if err != nil {
			logging.ErrorWithTags([]string{"db"}, "Failed to update user row", err.Error())
		}
	} else {
		err = u.Create()
		if err != nil {
			logging.ErrorWithTags([]string{"db"}, "Failed to create new user row", err.Error())
		}

		//Add the user's go routine
		StartUserRoutine(u, activityChan)
	}

	c.JSON(200, u)
}
Example #5
0
func templateHandler(c *gin.Context) {
	var err error
	Templates, err = tmpl.ParseDir(TemplatesDir)
	if err != nil {
		logging.ErrorWithTags([]string{"templates"}, "Failed to load templates.", err.Error())
	}

	c.JSON(200, Templates)
}
Example #6
0
func main() {
	// Configure port for server to run on
	if len(os.Getenv("PORT")) > 0 {
		port = os.Getenv("PORT")
	}

	err := feedbag.Start(port, templatesDir, publicDir)
	if err != nil {
		logging.ErrorWithTags([]string{"app"}, "Feedbag failed to start:", err)
	}
}
Example #7
0
func Start(port, templatesDir string, publicDir string) error {
	dbmap = setupDb()
	defer dbmap.Db.Close()

	// Process our templates
	TemplatesDir = templatesDir
	var err error
	Templates, err = tmpl.ParseDir(TemplatesDir)
	if err != nil {
		logging.ErrorWithTags([]string{"templates"}, "Failed to parse templates", err.Error())
		return err
	}

	// Setup Goth Authentication
	goth.UseProviders(
		github.New(os.Getenv("GITHUB_KEY"), os.Getenv("GITHUB_SECRET"), "http://localhost:3000/auth/github/callback", "repo", "user:email"),
	)

	// Setup Socket.io server and related activity fetching
	socketServer, err := SetupSocketIO()
	if err != nil {
		return err
	}

	err = StartSocketPusher(socketServer, activityChan)
	if err != nil {
		return err
	}

	err = StartExistingUsers(activityChan)
	if err != nil {
		return err
	}

	// Start up gin and its friends
	r := gin.Default()
	r.Use(cors.Middleware(cors.Options{AllowCredentials: true}))

	// Serve static assets
	r.Use(static.Serve("/", static.LocalFile(publicDir, false)))

	SetupRoutes(r, socketServer)
	r.Run(fmt.Sprintf(":%s", port))

	return nil
}
Example #8
0
func SetupSocketIO() (*socketio.Server, error) {
	server, err := socketio.NewServer(nil)
	if err != nil {
		return nil, err
	}

	server.On("connection", func(so socketio.Socket) {
		logging.InfoWithTags([]string{"socket.io"}, "New socket.io connection:", so.Id())
		so.Join("feedbag")
		so.On("disconnection", func() {
			// no op
		})
	})
	server.On("error", func(so socketio.Socket, err error) {
		logging.ErrorWithTags([]string{"socket.io"}, "Error on socket.io server", err.Error())
	})

	return server, nil
}