SocialDragon/socialdragon/main.go

62 lines
1.3 KiB
Go
Raw Normal View History

package main
import (
"io"
"os"
"os/signal"
"syscall"
"golang.org/x/net/websocket"
"github.com/robfig/cron"
"gopkg.in/gin-gonic/gin.v1"
"git.1750studios.com/AniNite/SocialDragon/config"
"git.1750studios.com/AniNite/SocialDragon/database"
"git.1750studios.com/AniNite/SocialDragon/instagram"
"git.1750studios.com/AniNite/SocialDragon/snapchat"
"git.1750studios.com/AniNite/SocialDragon/twitter"
)
func main() {
config.LoadConfig(os.Getenv("HOME") + "/.socialdragon.toml")
database.InitDb()
c := cron.New()
c.AddFunc("@every 30s", snapchat.LoadNewSnaps)
c.AddFunc("@every 30s", instagram.LoadNewInstas)
c.Start()
go twitter.LoadNewTweets()
ch := make(chan os.Signal)
signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM)
go setupGin()
<-ch
twitter.Stop()
}
func setupGin() {
router := gin.Default()
router.GET("/home", renderHomepage)
router.GET("/ws", websocketEndpoint)
router.LoadHTMLGlob("templates/*")
router.Static("/static", "assets")
router.Run("127.0.0.1:8000")
}
func renderHomepage(ctx *gin.Context) {
ctx.HTML(200, "index.html", gin.H{"title": "Main website"})
}
func websocketEndpoint(ctx *gin.Context) {
handler := websocket.Handler(echoHandler)
handler.ServeHTTP(ctx.Writer, ctx.Request)
}
func echoHandler(ws *websocket.Conn) {
websocket.Message.Send(ws, "Test Message from Server")
io.Copy(ws, ws)
}