Skip to content

Commit

Permalink
feat: add api server for database
Browse files Browse the repository at this point in the history
  • Loading branch information
gsxhnd committed Sep 29, 2024
1 parent 8b52ecb commit 696cf44
Show file tree
Hide file tree
Showing 16 changed files with 451 additions and 8 deletions.
19 changes: 16 additions & 3 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ builds:
- arm64

archives:
- format: tar.gz
allow_different_binary_count: true
# this name template makes the OS and Arch compatible with the results of uname.
- id: garage
builds: [garage]
format: tar.gz
name_template: >-
{{ .ProjectName }}_
{{- title .Os }}_
Expand All @@ -69,6 +69,19 @@ archives:
files:
- "LICENSE"
- "CHANGELOG.md"
- id: garage_server
builds: [garage_server]
format: tar.gz
name_template: >-
garage_server_
{{- title .Os }}_
{{- if eq .Arch "amd64" }}x86_64
{{- else if eq .Arch "386" }}i386
{{- else }}{{ .Arch }}{{ end }}
{{- if .Arm }}v{{ .Arm }}{{ end }}
files:
- "LICENSE"
- "CHANGELOG.md"

checksum:
name_template: 'checksums.txt'
Expand Down
48 changes: 48 additions & 0 deletions garage_server/db/init.go
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)
30 changes: 30 additions & 0 deletions garage_server/di/app.go
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
}
}
28 changes: 28 additions & 0 deletions garage_server/di/wire.go
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
}
42 changes: 42 additions & 0 deletions garage_server/di/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions garage_server/handler/init.go
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), "*"),
)
33 changes: 33 additions & 0 deletions garage_server/handler/ping.go
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")
}
12 changes: 10 additions & 2 deletions garage_server/main.go
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)
}
}
18 changes: 18 additions & 0 deletions garage_server/middleware/init.go
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,
}
}
58 changes: 58 additions & 0 deletions garage_server/router/init.go
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)
}
7 changes: 7 additions & 0 deletions garage_server/service/init.go
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,
)
31 changes: 31 additions & 0 deletions garage_server/service/ping.go
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
}
Loading

0 comments on commit 696cf44

Please sign in to comment.