-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Socket IO Golang Project Boilerplate
- Loading branch information
Showing
808 changed files
with
1,116 additions
and
126,323 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
cmd/golang/golang_socket_io/golang_socket_content_files/config_files.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
|
65 changes: 65 additions & 0 deletions
65
cmd/golang/golang_socket_io/golang_socket_content_files/controllers_files.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
|
74 changes: 74 additions & 0 deletions
74
cmd/golang/golang_socket_io/golang_socket_content_files/root_files.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
cmd/golang/golang_socket_io/golang_socket_content_files/router_files.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.