Skip to content

Commit

Permalink
add some basic functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
Clivern committed Oct 28, 2018
1 parent 272d25a commit b18f90f
Show file tree
Hide file tree
Showing 32 changed files with 494 additions and 223 deletions.
18 changes: 9 additions & 9 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@

[[constraint]]
branch = "master"
name = "github.com/streadway/amqp"
name = "github.com/gorilla/websocket"

[[constraint]]
branch = "master"
name = "github.com/gorilla/websocket"
name = "github.com/satori/go.uuid"
26 changes: 21 additions & 5 deletions beaver.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,23 @@ import (
"os"

"github.com/clivern/beaver/internal/app/controller"
"github.com/clivern/beaver/internal/pkg/broadcast"
"github.com/clivern/beaver/internal/pkg/pusher"
"github.com/clivern/beaver/internal/pkg/utils"

"github.com/gin-gonic/gin"
)

func main() {

utils.PrintBanner()

// Load config.json file and store on env
config := &utils.Config{}
config.Load("config.dist.json")
// This will never override ENV Vars if exists
config.Cache()
config.GinEnv()

if os.Getenv("AppMode") == "prod" {
gin.SetMode(gin.ReleaseMode)
gin.DisableConsoleColor()
Expand All @@ -31,13 +42,18 @@ func main() {
r.GET("/favicon.ico", func(c *gin.Context) {
c.String(http.StatusNoContent, "")
})
r.GET("/chat", controller.Chat)

socket := &broadcast.Websocket{}
r.POST("/apps/:app_id/events", controller.Events)
r.GET("/apps/:app_id/channels", controller.Channels)
r.GET("/apps/:app_id/channels/:channel_name", controller.Channel)
r.GET("/apps/:app_id/channels/:channel_name/users", controller.ChannelUsers)

socket := &pusher.Websocket{}
socket.Init()
r.GET("/ws", func(c *gin.Context) {
socket.HandleConnections(c.Writer, c.Request, c.DefaultQuery("channel", ""))
r.GET("/app/:key", func(c *gin.Context) {
socket.HandleConnections(c.Writer, c.Request, c.Param("key"))
})

go socket.HandleMessages()

r.Run()
Expand Down
12 changes: 12 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"app_mode": "dev",
"app_port": "8080",
"app_log_level": "info",
"app_domain": "example.com",
"mysql_username": "root",
"mysql_password": "root",
"mysql_protocol": "tcp",
"mysql_host": "localhost",
"mysql_port": "3306",
"mysql_database": "beaver"
}
21 changes: 21 additions & 0 deletions internal/app/controller/channel.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright 2018 Clivern. All rights reserved.
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.

package controller

import (
"github.com/gin-gonic/gin"
)

// Channel
func Channel(c *gin.Context) {
appID := c.Param("app_id")
channelName := c.Param("channel_name")

c.JSON(200, gin.H{
"status": "ok",
"appID": appID,
"channelName": channelName,
})
}
21 changes: 21 additions & 0 deletions internal/app/controller/channel_users.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright 2018 Clivern. All rights reserved.
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.

package controller

import (
"github.com/gin-gonic/gin"
)

// ChannelUsers
func ChannelUsers(c *gin.Context) {
appID := c.Param("app_id")
channelName := c.Param("channel_name")

c.JSON(200, gin.H{
"status": "ok",
"appID": appID,
"channelName": channelName,
})
}
19 changes: 19 additions & 0 deletions internal/app/controller/channels.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright 2018 Clivern. All rights reserved.
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.

package controller

import (
"github.com/gin-gonic/gin"
)

// Channels
func Channels(c *gin.Context) {
appID := c.Param("app_id")

c.JSON(200, gin.H{
"status": "ok",
"appID": appID,
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ package controller

import (
"github.com/gin-gonic/gin"
"net/http"
)

// Chat controller
func Chat(c *gin.Context) {
c.HTML(http.StatusOK, "chat.tmpl", gin.H{
"title": "Beaver",
// Events
func Events(c *gin.Context) {
appID := c.Param("app_id")

c.JSON(200, gin.H{
"status": "ok",
"appID": appID,
})
}
53 changes: 53 additions & 0 deletions internal/app/controller/websocket.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright 2018 Clivern. All rights reserved.
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.

package controller

import (
"github.com/gin-gonic/gin"

"fmt"
"github.com/gorilla/websocket"
_ "log"
"net/http"
)

var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
CheckOrigin: func(_ *http.Request) bool {
return true
},
}

// Websocket
func Websocket(c *gin.Context) {
//key := c.Param("key")

conn, err := upgrader.Upgrade(c.Writer, c.Request, nil)
defer func() {
if conn != nil {
conn.Close()
}
}()

if err != nil {
fmt.Println("1")
fmt.Println(err.Error())
return
}

for {
_, message, err := conn.ReadMessage()

if err != nil {
fmt.Println("2")
fmt.Println(err.Error())
break
}

conn.WriteJSON(fmt.Sprintf(`{"item":"%s"}`, message))
fmt.Println(string(message))
}
}
Empty file.
Empty file.
Empty file.
Empty file.
Empty file added internal/model/.gitkeep
Empty file.
13 changes: 0 additions & 13 deletions internal/pkg/database/mysql.go

This file was deleted.

47 changes: 47 additions & 0 deletions internal/pkg/driver/mysql.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright 2018 Clivern. All rights reserved.
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.

package driver

import (
"database/sql"
"fmt"
"github.com/clivern/beaver/internal/pkg/logger"
_ "github.com/go-sql-driver/mysql"
)

// MySQL
type MySQL struct {
Username string
Password string
Host string
Port int
Database string
Protocol string
}

// Ping check the db connection
func (e *MySQL) Ping() bool {

db, err := sql.Open(
"mysql",
fmt.Sprintf("%s:%s@%s(%s:%d)/%s", e.Username, e.Password, e.Protocol, e.Host, e.Port, e.Database),
)

if err != nil {
logger.Errorf("Error connecting to DB: %s", err.Error())
return false
}

defer db.Close()

err = db.Ping()

if err != nil {
logger.Errorf("Error while checking DB connection: %s", err.Error())
return false
}

return true
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.

package provider
package driver

import (
_ "github.com/go-redis/redis"
)

// Redis provider
// Redis
type Redis struct {
}
Loading

0 comments on commit b18f90f

Please sign in to comment.