Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v0.0.1 #26

Draft
wants to merge 268 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
268 commits
Select commit Hold shift + click to select a range
0c6f48e
tidy & rename
adoublef Sep 15, 2022
33d9189
remove unnecessaryfunction param
adoublef Sep 15, 2022
7b8ee5e
clean up play view
adoublef Sep 15, 2022
fad8c66
adding comments for harvey
adoublef Sep 15, 2022
cd6d564
Fix VSCode config; Use PORT env var
harveysanders Sep 15, 2022
042ff6b
readed graceful shutdown to `cmd` package
adoublef Sep 16, 2022
e762024
added prettierrc
pmoieni Sep 16, 2022
ffd38e0
go1.19 -> go1.18
adoublef Sep 16, 2022
a7b0fce
contextKey now a type alias
adoublef Sep 16, 2022
79440d2
Merge branch 'dev' of github.com:rog-golang-buddies/rapidmidiex into dev
adoublef Sep 16, 2022
44614a6
remove unnecessary request types
pmoieni Sep 16, 2022
1175ae2
fixed some typos
pmoieni Sep 16, 2022
0b80b80
moved config inside cmd
adoublef Sep 16, 2022
c157eb1
Merge branch 'dev' of github.com:rog-golang-buddies/rapidmidiex into dev
adoublef Sep 16, 2022
62d928e
Server start log
harveysanders Sep 16, 2022
48679e3
added a U/SUID type as wrapper/alternative to Google's uuid.UUID type
adoublef Sep 16, 2022
b8d7098
uuid.UUID -> rmx.UUID
adoublef Sep 16, 2022
33d925b
Merge branch 'dev' of github.com:rog-golang-buddies/rapidmidiex into dev
adoublef Sep 16, 2022
f5809ca
refactor inside run function
adoublef Sep 16, 2022
9dc2b0c
inteface{} -> any
adoublef Sep 16, 2022
c717b7a
Reorg server and TUI cmds
harveysanders Sep 16, 2022
f2161e8
Merge branch 'dev' of github.com:rog-golang-buddies/rapidmidiex into dev
harveysanders Sep 16, 2022
5f4e618
mv run/loadConfig into main.go
adoublef Sep 16, 2022
ac03d18
fixed dependencies
pmoieni Sep 16, 2022
7708345
WIP: 5f4e618 mv run/loadConfig into main.go
adoublef Sep 17, 2022
0627968
Merge branch 'dev' of github.com:rog-golang-buddies/rapidmidiex into dev
adoublef Sep 17, 2022
573cdcb
v0 is depreciated
adoublef Sep 17, 2022
792c5ae
suid test file change
adoublef Sep 17, 2022
c19f6fe
added new id generation tool
pmoieni Sep 17, 2022
be28c58
added list sessions handler
pmoieni Sep 18, 2022
b4ae585
tmp rm of assets folder
adoublef Sep 18, 2022
69d6b6d
Merge branch 'dev' of github.com:rog-golang-buddies/rapidmidiex into dev
adoublef Sep 18, 2022
a0c28ad
Refactor REST and ws routes
harveysanders Sep 19, 2022
40a6d29
routes change
adoublef Sep 19, 2022
354384c
Merge branch 'dev' of github.com:rog-golang-buddies/rapidmidiex into dev
adoublef Sep 19, 2022
e30b898
change sessionData handler syntax
pmoieni Sep 19, 2022
d93ef8e
Merge branch 'dev' of github.com:rog-golang-buddies/rapidmidiex into dev
adoublef Sep 19, 2022
7e694ae
changed www to api
pmoieni Sep 19, 2022
5970468
added POST to allowed methods
pmoieni Sep 19, 2022
73046bb
removed key from parseUUID and fixed a typo
pmoieni Sep 20, 2022
d411f17
Initial TUI client setup
harveysanders Sep 20, 2022
e798346
Initial Jam view connected
harveysanders Sep 20, 2022
f148dfa
Join Jam
harveysanders Sep 20, 2022
0f864c5
fix ws route
harveysanders Sep 21, 2022
af2cc31
api changes
adoublef Sep 21, 2022
079113d
Merge branch 'dev' of github.com:rog-golang-buddies/rapidmidiex into dev
adoublef Sep 21, 2022
55857ae
Status and help menu (not working)
harveysanders Sep 21, 2022
e1bcabc
Merge branch 'dev' of github.com:rog-golang-buddies/rapidmidiex into dev
harveysanders Sep 21, 2022
b690d6c
Update help menu
harveysanders Sep 22, 2022
81d5801
Resolve merge conflicts
harveysanders Sep 22, 2022
fbee0eb
Merge branch 'dev' of github.com:rog-golang-buddies/rapidmidiex into …
harveysanders Sep 22, 2022
06e741d
Fix Service receivers
harveysanders Sep 22, 2022
f23b5a3
clean up conflicts
adoublef Sep 22, 2022
cebd834
Merge branch 'dev' of github.com:rog-golang-buddies/rapidmidiex into dev
adoublef Sep 22, 2022
f68d41c
go mod tidy
adoublef Sep 22, 2022
f39ef19
move "api" package to "service" package
adoublef Sep 23, 2022
cfe80c1
move `websocket` package inside `internal`
adoublef Sep 23, 2022
beeb688
`ap`i &`middleware` to `service` & `websocket`
adoublef Sep 23, 2022
b541414
soft-delete of internal web ui
adoublef Sep 23, 2022
303191f
clean up
adoublef Sep 23, 2022
8be0c68
Creating a new Jam Session
harveysanders Sep 24, 2022
2b11200
adding testable endpoint
adoublef Sep 24, 2022
0a2dea5
potential mutex issue
adoublef Sep 24, 2022
5e7efc5
Merge branch 'dev' of github.com:rog-golang-buddies/rapidmidiex into dev
adoublef Sep 24, 2022
0e65325
reamove ui/web and assets folders
adoublef Sep 24, 2022
58ad099
cleanup Service.connectionPool
adoublef Sep 24, 2022
ee71fd8
fixed makefile build command
pmoieni Sep 24, 2022
734decc
default port 8888
adoublef Sep 24, 2022
adaedf5
new CLI for server
pmoieni Sep 24, 2022
77d20c0
Fix ws endpoint, launch config
harveysanders Sep 25, 2022
4e1a639
moved cli to cli.go (contains bug)
pmoieni Sep 25, 2022
1ea68ee
fixed makefile
pmoieni Sep 25, 2022
a99b2f7
truncate to single file
adoublef Sep 25, 2022
3d03337
errInvalidPort -> ErrInvalidPort
adoublef Sep 25, 2022
47a4eb5
custom type has exported fields; made it exported
adoublef Sep 25, 2022
0baf085
rename from rapidmidiex to rmx
pmoieni Sep 25, 2022
7b246d5
lspsaga config
pmoieni Sep 27, 2022
9c3a56a
added User db config and other small changes
pmoieni Sep 29, 2022
2f8fc45
db/ root-package && splitting up logic in service/
adoublef Sep 30, 2022
3d2656e
ping endpoint for user.Service
adoublef Sep 30, 2022
ac2660f
dummy paths for user.Service
adoublef Sep 30, 2022
564c556
unit & integration tests for services (handlePing)
adoublef Sep 30, 2022
6f5845d
(s Service) -> (s *Service)
adoublef Sep 30, 2022
108e8b7
mock user repo
adoublef Sep 30, 2022
bbe0675
remove errTodo
adoublef Sep 30, 2022
99751d7
Email, Password(Hash) types added
adoublef Sep 30, 2022
1eca293
move repo/user struct defs to the top
adoublef Sep 30, 2022
97e0558
simple signup example, bound to change
adoublef Sep 30, 2022
edb884d
logic for auth slowly being brought in
adoublef Oct 1, 2022
c036ae1
start on auth package
adoublef Oct 1, 2022
c660f60
middleware using chi.With
adoublef Oct 1, 2022
7bd42b4
remove redundant handlers
adoublef Oct 1, 2022
1c4e182
reset cli
adoublef Oct 1, 2022
214974d
handler_register, handle_login, fleshing out
adoublef Oct 1, 2022
9030db0
auth works?
adoublef Oct 1, 2022
528bf00
prep for cleanup
adoublef Oct 1, 2022
a702829
small fix
pmoieni Oct 1, 2022
5d1c315
moved tokens to a helper
adoublef Oct 1, 2022
515e666
in-mem secret will be replaced with env var
adoublef Oct 1, 2022
8304a99
Merge branch 'dev' of github.com:rog-golang-buddies/rmx into dev
adoublef Oct 1, 2022
b21908b
small clean-up
adoublef Oct 1, 2022
803d85d
fixed token generation
adoublef Oct 1, 2022
c112dee
working on refresh route
adoublef Oct 2, 2022
2ce24aa
removed duplication with gen auth token
adoublef Oct 2, 2022
a3d4c78
generate key from pem helper
adoublef Oct 2, 2022
6b42b85
added default run for debugging
adoublef Oct 2, 2022
f2239f4
attempt at reading PEM file.
adoublef Oct 2, 2022
05e4fc6
mock does signup new user properly now
adoublef Oct 2, 2022
cde139f
Cookie.secure set to false
adoublef Oct 2, 2022
5bdcc7d
delete session works
adoublef Oct 3, 2022
b8fb15f
removed logs
adoublef Oct 3, 2022
186ba51
removed userPath variable for "/"
adoublef Oct 3, 2022
4cd02b1
user service comments pushed to top of routes method
adoublef Oct 3, 2022
81a93e6
fixed sqlc.yaml typo
pmoieni Oct 3, 2022
792ace1
better auth implementation (not tested)
pmoieni Oct 6, 2022
133d9ea
included testing package
adoublef Oct 6, 2022
aa924c8
service.Service should not use go routines
adoublef Oct 6, 2022
1635c31
root service is currently disconnected from cmd/
adoublef Oct 6, 2022
4d0e5f4
fix authenticate middleware (not tested)
pmoieni Oct 6, 2022
d62380a
delete service tests, will re-add
adoublef Oct 6, 2022
5de809c
Merge branch 'dev' of github.com:rog-golang-buddies/rmx into dev
adoublef Oct 6, 2022
5804a82
auth back in internal
adoublef Oct 6, 2022
e4e5e5a
fix refresh token flow (not tested)
pmoieni Oct 6, 2022
62dd80a
Merge branch 'dev' of https://github.com/rog-golang-buddies/rmx into dev
pmoieni Oct 6, 2022
896354c
create new service test
adoublef Oct 6, 2022
51419ed
created password and password hash types
adoublef Oct 6, 2022
1708880
cleaned up marshalling
adoublef Oct 6, 2022
52d26ae
signup route
adoublef Oct 6, 2022
adea26b
signToken function tested
adoublef Oct 6, 2022
9107391
create token
adoublef Oct 6, 2022
a0146f5
tokenOption inc. algo option
adoublef Oct 6, 2022
311baa3
indentity and sign-out methods tested
adoublef Oct 6, 2022
0eea886
auth mw now includes algoTyp as an argument
adoublef Oct 6, 2022
36e165f
signedTokens helper takes in an id value
adoublef Oct 6, 2022
4f99ad5
add in-mem repo
adoublef Oct 6, 2022
d8e2cf6
look-back at v1
adoublef Oct 6, 2022
22086f0
added testing for authentication
adoublef Oct 6, 2022
913a068
auth redis client
adoublef Oct 6, 2022
2d7673a
suid.MustParse
adoublef Oct 6, 2022
a8079b5
rearrange handleRefreshToken
adoublef Oct 6, 2022
2bae747
middlware tests for Autherization/Cookie headers
adoublef Oct 6, 2022
f688d5b
added testing for refresh inside service
adoublef Oct 6, 2022
79c0d2d
auth is now in a single file
adoublef Oct 6, 2022
81d0379
cleaning up store/
adoublef Oct 6, 2022
fcaaf7d
jam service needs to get some tests
adoublef Oct 6, 2022
4618ceb
moved auth into root internal package
adoublef Oct 6, 2022
136b05a
clean up
adoublef Oct 6, 2022
660e2c4
cmd main needs to held off till other packages have good tests
adoublef Oct 6, 2022
e63c427
ignore incoming changes
adoublef Oct 6, 2022
31fecd3
auth/ and user/ packages now root of store/ package
adoublef Oct 6, 2022
518c7e9
dto not needed
adoublef Oct 6, 2022
da9393e
deleted unneeded pacakges
adoublef Oct 6, 2022
999d50c
not needed
adoublef Oct 6, 2022
fb67e02
service test still works after changing inmem repo
adoublef Oct 6, 2022
9f3afec
token client cleanup
adoublef Oct 6, 2022
9ce634e
add in-mem client
adoublef Oct 6, 2022
ea56a56
Merge branch 'dev' of github.com:rog-golang-buddies/rmx into dev
adoublef Oct 6, 2022
3ea6553
add fatal to use instead of panic
pmoieni Oct 6, 2022
7550601
change Authentication to ParseAuth
pmoieni Oct 6, 2022
6f810bc
fix sql and redis interfaces
pmoieni Oct 6, 2022
423ae23
fix refresh token flow
pmoieni Oct 6, 2022
017bd7b
uuid to suid for Client ID
pmoieni Oct 6, 2022
a6cd80a
better API for CLI
pmoieni Oct 7, 2022
b3330d4
store cleanup
adoublef Oct 7, 2022
7d5507b
auth service cleanup
adoublef Oct 7, 2022
aecb7dd
Merge branch 'dev' of github.com:rog-golang-buddies/rmx into dev
adoublef Oct 7, 2022
38727b9
CLI prompts
pmoieni Oct 7, 2022
483ea45
write/load configuration to/from file
pmoieni Oct 7, 2022
9f8f9ff
fix sqlc.yaml
pmoieni Oct 7, 2022
1a01454
fix small bug
pmoieni Oct 7, 2022
f00db32
clean run file
pmoieni Oct 7, 2022
5f7408b
🍕 /api/v2 -> /api/v1
pmoieni Oct 7, 2022
9982c38
Merge branch 'dev' of github.com:rog-golang-buddies/rmx into dev
adoublef Oct 7, 2022
2c0c3ae
store && service
adoublef Oct 7, 2022
e2a5bdf
no need for `Lookup` as `Select` already does this
adoublef Oct 7, 2022
3cb3eec
update service
adoublef Oct 7, 2022
8d3f583
example of a repo that wraps sqlc gen code w/ test
adoublef Oct 7, 2022
41dbc9d
gen file can live in here
adoublef Oct 7, 2022
38e6764
cleanup
adoublef Oct 7, 2022
653fe61
getting user db setup with tests
adoublef Oct 7, 2022
051a71a
dummy, will be deleted once user is setup
adoublef Oct 7, 2022
c0f5590
interface fix
adoublef Oct 7, 2022
7438ef8
interface fix
adoublef Oct 7, 2022
a2ecc14
config inside store/sql package
adoublef Oct 7, 2022
e6164e2
got mod tidy
adoublef Oct 7, 2022
e8754c8
mysql & psql generation to see the difference
adoublef Oct 8, 2022
2a47a6f
store//sql/user/mysql.sql in mysql format again
adoublef Oct 8, 2022
01df7aa
auto gen files deleted for time being
adoublef Oct 8, 2022
a92c2a6
internal.UserRepo updated
adoublef Oct 8, 2022
3faa86f
go mod tidy
adoublef Oct 8, 2022
67d0171
fixed bugs
adoublef Oct 14, 2022
cf41fd2
🧪 config test
pmoieni Oct 14, 2022
a71db26
❌ remove unused dependency
pmoieni Oct 14, 2022
7a1926f
✨ better dev mode
pmoieni Oct 14, 2022
cdfa558
in process of cleaning up
adoublef Oct 20, 2022
f74acba
generic helpers for sql operation added
adoublef Oct 20, 2022
29fac04
move to external types package
adoublef Oct 20, 2022
9a7d1a5
adding documentation inside repo/user
adoublef Oct 20, 2022
c02930b
sorted repo/user
adoublef Oct 20, 2022
474b6c4
reverting auth to version that passed tests
adoublef Oct 20, 2022
c819f50
auth api changed, removed the Pair type
adoublef Oct 20, 2022
6cae235
🪛 fix auth tests
pmoieni Oct 20, 2022
b62e5f6
auth_test fixed
adoublef Oct 20, 2022
e1d4a56
Merge 'dev' of github.com:rog-golang-buddies/rmx
adoublef Oct 20, 2022
2d71e89
fix errors with key
adoublef Oct 20, 2022
a3f7a32
update to [email protected]
adoublef Oct 21, 2022
729b507
added experimental ws package
adoublef Oct 21, 2022
ef203ef
echo test with websocket
adoublef Oct 21, 2022
bfda18b
update external pacakges
adoublef Oct 21, 2022
e4c7b4c
tests between two clients using a string message
adoublef Oct 21, 2022
54dd199
🧬 small improvements
pmoieni Nov 1, 2022
85c062e
rework with user repo
adoublef Oct 29, 2022
08731a1
fix user repo bugs
adoublef Oct 29, 2022
336cd4a
progress with concurrent pool
adoublef Oct 29, 2022
3ca5f34
experimental websocket api
adoublef Oct 29, 2022
f38f451
store updates
adoublef Oct 29, 2022
8c136c8
store/sql/user -> store/user
adoublef Nov 5, 2022
e1dfc82
websockets create and join
adoublef Nov 5, 2022
9995048
hyphengolang/prelude v0.1.0 -> v0.1.3
adoublef Nov 6, 2022
54bfe90
slightly cleaner
adoublef Nov 6, 2022
afa1d24
removed dubplicated packages
adoublef Nov 6, 2022
9a5679a
hotfix services
adoublef Nov 6, 2022
ef3cafb
hotfix user_repo
adoublef Nov 6, 2022
7ce7e4f
go tests pass
adoublef Nov 6, 2022
5f34275
pkg has auth, service and test pacakges
adoublef Dec 12, 2022
35cce75
auth service passes tests
adoublef Dec 12, 2022
4664e03
jam service passes websocket tests
adoublef Dec 12, 2022
9931fa3
root service refactor
adoublef Dec 12, 2022
fccb5df
root store refactor
adoublef Dec 12, 2022
c7a8ced
auth token client not touched
adoublef Dec 12, 2022
30f36b7
user repo refactor
adoublef Dec 12, 2022
2b5213e
internal package refactor
adoublef Dec 12, 2022
8ab2d4e
auth refactoring
adoublef Dec 12, 2022
9722785
new websocket implementation (in development)
pmoieni Dec 26, 2022
23f6b4c
fix JSON tags
pmoieni Dec 28, 2022
a853ec3
websocket structure cleanup
pmoieni Dec 28, 2022
b101236
websocket revamp & new error type
pmoieni Dec 29, 2022
8aa6e68
fix jam service
pmoieni Dec 29, 2022
220e67e
test public API for jam service
pmoieni Dec 29, 2022
269a645
return on errors
harveysanders Dec 29, 2022
185411c
fix error handling
pmoieni Dec 30, 2022
9aa2f99
Split the RMX CLI and server application into two commands
harveysanders Dec 31, 2022
f7d40ec
Set the token client to DefaultTokenClient
harveysanders Dec 31, 2022
0f6f3cb
Move middleware registration before route registration
harveysanders Dec 31, 2022
b32f004
Merge pull request #31 from rog-golang-buddies/chore/split-cli-server…
harveysanders Dec 31, 2022
3fd6b48
new endpoints
pmoieni Dec 31, 2022
a46198b
subscriber connect method fix
pmoieni Dec 31, 2022
ef33bfc
new handlers
pmoieni Jan 1, 2023
bd639fb
Export Jam ID for API responses
harveysanders Jan 1, 2023
1c2eb0b
send Jam IDs
pmoieni Jan 1, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,7 @@ bin/

# Other
.DS_Store
*.env*
certs
rmx.config.json
rmx-dev.config.json
12 changes: 12 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"trailingComma": "es5",
"tabWidth": 4,
"semi": true,
"singleQuote": true,
"bracketSpacing": true,
"bracketSameLine": true,
"arrowParens": "always",
"endOfLine": "lf",
"embeddedLanguageFormatting": "auto",
"singleAttributePerLine": true
}
36 changes: 36 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Start App Server (dev)",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}/cmd/server",
"cwd": "${workspaceFolder}",
"envFile": "${workspaceFolder}/.env.development"
},
{
"name": "Start CLI Server (dev)",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}/cmd/cli",
"cwd": "${workspaceFolder}",
"args": ["start", "dev"]
},
{
"name": "TUI Client",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}/ui/terminal",
"cwd": "${workspaceFolder}",
"args": ["--server", "http://localhost:9003"],
"console": "integratedTerminal"
}
]
}
16 changes: 8 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ fmt:
go fmt ./...

## build_server: Build server binary into bin/ directory
.PHONY: build_server
build_server:
$(GOFLAGS) $(GO_BUILD) -a -v -ldflags="-w -s" -o bin/server cmd/server/main.go

## build_cli: Build cli binary into bin/ directory
.PHONY: build_cli
build_cli:
$(GOFLAGS) $(GO_BUILD) -a -v -ldflags="-w -s" -o bin/cli cmd/cli/main.go
.PHONY: build
build:
$(GOFLAGS) $(GO_BUILD) -a -v -ldflags="-w -s" -o bin/rmx-server cmd/*.go

# --host should be from ENV
.PHONT: tls
tls:
go run /usr/local/go/src/crypto/tls/generate_cert.go --host=$(HOSTNAME)
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# golang-template-repository
<!-- # golang-template-repository

> 💣 &nbsp; _All Setup content in this file should be replaced with your project details post setup._

Expand All @@ -25,16 +25,20 @@ A gh-pages site is automatically generated for you when you clone/fork this repo

<br>

> _The following section provides a sample README template sourced from https://www.makeareadme.com_
> _The following section provides a sample README template sourced from https://www.makeareadme.com_ -->

# [Repo name]
# Rapidmidiex

## Description

_Let people know what your project can do specifically. Provide context and add a link to any reference visitors might be unfamiliar with. A list of Features or a Background subsection can also be added here. If there are alternatives to your project, this is a good place to list differentiating factors._

## Installation

<!-- accessing the web ui -->

<!-- tui installation -->

_Within a particular ecosystem, there may be a common way of installing things, such as using Yarn, NuGet, or Homebrew. However, consider the possibility that whoever is reading your README is a novice and would like more guidance. Listing specific steps helps remove ambiguity and gets people to using your project as quickly as possible. If it only runs in a specific context like a particular programming language version or operating system or has dependencies that have to be installed manually, also add a Requirements subsection._

## Usage
Expand Down
29 changes: 26 additions & 3 deletions cmd/cli/main.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,31 @@
package main

import "fmt"
import (
"log"
"os"
"time"

"github.com/rog-golang-buddies/rmx/internal/commands"
"github.com/urfave/cli/v2"
)

func main() {
// Feel free to delete this file.
fmt.Println("Hello Gophers")
if err := initCLI().Run(os.Args); err != nil {
log.Fatalln(err)
}
}

func initCLI() *cli.App {
c := &cli.App{
EnableBashCompletion: true,
Name: "rmx",
Usage: "RapidMidiEx Server CLI",
Version: commands.Version,
Compiled: time.Now().UTC(),
Action: commands.GetVersion,
Flags: commands.Flags,
Commands: commands.Commands,
}

return c
}
8 changes: 0 additions & 8 deletions cmd/main.go

This file was deleted.

24 changes: 21 additions & 3 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,26 @@
package main

import "fmt"
import (
"log"
"os"

"github.com/rog-golang-buddies/rmx/config"
"github.com/rog-golang-buddies/rmx/internal/commands"
)

func main() {
// Feel free to delete this file.
fmt.Println("Hello Gophers")
rmxEnv := os.Getenv("RMX_ENV")
isDev := false
if rmxEnv == "development" {
isDev = true
}
cfg, err := config.LoadConfigFromEnv(isDev)
if err != nil {
log.Fatalf("Could load config: %v", err)
}

err = commands.StartServer(cfg)
if err != nil {
log.Fatalf("Could not start server: %v", err)
}
}
126 changes: 126 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package config

import (
"encoding/json"
"errors"
"fmt"
"log"
"net/url"
"os"
)

type Config struct {
ServerPort string `json:"serverPort"`
DBHost string `json:"dbHost"`
DBPort string `json:"dbPort"`
DBName string `json:"dbName"`
DBUser string `json:"dbUser"`
DBPassword string `json:"dbPassword"`
RedisHost string `json:"redisHost"`
RedisPort string `json:"redisPort"`
RedisPassword string `json:"redisPassword"`
}

const (
configFileName = "rmx.config.json"
devConfigFileName = "rmx-dev.config.json"
)

// writes the values of the config to a file
// NOTE: this will overwrite the previous generated file
func (c *Config) WriteToFile(dev bool) error {
var fp string
if dev {
fp = devConfigFileName
} else {
fp = configFileName
}

bs, err := json.MarshalIndent(c, "", " ")
if err != nil {
return err
}

f, err := os.OpenFile(fp, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0660)
if err != nil {
return err
}

defer func() {
if err := f.Close(); err != nil {
log.Fatalln(err)
}
}()

if _, err := f.Write(bs); err != nil {
return err
}

return nil
}

// checks for a config file and if one is available the value is returned
func ScanConfigFile(dev bool) (*Config, error) {
// check for a config file
var fp string
if dev {
fp = devConfigFileName
} else {
fp = configFileName
}

if _, err := os.Stat(fp); err != nil {
if errors.Is(err, os.ErrNotExist) {
return nil, nil
}

return nil, err
}

c := &Config{}
bs, err := os.ReadFile(fp)
if err != nil {
return nil, err
}

if err := json.Unmarshal(bs, c); err != nil {
return nil, err
}

return c, nil
}

// LoadConfigFromEnv creates a Config from environment variables.
func LoadConfigFromEnv(dev bool) (*Config, error) {
serverPort := os.Getenv("PORT")

pgURI := os.Getenv("POSTGRES_URI")

pgParsed, err := url.Parse(pgURI)
if err != nil && pgURI != "" {
return nil, fmt.Errorf("invalid POSTGRES_URL env var: %q: %w", pgURI, err)
}

pgUser := pgParsed.User.Username()
pgPassword, _ := pgParsed.User.Password()

pgHost := pgParsed.Host
pgPort := pgParsed.Port()
pgName := pgParsed.Path

redisHost := os.Getenv("REDIS_HOST")
redisPort := os.Getenv("REDIS_PORT")
redisPassword := os.Getenv("REDIS_PASSWORD")

return &Config{
ServerPort: serverPort,
DBHost: pgHost,
DBPort: pgPort,
DBName: pgName,
DBUser: pgUser,
DBPassword: pgPassword,
RedisHost: redisHost,
RedisPort: redisPort,
RedisPassword: redisPassword,
}, nil
}
35 changes: 35 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package config

import (
"reflect"
"testing"
)

func TestConfig(t *testing.T) {
// Write config to file
i := &Config{
ServerPort: "8000",
DBHost: "localhost",
DBPort: "3306",
DBName: "rmx",
DBUser: "rmx",
DBPassword: "password",
RedisHost: "localhost",
RedisPort: "6379",
RedisPassword: "password",
}

if err := i.WriteToFile(false); err != nil {
t.Fatal(err)
}

// Read config from file
o, err := ScanConfigFile(false)
if err != nil {
t.Fatal(err)
}

if !reflect.DeepEqual(i, o) {
t.Fatalf("expected:\n%+v\ngot:\n%+v", i, o)
}
}
Loading