Skip to content

Commit

Permalink
Simpler functions not relying on paths to init.
Browse files Browse the repository at this point in the history
  • Loading branch information
davidrenne committed Aug 13, 2022
1 parent 5da2add commit c6990e6
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 84 deletions.
9 changes: 9 additions & 0 deletions core/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/DanielRenne/GoCore/core/ginServer"
"github.com/DanielRenne/GoCore/core/gitWebHooks"
"github.com/DanielRenne/GoCore/core/logger"
"github.com/DanielRenne/GoCore/core/path"
"github.com/DanielRenne/GoCore/core/serverSettings"
"github.com/DanielRenne/GoCore/core/store"
"github.com/gin-gonic/gin"
Expand Down Expand Up @@ -175,6 +176,14 @@ func init() {
BroadcastSockets = true
}

func Init() (err error) {
return Initialize(path.GetBinaryPath(), "webConfig.json")
}

func InitCustomWebConfig(webConfig string) {
Initialize(path.GetBinaryPath(), webConfig)
}

func Initialize(path string, config string) (err error) {
err = serverSettings.Initialize(path, config)
if err != nil {
Expand Down
35 changes: 35 additions & 0 deletions core/path/path.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package path

import (
"log"
"os"
"path/filepath"
"strings"
)

//GetBinaryPath will return the location of the binary or the project in go run mode.
func GetBinaryPath() (path string) {
ex, err := os.Executable()
if err != nil {
log.Println("path.GetBinaryPath() failed to get Executable: " + err.Error())
return
}
exPath := filepath.Dir(ex)
if strings.Contains(exPath, "go-build") { //This means we are running in go run and need to use the goPath
if len(os.Args) >= 2 {
path = os.Args[1] + PathSeparator
} else {
log.Println("Using GoCore in go run, you must pass go run yourMain.go $(pwd) so it can get your path of your project")
os.Exit(1)
}
} else {
if strings.Index(exPath, "Contents/MacOS") != -1 {
dirs := strings.Split(exPath, "/")
dirs = dirs[:len(dirs)-3]
path = strings.Join(dirs, "/") + PathSeparator
} else {
path = exPath + PathSeparator
}
}
return
}
7 changes: 7 additions & 0 deletions core/path/pathUnix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//go:build !windows
// +build !windows

package path

//PathSeparator provides windows or non-windows path separator.
const PathSeparator = "/"
6 changes: 6 additions & 0 deletions core/path/pathWindows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
//go:build windows
// +build windows

package path

const PathSeparator = "\\"
48 changes: 31 additions & 17 deletions core/serverSettings/serverSettings.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,19 @@ import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"sync"

"github.com/DanielRenne/GoCore/core/path"
)

type htmlTemplates struct {
type HtmlTemplates struct {
Enabled bool `json:"enabled"`
Directory string `json:"directory"`
DirectoryLevels int `json:"directoryLevels"`
}

type dbConnection struct {
type DbConnection struct {
ConnectionString string `json:"connectionString"`
EnableTLS bool `json:"enableTLS"`
Driver string `json:"driver"`
Expand All @@ -29,23 +32,23 @@ type dbConnection struct {
} `json:"replication"`
}

type license struct {
type License struct {
Name string `json:"name"`
URL string `json:"url"`
}

type contact struct {
type Contact struct {
Name string `json:"name"`
URL string `json:"url"`
Email string `json:"email"`
}

type info struct {
type Info struct {
Title string `json:"title"`
Description string `json:"description"`
Contact contact `json:"contact"`
License license `json:"license"`
TermsOfService string `json:termsOfService"`
Contact Contact `json:"contact"`
License License `json:"license"`
TermsOfService string `json:"termsOfService"`
}

type Application struct {
Expand All @@ -64,8 +67,8 @@ type Application struct {
GitWebHookSecretKey string `json:"gitWebHookSecretKey"`
GitWebHookPort string `json:"gitWebHookServerPort"`
GitWebHookPath string `json:"gitWebHookPath"`
Info info `json:"info"`
HtmlTemplates htmlTemplates `json:"htmlTemplates"`
Info Info `json:"info"`
HtmlTemplates HtmlTemplates `json:"htmlTemplates"`
RootIndexPath string `json:"rootIndexPath"`
DisableRootIndex bool `json:"disableRootIndex"`
CustomGinLogger bool `json:"customGinLogger"`
Expand All @@ -84,34 +87,45 @@ type Application struct {
AllowCrossOriginRequests bool `json:"allowCrossOriginRequests"`
}

type webConfigObj struct {
DbConnections []dbConnection `json:"dbConnections"`
type WebConfigType struct {
DbConnections []DbConnection `json:"dbConnections"`
Application Application `json:"application"`
DbConnection dbConnection
DbConnection DbConnection
}

var WebConfig webConfigObj
var WebConfig WebConfigType
var WebConfigMutex sync.RWMutex
var APP_LOCATION string
var SWAGGER_UI_PATH string

func Initialize(path string, configurationFile string) (err error) {
func Init() {
Initialize(path.GetBinaryPath(), "webConfig.json")
}

func InitCustomWebConfig(webConfig string) {
Initialize(path.GetBinaryPath(), webConfig)
}

func InitPath(path string) {
APP_LOCATION = path
SWAGGER_UI_PATH = APP_LOCATION + "/web/swagger/dist"
}

func Initialize(path string, configurationFile string) (err error) {
InitPath(path)
fmt.Println("core serverSettings initialized.")

jsonData, err := ioutil.ReadFile(APP_LOCATION + "/" + configurationFile)
if err != nil {
fmt.Println("Reading of webConfig.json failed: " + err.Error())
return
os.Exit(1)
}

WebConfigMutex.Lock()
errUnmarshal := json.Unmarshal(jsonData, &WebConfig)
if errUnmarshal != nil {
fmt.Println("Parsing / Unmarshaling of webConfig.json failed: " + errUnmarshal.Error())
return
os.Exit(1)
}

for _, dbConnection := range WebConfig.DbConnections {
Expand Down
92 changes: 25 additions & 67 deletions doc/Application_Settings.md
Original file line number Diff line number Diff line change
@@ -1,51 +1,36 @@
#GoCore Application Settings
# GoCore Application Settings

There are 2 components to GoCore which must be configured within your application:
## app package

##buildCore
The GoCore/core/app package is what runs your application. You must first Init() it with the root path of your application. Then call the Run() method which will block on the HTTP server initialization.

Create a build package for your application with the following:

package main

import (
"github.com/DanielRenne/GoCore/buildCore"
)

func main() {
buildCore.Initialize("src/github.com/DanielRenne/GoCoreHelloWorld")
}

##app

The GoCore/core/app package is what runs your application. You must first Initialize() it with the root path of your application. Then call the Run() method.
Please note, that if you use go run, you must call `go run main.go $(pwd)` because GoCore needs to know the directory of your project to read the webConfig.json file and associated paths for things like models, keys, db/bootstrap etc. You can also call app.InitCustomWebConfig() and pass a custom file name for webConfig.json based on your own logic.

package main

import (
"github.com/DanielRenne/GoCore/core/app"
_ "github.com/DanielRenne/GoCoreHelloWorld/webAPIs/v1/webAPI"
)

func main() {
//Run First.
app.Initialize("src/github.com/DanielRenne/GoCoreHelloWorld")
app.Init()

//Add your Application Code here.

//Run Last.
app.Run()
}

#App Settings
# App Settings

GoCore reads a file located in the root directory of your package called WebConfig.json If one does not exist `buildCore` will auto generate one for your package with default settings.
GoCore reads a file located in the root directory of your package called webConfig.json If one does not exist `buildCore` will auto generate one for your package with default settings.

##WebConfig.json
## WebConfig.json

There are two root objects to be configured:

###application
### application



Expand Down Expand Up @@ -78,35 +63,35 @@ There are two root objects to be configured:

At the root of application there are the following fields:

####domain
#### domain

Tells the application which domain to redirect https traffic to.

####serverFQDN
#### serverFQDN

Currently only used for bootstrap purposes to compare domainName where you want your data inserted

####logGophers
#### logGophers

Instead of calling go func on anonymous functions. Use logger.GoRoutineLogger() and pass the func with a description. Then setup logGophers to true in your web config to log the time in which a goroutine starts and potentially exits.

####releaseMode
#### releaseMode

Tells the application to debug and run GIN http routing into release mode. "release" will enable release. An empty string will place the application in debug mode.

####webServiceOnly
#### webServiceOnly

Tells the application only route web service paths. NO static file routing will be enabled when set to true.

####customGinLogger
#### customGinLogger

If you plan to write and .Use a custom gin logger in your AppIndex, set to true. Otherwise the default of false will use the default logger and recovery handler.

####productName
#### productName

A short name (usually not human with spaces). Can be used to control which bootstrap information to seed based on the webConfig.json

####versionNumeric
#### versionNumeric

This is used primarily for bootstrapping data with the BootstrapMeta struct to tag your seeds with how you want them to run

Expand All @@ -120,24 +105,24 @@ type BootstrapMeta struct {
}
```

####versionDot
#### versionDot

Useful to show the users a dot-based version

####info
#### info

Tells the application details about the application for swagger.io information and schema.

####htmlTemplates
#### htmlTemplates

Tells the application to use HTML templates that conform to the GIN Engine. See [HTML Rendering in GIN](https://github.com/gin-gonic/gin#html-rendering]). See [HTML Templates](https://github.com/DanielRenne/GoCore/blob/master/doc/HTML_Templates.md) for more details and examples.

####htmlTemplates
#### htmlTemplates

Tells the application to use HTML templates that conform to the GIN Engine. See [HTML Rendering in GIN](https://github.com/gin-gonic/gin#html-rendering]). See [HTML Templates](https://github.com/DanielRenne/GoCore/blob/master/doc/HTML_Templates.md) for more details and examples.


###dbConnections
### dbConnections

Provides an array of database connections. Currently GoCore only supports a single database connection. Future releases will allow for multiple connections and types.

Expand All @@ -147,9 +132,9 @@ Provides an array of database connections. Currently GoCore only supports a sin
"connectionString" : "db/helloWorld.db"
}
]
###Database Connection Examples
### Database Connection Examples

###Bolt DB
### Bolt DB

A NOSQL GOLang native database that runs within your application

Expand All @@ -158,38 +143,11 @@ A NOSQL GOLang native database that runs within your application
"connectionString" : "db/helloWorld.db"
}

###Mongo DB
### Mongo DB

A NOSQL database that runs outside your application

{
"driver" : "mongoDB",
"connectionString" : "mongodb://myuser:mypass@localhost:40001,otherhost:40001/mydb"
}

###SQLite3

A SQL Database instance running within your application

{
"driver" : "sqlite3",
"connectionString" : "db/helloWorld.db"
}

###MYSQL

A SQL Database instance running external to your application

{
"driver" : "mysql",
"connectionString" : " myUsername:myPassword@/HelloWorld"
}

###MS SQL Server

A SQL Database instance running external to your application

{
"driver" : "mssql",
"connectionString" : "server=myServerAddress;Database=HelloWorld;user id=myUsername;Password=myPassword;Connection Timeout=3000;"
}

0 comments on commit c6990e6

Please sign in to comment.