Skip to content

Commit

Permalink
feat: wip project setup
Browse files Browse the repository at this point in the history
  • Loading branch information
SomethingSexy committed Jul 21, 2024
1 parent fd36028 commit 49548b0
Show file tree
Hide file tree
Showing 13 changed files with 235 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
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>
29 changes: 29 additions & 0 deletions internal/chronicle/adapter/http.go
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)
}
1 change: 1 addition & 0 deletions internal/chronicle/core/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This might have a bunch of folders for separated domains
83 changes: 83 additions & 0 deletions internal/chronicle/core/game/adapter/http.go
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
}
18 changes: 18 additions & 0 deletions internal/chronicle/core/game/application/application.go
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 internal/chronicle/core/game/application/command/create_game.go
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
}
6 changes: 6 additions & 0 deletions internal/chronicle/core/game/domain/game.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package domain

type Game struct {
Name string
Type string
}
11 changes: 11 additions & 0 deletions internal/chronicle/core/game/port/service.go
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
}
6 changes: 6 additions & 0 deletions internal/chronicle/go.mod
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
)
6 changes: 6 additions & 0 deletions internal/chronicle/go.sum
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=
13 changes: 13 additions & 0 deletions internal/chronicle/main.go
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")
}
7 changes: 7 additions & 0 deletions internal/chronicle/port/server.go
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
}
23 changes: 23 additions & 0 deletions internal/chronicle/service/application.go
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()
}

0 comments on commit 49548b0

Please sign in to comment.