diff --git a/socialdragon/main.go b/socialdragon/main.go index 55df12c..8a37570 100644 --- a/socialdragon/main.go +++ b/socialdragon/main.go @@ -1,15 +1,12 @@ package main import ( - "io" "os" "os/signal" "syscall" - "golang.org/x/net/websocket" - + "github.com/gin-gonic/gin" "github.com/robfig/cron" - "gopkg.in/gin-gonic/gin.v1" "git.1750studios.com/AniNite/SocialDragon/config" "git.1750studios.com/AniNite/SocialDragon/database" @@ -25,13 +22,15 @@ func main() { c := cron.New() c.AddFunc("@every 30s", snapchat.LoadNewSnaps) c.AddFunc("@every 30s", instagram.LoadNewInstas) + c.AddFunc("@every 5s", sendNewPicture) c.Start() go twitter.LoadNewTweets() + go setupGin() ch := make(chan os.Signal) signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM) - go setupGin() + <-ch twitter.Stop() @@ -39,23 +38,17 @@ func main() { 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") -} + router.GET("/", renderHomepage) -func renderHomepage(ctx *gin.Context) { - ctx.HTML(200, "index.html", gin.H{"title": "Main website"}) -} + router.GET("/admin", func(c *gin.Context) { c.Redirect(301, "/admin/inbox") }) + router.GET("/admin/inbox", renderAdminInbox) + router.GET("/admin/approved", renderAdminApproved) + router.GET("/admin/rejected", renderAdminRejected) -func websocketEndpoint(ctx *gin.Context) { - handler := websocket.Handler(echoHandler) - handler.ServeHTTP(ctx.Writer, ctx.Request) -} + router.GET("/ws", func(c *gin.Context) { wsHandler(c.Writer, c.Request) }) -func echoHandler(ws *websocket.Conn) { - websocket.Message.Send(ws, "Test Message from Server") - io.Copy(ws, ws) + router.LoadHTMLGlob(config.C.TemplatesDirectory + "/*") + router.Static("/static", config.C.AssetsDirectory) + router.Static(config.C.ContentWebDirectory, config.C.ContentDirectory) + router.Run(config.C.BindAddress) } diff --git a/socialdragon/socket.go b/socialdragon/socket.go new file mode 100644 index 0000000..7642b18 --- /dev/null +++ b/socialdragon/socket.go @@ -0,0 +1,54 @@ +package main + +import ( + "log" + "net/http" + + "git.1750studios.com/AniNite/SocialDragon/database" + + "github.com/gorilla/websocket" +) + +var wsupgrader = websocket.Upgrader{ + ReadBufferSize: 1024, + WriteBufferSize: 1024, + CheckOrigin: func(r *http.Request) bool { + return true + }, +} +var sockets []*websocket.Conn +var lastID uint + +func wsHandler(w http.ResponseWriter, r *http.Request) { + conn, err := wsupgrader.Upgrade(w, r, nil) + if err != nil { + log.Printf("Failed to set websocket upgrade: %+v", err) + return + } + pos := len(sockets) + sockets = append(sockets, conn) + for { + _, _, err := conn.ReadMessage() + if err != nil { + break + } + } + sockets = append(sockets[:pos], sockets[pos+1:]...) +} + +func sendNewPicture() { + var ITs []database.Item + database.Db.Offset(lastID).Limit(1).Find(&ITs) + for i, socket := range sockets { + for _, IT := range ITs { + err := socket.WriteJSON(IT) + if IT.ID > lastID { + lastID = IT.ID + } + if err != nil { + sockets = append(sockets[:i], sockets[i+1:]...) + continue + } + } + } +} diff --git a/socialdragon/templates/admin-inbox.html b/socialdragon/templates/admin-inbox.html new file mode 100644 index 0000000..bba2c77 --- /dev/null +++ b/socialdragon/templates/admin-inbox.html @@ -0,0 +1,46 @@ +{{ template "header.html" . }} +
+
+

{{ .title }}

+
+ {{ range .its }} +
+ {{ if .IsVideo }} + + Video + + {{ else }} + + + + {{ end }} +
+
+
+
+ {{ if .IsVideo }} + + {{ else }} + + {{ end }} +
+
+ Approve
+ Reject +
+
+ +
+ {{ end }} +
+
+
+ +{{ template "footer.html" . }} diff --git a/socialdragon/templates/admin.html b/socialdragon/templates/admin.html deleted file mode 100644 index d9e798d..0000000 --- a/socialdragon/templates/admin.html +++ /dev/null @@ -1,65 +0,0 @@ - - - - - - - Snapchat Wall Admin - - - - - - -
-
-

Snapchat Wall · Admin

- -
-
- -
-
-
-

Click on snap to review:

- -
-
-
- - - - - - - diff --git a/socialdragon/templates/footer.html b/socialdragon/templates/footer.html new file mode 100644 index 0000000..132da47 --- /dev/null +++ b/socialdragon/templates/footer.html @@ -0,0 +1,6 @@ + + + + + + diff --git a/socialdragon/templates/header.html b/socialdragon/templates/header.html new file mode 100644 index 0000000..15db20d --- /dev/null +++ b/socialdragon/templates/header.html @@ -0,0 +1,21 @@ + + + + + + + {{ .title }} + + + + +
+
+ +
+
diff --git a/socialdragon/templates/index.html b/socialdragon/templates/index.html index 4490df6..817fed1 100755 --- a/socialdragon/templates/index.html +++ b/socialdragon/templates/index.html @@ -2,7 +2,7 @@ - Snapchat Wall + {{.title}}