-
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.
- Loading branch information
1 parent
fd36028
commit 49548b0
Showing
13 changed files
with
235 additions
and
0 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,5 +1,7 @@ | ||
# Untitled TTRPG discord bot service thingie | ||
|
||
For now setting this up as monorepo of services but might only have one. | ||
|
||
## .devcontainer | ||
|
||
See <https://github.com/qdm12/godevcontainer/tree/master> |
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,29 @@ | ||
package port | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/SomethingSexy/chronicle/internal/chronicle/service" | ||
"github.com/go-chi/chi/v5" | ||
"github.com/go-chi/chi/v5/middleware" | ||
) | ||
|
||
type HttpServer struct { | ||
app service.Application | ||
} | ||
|
||
func NewHttpServer(application service.Application) HttpServer { | ||
return HttpServer{ | ||
app: application, | ||
} | ||
} | ||
|
||
func (h HttpServer) Start() { | ||
// This should be responsible for running and starting the http service but it should be given the routes | ||
r := chi.NewRouter() | ||
r.Use(middleware.Logger) | ||
|
||
// TODO: Given the application, this should mount all of the route handlers | ||
|
||
http.ListenAndServe(":3000", r) | ||
} |
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 @@ | ||
This might have a bunch of folders for separated domains |
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,83 @@ | ||
package adapter | ||
|
||
import ( | ||
"errors" | ||
"net/http" | ||
|
||
"github.com/SomethingSexy/chronicle/internal/chronicle/core/game/domain" | ||
"github.com/go-chi/chi/v5" | ||
"github.com/go-chi/render" | ||
) | ||
|
||
// TODO: Do something with this error shit | ||
type ErrResponse struct { | ||
Err error `json:"-"` // low-level runtime error | ||
HTTPStatusCode int `json:"-"` // http response status code | ||
|
||
StatusText string `json:"status"` // user-level status message | ||
AppCode int64 `json:"code,omitempty"` // application-specific error code | ||
ErrorText string `json:"error,omitempty"` // application-level error message, for debugging | ||
} | ||
|
||
func (e *ErrResponse) Render(w http.ResponseWriter, r *http.Request) error { | ||
render.Status(r, e.HTTPStatusCode) | ||
return nil | ||
} | ||
|
||
func ErrInvalidRequest(err error) render.Renderer { | ||
return &ErrResponse{ | ||
Err: err, | ||
HTTPStatusCode: 400, | ||
StatusText: "Invalid request.", | ||
ErrorText: err.Error(), | ||
} | ||
} | ||
|
||
func ErrRender(err error) render.Renderer { | ||
return &ErrResponse{ | ||
Err: err, | ||
HTTPStatusCode: 422, | ||
StatusText: "Error rendering response.", | ||
ErrorText: err.Error(), | ||
} | ||
} | ||
|
||
var ErrNotFound = &ErrResponse{HTTPStatusCode: 404, StatusText: "Resource not found."} | ||
|
||
type GameHttpServer struct { | ||
} | ||
|
||
func NewHttpServer() GameHttpServer { | ||
return GameHttpServer{} | ||
} | ||
|
||
func (h GameHttpServer) Routes() chi.Router { | ||
r := chi.NewRouter() | ||
r.Post("/", CreateGame) | ||
return r | ||
} | ||
|
||
func CreateGame(w http.ResponseWriter, r *http.Request) { | ||
data := &GameRequest{} | ||
if err := render.Bind(r, data); err != nil { | ||
render.Render(w, r, ErrInvalidRequest(err)) | ||
return | ||
} | ||
|
||
render.Status(r, http.StatusCreated) | ||
// render.Render(w, r, NewGameResponse(article)) | ||
} | ||
|
||
type GameRequest struct { | ||
*domain.Game | ||
} | ||
|
||
func (a *GameRequest) Bind(r *http.Request) error { | ||
// a.Article is nil if no Article fields are sent in the request. Return an | ||
// error to avoid a nil pointer dereference. | ||
if a.Game == nil { | ||
return errors.New("missing required game fields") | ||
} | ||
|
||
return nil | ||
} |
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,18 @@ | ||
package application | ||
|
||
import "github.com/SomethingSexy/chronicle/internal/chronicle/core/game/application/command" | ||
|
||
type GameApplication struct { | ||
Commands GameCommands | ||
Queries GameQueries | ||
} | ||
|
||
type GameCommands struct { | ||
CreateGame command.CreateGameHander | ||
} | ||
|
||
type GameQueries struct { | ||
} | ||
|
||
// TODO: This should have a New function to setup the commands, repositories | ||
// and routes for this domain |
30 changes: 30 additions & 0 deletions
30
internal/chronicle/core/game/application/command/create_game.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,30 @@ | ||
package command | ||
|
||
import ( | ||
"context" | ||
"log" | ||
|
||
"github.com/SomethingSexy/chronicle/internal/chronicle/core/game/domain" | ||
) | ||
|
||
// TODO: Move this to a common spot | ||
// TODO: Name this Command or CommandHandler | ||
type CommandHandler[C any] interface { | ||
Handle(ctx context.Context, cmd C) error | ||
} | ||
|
||
type CreateGame struct { | ||
Game domain.Game | ||
} | ||
|
||
type CreateGameHander struct{} | ||
|
||
func NewCreateGameCommand() CommandHandler[CreateGame] { | ||
|
||
return CreateGameHander{} | ||
} | ||
|
||
func (c CreateGameHander) Handle(ctx context.Context, cmd CreateGame) error { | ||
log.Printf("Create game %s", cmd.Game.Name) | ||
return nil | ||
} |
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,6 @@ | ||
package domain | ||
|
||
type Game struct { | ||
Name string | ||
Type string | ||
} |
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,11 @@ | ||
package port | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/SomethingSexy/chronicle/internal/chronicle/core/game/domain" | ||
) | ||
|
||
type GameService interface { | ||
CreateUser(ctx context.Context, game domain.Game) error | ||
} |
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 |
---|---|---|
@@ -1,3 +1,9 @@ | ||
module github.com/SomethingSexy/chronicle/internal/chronicle | ||
|
||
go 1.22.1 | ||
|
||
require ( | ||
github.com/ajg/form v1.5.1 // indirect | ||
github.com/go-chi/chi/v5 v5.1.0 // indirect | ||
github.com/go-chi/render v1.0.3 // indirect | ||
) |
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,6 @@ | ||
github.com/ajg/form v1.5.1 h1:t9c7v8JUKu/XxOGBU0yjNpaMloxGEJhUkqFRq0ibGeU= | ||
github.com/ajg/form v1.5.1/go.mod h1:uL1WgH+h2mgNtvBq0339dVnzXdBETtL2LeUXaIv25UY= | ||
github.com/go-chi/chi/v5 v5.1.0 h1:acVI1TYaD+hhedDJ3r54HyA6sExp3HfXq7QWEEY/xMw= | ||
github.com/go-chi/chi/v5 v5.1.0/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8= | ||
github.com/go-chi/render v1.0.3 h1:AsXqd2a1/INaIfUSKq3G5uA8weYx20FOsM7uSoCyyt4= | ||
github.com/go-chi/render v1.0.3/go.mod h1:/gr3hVkmYR0YlEy3LxCuVRFzEu9Ruok+gFqbIofjao0= |
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,13 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
|
||
chronicleService "github.com/SomethingSexy/chronicle/internal/chronicle/service" | ||
) | ||
|
||
func main() { | ||
|
||
chronicleService.NewService() | ||
log.Println("verify running") | ||
} |
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,7 @@ | ||
package port | ||
|
||
import "github.com/go-chi/chi/v5" | ||
|
||
type HttpServer interface { | ||
Routes() chi.Router | ||
} |
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,23 @@ | ||
package service | ||
|
||
import ( | ||
"github.com/SomethingSexy/chronicle/internal/chronicle/core/game/adapter" | ||
gameApplication "github.com/SomethingSexy/chronicle/internal/chronicle/core/game/application" | ||
) | ||
|
||
type Application struct { | ||
Commands Commands | ||
Queries Queries | ||
} | ||
|
||
type Commands struct { | ||
gameApplication.GameCommands | ||
} | ||
|
||
type Queries struct { | ||
gameApplication.GameQueries | ||
} | ||
|
||
func NewService() { | ||
adapter.NewHttpServer() | ||
} |