Skip to content

Commit

Permalink
Added Socket IO Golang Project Boilerplate
Browse files Browse the repository at this point in the history
  • Loading branch information
jsdaniell committed Mar 22, 2021
1 parent a341060 commit 7e98669
Show file tree
Hide file tree
Showing 808 changed files with 1,116 additions and 126,323 deletions.
836 changes: 836 additions & 0 deletions .idea/workspace.xml

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions bin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ project
- CI CD Scripts for publish release pre-configured.
- Deploy and release with `git tag -a v1.0.0 -m "Alpha Release" && git push origin v1.0.0` (the version have to be the same of package.json)
- NPM deploy script configured, production-ready, publish with `npm publish`.
- Install GO CLI package with `npm install -g your-package`, update with `npm update -g your-package`.

```
project
Expand Down
18 changes: 12 additions & 6 deletions cmd/golang/golang.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,18 @@ import (
"github.com/jsdaniell/recipe-cli/cli"
"github.com/jsdaniell/recipe-cli/cmd/golang/golang_api"
"github.com/jsdaniell/recipe-cli/cmd/golang/golang_cli"
"github.com/jsdaniell/recipe-cli/cmd/golang/golang_socket_io"
"github.com/spf13/cobra"
"log"
)

const (
API string = "API"
CLI string = "CLI"
MongoDBDatabase = "MongoDB"
FirebaseDatabase = "Firebase"
NoSelection = "NoSelection"
API string = "API"
CLI string = "CLI"
SocketIo = "Socket IO"
MongoDBDatabase = "MongoDB"
FirebaseDatabase = "Firebase"
NoSelection = "NoSelection"
)

func validateProjectName(input string) error {
Expand All @@ -34,7 +36,7 @@ var GoCmd = &cobra.Command{
Short: "Choose the type of project:",
Long: `Different golang projects options for boilerplate.`,
Run: func(cmd *cobra.Command, args []string) {
projectType, err := cli.SelectorCli("Choose the type of golang project:", API, CLI)
projectType, err := cli.SelectorCli("Choose the type of golang project:", API, CLI, SocketIo)
if err != nil {
log.Fatal(err)
}
Expand All @@ -61,5 +63,9 @@ var GoCmd = &cobra.Command{
if projectType == CLI {
golang_cli.InitRoot(username, projectName)
}

if projectType == SocketIo {
golang_socket_io.InitRoot(username, projectName)
}
},
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func CreateOtherCommandPackage(username, projectName string) {
}

func writeOtherCommandFile(username, projectName string){
var content = `package golang
var content = `package other_commands
import (
"errors"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func ExecuteShellCommand(command string, projectName string, args ...string){
}
`

file, err := os.Create(projectName + "/utils/go_commands/shell_commands.go")
file, err := os.Create(projectName + "/utils/shell_commands/shell_commands.go")
if err != nil {
log.Fatal(err)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package golang_socket_content_files

import (
"log"
"os"
)

func CreateConfigPackage(projectName string) {
err := os.Mkdir(projectName + "/config", os.FileMode(0777))
if err != nil {
log.Fatal(err)
}

writeServerJSON(projectName)
writeServerSecurityJSON(projectName)
}

func writeServerJSON(projectName string){
var content = `{
"port": 9990,
"path": "/"
}
`

file, err := os.Create(projectName + "/config/server.json")
if err != nil {
log.Fatal(err)
}

_, err = file.WriteString(content)
if err != nil {
log.Fatal(err)
}
}

func writeServerSecurityJSON(projectName string){
var content = `{
"port": 9999,
"path": "/",
"secure": true,
"ssl-cert": "ssl.crt",
"ssl-key": "ssl.key"
}
`

file, err := os.Create(projectName + "/config/server-secure.json")
if err != nil {
log.Fatal(err)
}

_, err = file.WriteString(content)
if err != nil {
log.Fatal(err)
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package golang_socket_content_files

import (
"log"
"os"
)

func CreateControllersPackage(projectName string) {
err := os.Mkdir(projectName + "/controllers", os.FileMode(0777))
if err != nil {
log.Fatal(err)
}

writeEchoController(projectName)
}

func writeEchoController(projectName string){
var content = `package controllers
import "github.com/ambelovsky/gosf"
type EchoDetail struct {
OneThing string ` + "`" + `json:"oneThing,omitempty"` + "`" + `
AnotherThing struct {
MoreDetail string ` + "`" + `json:"moreDetail,omitempty"` + "`" + `
} ` + "`" + `json:"anotherThing,omitempty"` + "`" + `
}
type EchoRequestBody struct {
Description string ` + "`" + `json:"description,omitempty"` + "`" + `
}
// Echo returns the passed message back to the client
func Echo(client *gosf.Client, request *gosf.Request) *gosf.Message {
// Get request arguments and convert them to a predefined struct
requestBody := new(EchoRequestBody)
gosf.MapToStruct(request.Message.Body, requestBody)
responseText := ""
// If a detailed description was entered, send it back to the client
if requestBody.Description != "" {
responseText = " - " + requestBody.Description
}
echoDetail := &EchoDetail{
OneThing: "this is one thing",
}
echoDetail.AnotherThing.MoreDetail = "and another thing..."
return gosf.NewSuccessMessage(responseText, gosf.StructToMap(echoDetail))
}
`

file, err := os.Create(projectName + "/controllers/echo.go")
if err != nil {
log.Fatal(err)
}

_, err = file.WriteString(content)
if err != nil {
log.Fatal(err)
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package golang_socket_content_files

import (
"log"
"os"
)

func CreateMainFile(username, projectName string) {
var content = `package main
import (
"github.com/ambelovsky/gosf"
"github.com/`+ username +`/`+ projectName +`/router"
)
func init() {
router.RegisterRoutes() // Configure endpoint request handlers
// Load Config File Based on Environmental Configuration
if value, exist := gosf.App.Env["GOSF_ENV"]; exist && value != "dev" {
// Prod/Stage Config
gosf.LoadConfig("server", "server-secure.json")
} else {
// Default and "dev" config
gosf.LoadConfig("server", "server.json")
}
}
func main() {
// Start the server
serverConfig := gosf.App.Config["server"].(map[string]interface{})
gosf.Startup(serverConfig)
}`

file, err := os.Create(projectName + "/main.go")
if err != nil {
log.Fatal(err)
}

_, err = file.WriteString(content)
if err != nil {
log.Fatal(err)
}
}

func CreateHTMLFile(projectName string) {
var content = `<!DOCTYPE html>
<html>
<head>
<title>Socket</title>
</head>
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.2.0/socket.io.slim.js"></script>
<script>
var socket = io.connect('ws://localhost:9999', { transports: ['websocket'] });
socket.emit('echo', { text: 'Hello world.' }, function(response) {
console.log(response);
});
</script>
</body>
</html>`

file, err := os.Create(projectName + "/index.html")
if err != nil {
log.Fatal(err)
}

_, err = file.WriteString(content)
if err != nil {
log.Fatal(err)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package golang_socket_content_files

import (
"log"
"os"
)

func CreateRouterPackage(username, projectName string) {
err := os.Mkdir(projectName + "/router", os.FileMode(0777))
if err != nil {
log.Fatal(err)
}

writeRouterFile(username, projectName)
}

func writeRouterFile(username, projectName string){
var content = `package router
import (
"github.com/ambelovsky/gosf"
"github.com/`+ username +`/`+ projectName +`/controllers"
)
// RegisterRoutes maps controller functions to endpoints
func RegisterRoutes() {
gosf.Listen("echo", controllers.Echo)
}
`

file, err := os.Create(projectName + "/router/router.go")
if err != nil {
log.Fatal(err)
}

_, err = file.WriteString(content)
if err != nil {
log.Fatal(err)
}
}

28 changes: 28 additions & 0 deletions cmd/golang/golang_socket_io/init_root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package golang_socket_io

import (
"github.com/jsdaniell/recipe-cli/cmd/golang/golang_socket_io/golang_socket_content_files"
"github.com/jsdaniell/recipe-cli/utils/go_commands"
"log"
"os"
)

func InitRoot(username, projectName string){
err := os.Mkdir(projectName, os.FileMode(0777))
if err != nil {
log.Fatal(err)
}

golang_socket_content_files.CreateMainFile(username, projectName)

go_commands.GoModInit(username, projectName)
go_commands.GoGet("github.com/ambelovsky/gosf", projectName)

golang_socket_content_files.CreateHTMLFile(projectName)
golang_socket_content_files.CreateRouterPackage(username, projectName)
golang_socket_content_files.CreateControllersPackage(projectName)
golang_socket_content_files.CreateConfigPackage(projectName)

go_commands.GoModTidy(projectName)
go_commands.GoModVendor(projectName)
}
1 change: 0 additions & 1 deletion node_modules/.bin/go-npm

This file was deleted.

1 change: 0 additions & 1 deletion node_modules/.bin/mkdirp

This file was deleted.

1 change: 0 additions & 1 deletion node_modules/.bin/rimraf

This file was deleted.

1 change: 0 additions & 1 deletion node_modules/.bin/sshpk-conv

This file was deleted.

1 change: 0 additions & 1 deletion node_modules/.bin/sshpk-sign

This file was deleted.

1 change: 0 additions & 1 deletion node_modules/.bin/sshpk-verify

This file was deleted.

1 change: 0 additions & 1 deletion node_modules/.bin/uuid

This file was deleted.

20 changes: 0 additions & 20 deletions node_modules/ajv/.tonic_example.js

This file was deleted.

Loading

0 comments on commit 7e98669

Please sign in to comment.