-
Notifications
You must be signed in to change notification settings - Fork 8
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
Showing
16 changed files
with
451 additions
and
8 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
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,48 @@ | ||
package db | ||
|
||
import ( | ||
"database/sql" | ||
|
||
"github.com/google/wire" | ||
"github.com/gsxhnd/garage/utils" | ||
_ "github.com/mattn/go-sqlite3" | ||
) | ||
|
||
type Database struct { | ||
TenhouDB *sql.DB | ||
logger utils.Logger | ||
} | ||
|
||
func NewDatabase(cfg *utils.Config, l utils.Logger) (*Database, error) { | ||
// dbSource := cfg.TenhouDBPath | ||
dbSource := "" | ||
db, err := sql.Open("sqlite3", dbSource) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
sqlStmt := ` | ||
CREATE TABLE IF NOT EXISTS "tenhou" | ||
( | ||
"id" INTEGER NOT NULL UNIQUE, | ||
"log_id" VARCHAR NOT NULL UNIQUE, | ||
"game_type" VARCHAR, | ||
"game_date" DATETIME, | ||
PRIMARY KEY ("id") | ||
); | ||
CREATE INDEX IF NOT EXISTS "tenhou_index_0" | ||
ON "tenhou" ("id", "log_id"); | ||
` | ||
_, err = db.Exec(sqlStmt) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &Database{ | ||
TenhouDB: db, | ||
logger: l, | ||
}, nil | ||
} | ||
|
||
var DBSet = wire.NewSet(NewDatabase) |
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 di | ||
|
||
import ( | ||
"github.com/gsxhnd/garage/garage_server/router" | ||
"golang.org/x/sync/errgroup" | ||
) | ||
|
||
type Application struct { | ||
r router.Router | ||
} | ||
|
||
func NewApplication(r router.Router) *Application { | ||
return &Application{ | ||
r: r, | ||
} | ||
} | ||
|
||
func (a *Application) Run() error { | ||
var g errgroup.Group | ||
|
||
g.Go(func() error { | ||
return a.r.Run() | ||
}) | ||
|
||
if err := g.Wait(); err != nil { | ||
return err | ||
} else { | ||
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,28 @@ | ||
//go:build wireinject | ||
// +build wireinject | ||
|
||
// The build tag makes sure the stub is not built in the final build. | ||
package di | ||
|
||
import ( | ||
"github.com/google/wire" | ||
"github.com/gsxhnd/garage/garage_server/db" | ||
"github.com/gsxhnd/garage/garage_server/handler" | ||
"github.com/gsxhnd/garage/garage_server/middleware" | ||
"github.com/gsxhnd/garage/garage_server/router" | ||
"github.com/gsxhnd/garage/garage_server/service" | ||
"github.com/gsxhnd/garage/utils" | ||
) | ||
|
||
func InitApp() (*Application, error) { | ||
wire.Build( | ||
utils.UtilsSet, | ||
NewApplication, | ||
router.NewRouter, | ||
middleware.NewMiddleware, | ||
handler.HandlerSet, | ||
service.ServiceSet, | ||
db.DBSet, | ||
) | ||
return &Application{}, nil | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,14 @@ | ||
package handler | ||
|
||
import ( | ||
"github.com/google/wire" | ||
) | ||
|
||
type Handler struct { | ||
PingHandler PingHandler | ||
} | ||
|
||
var HandlerSet = wire.NewSet( | ||
NewPingHandler, | ||
wire.Struct(new(Handler), "*"), | ||
) |
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,33 @@ | ||
package handler | ||
|
||
import ( | ||
"github.com/gofiber/fiber/v2" | ||
"github.com/gsxhnd/garage/garage_server/service" | ||
) | ||
|
||
type PingHandler interface { | ||
Ping(ctx *fiber.Ctx) error | ||
} | ||
|
||
type pingHandle struct { | ||
// validator *validator.Validate | ||
svc service.PingService | ||
} | ||
|
||
func NewPingHandler(svc service.PingService) PingHandler { | ||
return &pingHandle{ | ||
svc: svc, | ||
} | ||
} | ||
|
||
// @Description ping serivce working, db connect | ||
// @Produce json | ||
// @Success 200 | ||
// @Router /ping [get] | ||
func (h *pingHandle) Ping(ctx *fiber.Ctx) error { | ||
err := h.svc.Ping() | ||
if err != nil { | ||
return ctx.Status(400).SendString(err.Error()) | ||
} | ||
return ctx.Status(200).SendString("pong") | ||
} |
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,7 +1,15 @@ | ||
package main | ||
|
||
import "fmt" | ||
import ( | ||
"github.com/gsxhnd/garage/garage_server/di" | ||
) | ||
|
||
func main() { | ||
fmt.Println("server") | ||
app, err := di.InitApp() | ||
if err != nil { | ||
panic(err) | ||
} | ||
if err := app.Run(); err != nil { | ||
panic(err) | ||
} | ||
} |
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 middleware | ||
|
||
import ( | ||
"github.com/gsxhnd/garage/utils" | ||
) | ||
|
||
type Middlewarer interface { | ||
// RequestLog(ctx *fiber.Ctx) error | ||
} | ||
type middleware struct { | ||
logger utils.Logger | ||
} | ||
|
||
func NewMiddleware(l utils.Logger) Middlewarer { | ||
return &middleware{ | ||
logger: l, | ||
} | ||
} |
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,58 @@ | ||
package router | ||
|
||
import ( | ||
"github.com/gofiber/fiber/v2" | ||
"github.com/gsxhnd/garage/garage_server/handler" | ||
"github.com/gsxhnd/garage/garage_server/middleware" | ||
"github.com/gsxhnd/garage/utils" | ||
) | ||
|
||
type Router interface { | ||
Run() error | ||
} | ||
|
||
type router struct { | ||
cfg *utils.Config | ||
app *fiber.App | ||
h handler.Handler | ||
m middleware.Middlewarer | ||
} | ||
|
||
// @title Tenhou API | ||
// @version 1 | ||
// @description This is a sample server celler server. | ||
// @license.name MIT | ||
// @license.url https://opensource.org/license/mit | ||
// @host localhost:8080 | ||
// @securityDefinitions.basic BasicAuth | ||
// @externalDocs.description OpenAPI | ||
func NewRouter(cfg *utils.Config, m middleware.Middlewarer, h handler.Handler) (Router, error) { | ||
app := fiber.New(fiber.Config{ | ||
EnablePrintRoutes: cfg.Mode == "dev", | ||
DisableStartupMessage: cfg.Mode == "prod", | ||
Prefork: false, | ||
}) | ||
|
||
return &router{ | ||
cfg: cfg, | ||
app: app, | ||
h: h, | ||
m: m, | ||
}, nil | ||
} | ||
|
||
func (r *router) Run() error { | ||
// r.app.Use(r.m.RequestLog) | ||
r.app.Get("/ping", r.h.PingHandler.Ping) | ||
|
||
// api := r.app.Group("/api/v1") | ||
// api.Get("/log/:log_id", r.h.LogHandler.GetLogInfoByLogId) | ||
// api.Get("/paifu") | ||
// api.Get("/paifu/:log_id") | ||
|
||
r.app.Use(func(c *fiber.Ctx) error { | ||
return c.SendStatus(404) | ||
}) | ||
|
||
return r.app.Listen(r.cfg.Listen) | ||
} |
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 service | ||
|
||
import "github.com/google/wire" | ||
|
||
var ServiceSet = wire.NewSet( | ||
NewPingService, | ||
) |
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,31 @@ | ||
package service | ||
|
||
import ( | ||
"github.com/gsxhnd/garage/garage_server/db" | ||
"github.com/gsxhnd/garage/utils" | ||
) | ||
|
||
type PingService interface { | ||
Ping() error | ||
} | ||
|
||
type pingService struct { | ||
logger utils.Logger | ||
db *db.Database | ||
} | ||
|
||
func NewPingService(l utils.Logger, db *db.Database) PingService { | ||
return &pingService{ | ||
logger: l, | ||
db: db, | ||
} | ||
} | ||
|
||
func (p *pingService) Ping() error { | ||
if err := p.db.TenhouDB.Ping(); err != nil { | ||
p.logger.Errorf(err.Error()) | ||
return err | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.