feature: Upgrade documentation, add godotenv to read .env file, add CRSF token to frontend
This commit is contained in:
@@ -5,12 +5,16 @@ A simple one binary orchestrator for game server hosting, written in Go with a s
|
||||
- [Orchestra](#orchestra)
|
||||
- [Table of Contents](#table-of-contents)
|
||||
- [Project Synopsys](#project-synopsys)
|
||||
- [ENV Variables](#env-variables)
|
||||
- [ENV Naming Breakdown](#env-naming-breakdown)
|
||||
- [ENV Settings](#env-settings)
|
||||
- [User Stories (Functionalities)](#user-stories-functionalities)
|
||||
- [User](#user)
|
||||
- [Admin](#admin)
|
||||
- [Service owner](#service-owner)
|
||||
- [Other](#other)
|
||||
- [Q \& A](#q--a)
|
||||
- [European Made](#european-made)
|
||||
|
||||
## Project Synopsys
|
||||
|
||||
@@ -24,6 +28,34 @@ TODO: Write more.
|
||||
|
||||
INFO: Code organization: https://go.dev/doc/modules/layout
|
||||
|
||||
## ENV Variables
|
||||
|
||||
### ENV Naming Breakdown
|
||||
|
||||
To ensure all env variables have a consistent naming scheme, I enforce the following for Orchestra:
|
||||
|
||||
```env
|
||||
ORCHESTRA_{API|WEB}_{ENV_VAR}
|
||||
```
|
||||
|
||||
### ENV Settings
|
||||
|
||||
```env
|
||||
ORCHESTRA_API_PORT=9810
|
||||
|
||||
ORCHESTRA_API_OAUTH_ENABLED=bool
|
||||
ORCHESTRA_API_OAUTH_SECRET=string
|
||||
ORCHESTRA_API_OAUTH_AUTH_URL=string
|
||||
ORCHESTRA_API_OAUTH_TOKEN_URL=string
|
||||
ORCHESTRA_API_OAUTH_CLIENT_ID=string
|
||||
ORCHESTRA_API_OAUTH_REDIRECT_URL=string
|
||||
|
||||
ORCHESTRA_API_LOCAL_USERS_ENABLED=bool
|
||||
ORCHESTRA_API_LOCAL_USERS_REGISTRATION=bool
|
||||
|
||||
ORCHESTRA_WEB_PORT=9800
|
||||
```
|
||||
|
||||
## User Stories (Functionalities)
|
||||
|
||||
All marked user stories are implemented, those that are not are WIP.
|
||||
@@ -70,3 +102,7 @@ All marked user stories are implemented, those that are not are WIP.
|
||||
- [ ] As an \{\$ACTOR\} I want \{\$FUNCTION\} because {$REASON}
|
||||
|
||||
## Q & A
|
||||
|
||||
## European Made
|
||||
|
||||
The author is European and living in the European Union. The software was created in Europe.
|
||||
+16
-6
@@ -1,6 +1,7 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"os"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
@@ -11,24 +12,33 @@ import (
|
||||
"github.com/kataras/golog"
|
||||
)
|
||||
|
||||
var app *fiber.App
|
||||
const idleTimeout time.Duration = 5 * time.Second
|
||||
const defaultPort string = ":9810"
|
||||
|
||||
const idleTimeout = 5 * time.Second
|
||||
var userPort string = os.Getenv("ORCHESTRA_API_PORT")
|
||||
var app *fiber.App
|
||||
|
||||
func StartAPIServer(wg *sync.WaitGroup) {
|
||||
defer wg.Done()
|
||||
|
||||
app = fiber.New(fiber.Config{IdleTimeout: idleTimeout})
|
||||
app.Use(cors.New())
|
||||
app.Use(responsetime.New())
|
||||
|
||||
g := app.Group("/api/v1/")
|
||||
// Set global group to /api, let routes choose version.
|
||||
prefix := app.Group("/api")
|
||||
|
||||
routes.GlobalRouter(g)
|
||||
routes.GlobalRouter(prefix)
|
||||
|
||||
golog.Fatal(app.Listen(":9810"))
|
||||
golog.Fatal(app.Listen(func() string {
|
||||
if userPort == "" {
|
||||
return defaultPort
|
||||
} else {
|
||||
return userPort
|
||||
}
|
||||
}()))
|
||||
}
|
||||
|
||||
func StopAPIService() {
|
||||
app.Shutdown()
|
||||
golog.Print("Test")
|
||||
}
|
||||
|
||||
@@ -1,21 +1,16 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/gofiber/fiber/v3"
|
||||
)
|
||||
|
||||
func GlobalRouter(r fiber.Router) {
|
||||
r.Group("/v1")
|
||||
|
||||
r.Get("/", func(c fiber.Ctx) error {
|
||||
return c.Status(200).JSON(&fiber.Map{
|
||||
"ping": "pong!",
|
||||
})
|
||||
})
|
||||
|
||||
r.Get("/time", func(c fiber.Ctx) error {
|
||||
return c.Status(200).JSON(&fiber.Map{
|
||||
"current_time": time.Now(),
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
+27
-3
@@ -6,23 +6,41 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/gofiber/fiber/v3"
|
||||
"github.com/gofiber/fiber/v3/extractors"
|
||||
"github.com/gofiber/fiber/v3/middleware/cors"
|
||||
"github.com/gofiber/fiber/v3/middleware/csrf"
|
||||
"github.com/gofiber/fiber/v3/middleware/responsetime"
|
||||
"github.com/gofiber/fiber/v3/middleware/session"
|
||||
"github.com/gofiber/fiber/v3/middleware/static"
|
||||
"github.com/kataras/golog"
|
||||
)
|
||||
|
||||
var app *fiber.App
|
||||
const idleTimeout time.Duration = 5 * time.Second
|
||||
const defaultPort string = ":9800"
|
||||
|
||||
const idleTimeout = 5 * time.Second
|
||||
var userPort string = os.Getenv("ORCHESTRA_WEB_PORT")
|
||||
var sessionStore *session.Store
|
||||
|
||||
var app *fiber.App
|
||||
|
||||
func StartWebServer(wg *sync.WaitGroup) {
|
||||
defer wg.Done()
|
||||
|
||||
app = fiber.New(fiber.Config{IdleTimeout: idleTimeout})
|
||||
app.Use(cors.New())
|
||||
app.Use(csrf.New())
|
||||
app.Use(responsetime.New())
|
||||
|
||||
app.Use(csrf.New(csrf.Config{
|
||||
CookieName: "__Orchestra-csrf_",
|
||||
CookieSecure: true,
|
||||
CookieHTTPOnly: false,
|
||||
CookieSameSite: "Lax",
|
||||
CookieSessionOnly: true,
|
||||
Extractor: extractors.FromHeader("X-Csrf-Token"),
|
||||
Session: sessionStore,
|
||||
}))
|
||||
|
||||
app.Get("/assets*", static.New("", static.Config{
|
||||
MaxAge: 31536000, // 1 year
|
||||
FS: os.DirFS("cmd/web/ui/dist/assets"),
|
||||
@@ -37,7 +55,13 @@ func StartWebServer(wg *sync.WaitGroup) {
|
||||
})
|
||||
})
|
||||
|
||||
golog.Fatal(app.Listen(":9800"))
|
||||
golog.Fatal(app.Listen(func() string {
|
||||
if userPort == "" {
|
||||
return defaultPort
|
||||
} else {
|
||||
return userPort
|
||||
}
|
||||
}()))
|
||||
}
|
||||
|
||||
func StopAPIService() {
|
||||
|
||||
@@ -10,6 +10,7 @@ require (
|
||||
github.com/gofiber/schema v1.7.0 // indirect
|
||||
github.com/gofiber/utils/v2 v2.0.2 // indirect
|
||||
github.com/google/uuid v1.6.0 // indirect
|
||||
github.com/joho/godotenv v1.5.1 // indirect
|
||||
github.com/kataras/golog v0.1.15 // indirect
|
||||
github.com/klauspost/compress v1.18.4 // indirect
|
||||
github.com/mattn/go-colorable v0.1.14 // indirect
|
||||
|
||||
@@ -8,6 +8,8 @@ github.com/gofiber/utils/v2 v2.0.2 h1:ShRRssz0F3AhTlAQcuEj54OEDtWF7+HJDwEi/aa6QL
|
||||
github.com/gofiber/utils/v2 v2.0.2/go.mod h1:+9Ub4NqQ+IaJoTliq5LfdmOJAA/Hzwf4pXOxOa3RrJ0=
|
||||
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
|
||||
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
|
||||
github.com/kataras/golog v0.1.15 h1:gDNOENbbn+6me98UW1f9Cs5MRUlAkabnNvmgLFM58Xw=
|
||||
github.com/kataras/golog v0.1.15/go.mod h1:Ozu1TDa+OKC7fFe7OG64In71yLxjda+6kPl+Rg3v1hA=
|
||||
github.com/klauspost/compress v1.18.4 h1:RPhnKRAQ4Fh8zU2FY/6ZFDwTVTxgJ/EMydqSTzE9a2c=
|
||||
|
||||
@@ -1,23 +0,0 @@
|
||||
package internal
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"github.com/kataras/golog"
|
||||
"github.com/tidwall/gjson"
|
||||
)
|
||||
|
||||
var file_content []byte
|
||||
|
||||
func ReadEnvFile(p string) {
|
||||
file_content, err := os.ReadFile(p)
|
||||
|
||||
if err != nil {
|
||||
golog.Info("Env file read requested, error occurred. Skipping...", file_content)
|
||||
}
|
||||
}
|
||||
|
||||
func GetFromEnvFile(query string) string {
|
||||
// INFO: To query, use: https://github.com/tidwall/gjson syntax.
|
||||
return gjson.Get(string(file_content), query).String()
|
||||
}
|
||||
@@ -10,6 +10,8 @@ import (
|
||||
|
||||
"github.com/BlackChaosNL/Orchestra/cmd/api"
|
||||
"github.com/BlackChaosNL/Orchestra/cmd/web"
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
"github.com/kataras/golog"
|
||||
)
|
||||
|
||||
@@ -26,6 +28,11 @@ func main() {
|
||||
var wg sync.WaitGroup
|
||||
golog.Install(log.New(os.Stdout, "", 0))
|
||||
|
||||
err := godotenv.Load()
|
||||
if err != nil {
|
||||
golog.Error(".env file not found, using default values. Skipping...")
|
||||
}
|
||||
|
||||
wg.Add(2)
|
||||
|
||||
go api.StartAPIServer(&wg)
|
||||
|
||||
Reference in New Issue
Block a user