You've already forked gun-manager-backend
Compare commits
5 Commits
5ac3032a16
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
6e6bdc5709
|
|||
|
65af98bf03
|
|||
|
42bd95d804
|
|||
|
3c6750ab13
|
|||
|
61a31a6977
|
@@ -4,13 +4,14 @@ import (
|
||||
"bytes"
|
||||
"context"
|
||||
sql2 "database/sql"
|
||||
"image/jpeg"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"git.siteworxpro.com/gun-manager/sql"
|
||||
"git.siteworxpro.com/gun-manager/sql/db"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/nfnt/resize"
|
||||
"image/jpeg"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func GetPhotoResize(c echo.Context) error {
|
||||
|
||||
@@ -1,8 +0,0 @@
|
||||
package Photo
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func Get(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
4
go.mod
4
go.mod
@@ -10,7 +10,10 @@ require (
|
||||
|
||||
require (
|
||||
github.com/dustin/go-humanize v1.0.1 // indirect
|
||||
github.com/golang-migrate/migrate/v4 v4.18.3 // indirect
|
||||
github.com/google/uuid v1.6.0 // indirect
|
||||
github.com/hashicorp/errwrap v1.1.0 // indirect
|
||||
github.com/hashicorp/go-multierror v1.1.1 // indirect
|
||||
github.com/labstack/gommon v0.4.2 // indirect
|
||||
github.com/mattn/go-colorable v0.1.14 // indirect
|
||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||
@@ -18,6 +21,7 @@ require (
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
|
||||
github.com/valyala/bytebufferpool v1.0.0 // indirect
|
||||
github.com/valyala/fasttemplate v1.2.2 // indirect
|
||||
go.uber.org/atomic v1.7.0 // indirect
|
||||
golang.org/x/crypto v0.40.0 // indirect
|
||||
golang.org/x/exp v0.0.0-20250620022241-b7579e27df2b // indirect
|
||||
golang.org/x/net v0.42.0 // indirect
|
||||
|
||||
10
go.sum
10
go.sum
@@ -5,8 +5,15 @@ github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkp
|
||||
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
|
||||
github.com/golang-jwt/jwt v3.2.2+incompatible h1:IfV12K8xAKAnZqdXVzCZ+TOjboZ2keLg81eXfW3O+oY=
|
||||
github.com/golang-jwt/jwt v3.2.2+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I=
|
||||
github.com/golang-migrate/migrate/v4 v4.18.3 h1:EYGkoOsvgHHfm5U/naS1RP/6PL/Xv3S4B/swMiAmDLs=
|
||||
github.com/golang-migrate/migrate/v4 v4.18.3/go.mod h1:99BKpIi6ruaaXRM1A77eqZ+FWPQ3cfRa+ZVy5bmWMaY=
|
||||
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
|
||||
github.com/hashicorp/errwrap v1.1.0 h1:OxrOeh75EUXMY8TBjag2fzXGZ40LB6IKw45YeGUDY2I=
|
||||
github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
|
||||
github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo=
|
||||
github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM=
|
||||
github.com/labstack/echo/v4 v4.11.1 h1:dEpLU2FLg4UVmvCGPuk/APjlH6GDpbEPti61srUUUs4=
|
||||
github.com/labstack/echo/v4 v4.11.1/go.mod h1:YuYRTSM3CHs2ybfrL8Px48bO6BAnYIN4l8wSTMP6BDQ=
|
||||
github.com/labstack/echo/v4 v4.13.4 h1:oTZZW+T3s9gAu5L8vmzihV7/lkXGZuITzTQkTEhcXEA=
|
||||
@@ -39,6 +46,7 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
|
||||
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
|
||||
@@ -47,6 +55,8 @@ github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyC
|
||||
github.com/valyala/fasttemplate v1.2.1/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ=
|
||||
github.com/valyala/fasttemplate v1.2.2 h1:lxLXG0uE3Qnshl9QyaK6XJxMXlQZELvChBOCmQD0Loo=
|
||||
github.com/valyala/fasttemplate v1.2.2/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ=
|
||||
go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw=
|
||||
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
|
||||
golang.org/x/crypto v0.12.0 h1:tFM/ta59kqch6LlvYnPa0yx5a83cL2nHflFhYKvv9Yk=
|
||||
golang.org/x/crypto v0.12.0/go.mod h1:NF0Gs7EO5K4qLn+Ylc+fih8BSTeIjAP05siRnAh98yw=
|
||||
golang.org/x/crypto v0.40.0 h1:r4x+VvoG5Fm+eJcxMaY8CQM7Lb0l1lsmjGBQ6s8BfKM=
|
||||
|
||||
65
main.go
65
main.go
@@ -2,12 +2,16 @@ package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"git.siteworxpro.com/gun-manager/Handlers/Guns"
|
||||
"git.siteworxpro.com/gun-manager/sql"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/labstack/echo/v4/middleware"
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"git.siteworxpro.com/gun-manager/Handlers/Guns"
|
||||
"git.siteworxpro.com/gun-manager/sql"
|
||||
_ "github.com/golang-migrate/migrate/v4/source/file"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/labstack/echo/v4/middleware"
|
||||
log2 "github.com/labstack/gommon/log"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -21,35 +25,68 @@ func main() {
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
_, err := sql.NewDb(dbFile)
|
||||
e := echo.New()
|
||||
e.HideBanner = true
|
||||
e.HidePort = true
|
||||
|
||||
e.Logger.SetOutput(os.Stdout)
|
||||
e.Logger.SetLevel(log2.INFO)
|
||||
|
||||
db, err := sql.NewDb(dbFile)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
e := echo.New()
|
||||
e.Use(middleware.Logger())
|
||||
e.Use(middleware.Recover())
|
||||
e.Use(middleware.CORSWithConfig(middleware.CORSConfig{
|
||||
AllowOrigins: []string{
|
||||
e.Logger.Infof("Using database file: %s", dbFile)
|
||||
e.Logger.Infof("Running migrations for database file: %s", dbFile)
|
||||
err = db.Migrate()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
e.Logger.Infof("Database file %s is ready", dbFile)
|
||||
|
||||
var corsOriginsList []string
|
||||
corsOrigins := os.Getenv("CORS_ORIGINS")
|
||||
if corsOrigins != "" {
|
||||
corsOriginsList = strings.Split(corsOrigins, ",")
|
||||
} else {
|
||||
corsOriginsList = []string{
|
||||
"https://localhost",
|
||||
"https://127.0.0.1",
|
||||
"http://127.0.0.1:5173",
|
||||
"http://localhost:5173",
|
||||
"http://127.0.0.1:4173",
|
||||
"http://127.0.0.1:8000",
|
||||
},
|
||||
AllowMethods: nil,
|
||||
}
|
||||
}
|
||||
|
||||
e.Use(middleware.Logger())
|
||||
e.Use(middleware.Recover())
|
||||
e.Use(middleware.CORSWithConfig(middleware.CORSConfig{
|
||||
AllowOrigins: corsOriginsList,
|
||||
AllowMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
|
||||
AllowHeaders: []string{"Origin", "Content-Type", "Accept"},
|
||||
}))
|
||||
|
||||
// Serve static files from the "dist" directory
|
||||
e.Static("/", "dist")
|
||||
|
||||
// Gun management routes
|
||||
e.GET("/gun", Guns.Get)
|
||||
e.PUT("/gun/:id", Guns.UpdateGun)
|
||||
e.GET("/gun/:id", Guns.GetById)
|
||||
e.DELETE("/gun/:id", Guns.DeleteById)
|
||||
e.POST("/gun", Guns.Post)
|
||||
|
||||
// Photo download routes
|
||||
e.GET("/gun/photo/:id/:filename", Guns.GetPhoto)
|
||||
e.POST("/gun/photo/:id", Guns.PostPhoto)
|
||||
e.DELETE("/gun/photo/:id", Guns.DeletePhoto)
|
||||
e.GET("/gun/photo/:id/:size/:filename", Guns.GetPhotoResize)
|
||||
|
||||
// Photo management routes
|
||||
e.POST("/gun/photo/:id", Guns.PostPhoto)
|
||||
e.DELETE("/gun/photo/:id", Guns.DeletePhoto)
|
||||
|
||||
e.Logger.Info("Starting server on port 8000")
|
||||
e.Logger.Fatal(e.Start(":8000"))
|
||||
}
|
||||
|
||||
2
migrations/000001_initial_db.down.sql
Normal file
2
migrations/000001_initial_db.down.sql
Normal file
@@ -0,0 +1,2 @@
|
||||
drop table photos;
|
||||
drop table guns;
|
||||
23
migrations/000001_initial_db.up.sql
Normal file
23
migrations/000001_initial_db.up.sql
Normal file
@@ -0,0 +1,23 @@
|
||||
create table if not exists guns
|
||||
(
|
||||
id integer
|
||||
constraint id
|
||||
primary key autoincrement,
|
||||
make text,
|
||||
model text,
|
||||
serial_number text,
|
||||
purchase_amount integer,
|
||||
value_amount integer,
|
||||
date_purchased text,
|
||||
notes text
|
||||
);
|
||||
|
||||
create table if not exists photos
|
||||
(
|
||||
id integer
|
||||
constraint id
|
||||
primary key autoincrement,
|
||||
gun_id integer,
|
||||
photo blob,
|
||||
filename text
|
||||
);
|
||||
350
openapi.yml
Normal file
350
openapi.yml
Normal file
@@ -0,0 +1,350 @@
|
||||
openapi: 3.1.0
|
||||
info:
|
||||
title: Gun API
|
||||
version: 1.0.0
|
||||
description: API for managing guns in a system.
|
||||
contact:
|
||||
name: Siteworx Pro
|
||||
url: https://siteworxpro.com
|
||||
email: websites@siteworxpro.com
|
||||
|
||||
servers:
|
||||
- url: http://localhost:8000
|
||||
description: Local Server
|
||||
|
||||
tags:
|
||||
- name: Gun Management
|
||||
description: Operations related to gun management
|
||||
- name: Photo Management
|
||||
description: Operations related to photo management
|
||||
|
||||
paths:
|
||||
/gun/{id}:
|
||||
delete:
|
||||
tags:
|
||||
- Gun Management
|
||||
operationId: deleteGunById
|
||||
description: Deletes a gun by its unique identifier.
|
||||
summary: Delete Gun by ID
|
||||
parameters:
|
||||
- name: id
|
||||
in: path
|
||||
required: true
|
||||
description: Unique identifier for the gun
|
||||
schema:
|
||||
type: string
|
||||
format: uuid
|
||||
responses:
|
||||
'404':
|
||||
description: Gun not found
|
||||
'400':
|
||||
description: Invalid ID supplied
|
||||
'204':
|
||||
description: Successfully deleted gun
|
||||
put:
|
||||
tags:
|
||||
- Gun Management
|
||||
operationId: updateGunById
|
||||
description: Updates a gun by its unique identifier.
|
||||
summary: Update Gun by ID
|
||||
parameters:
|
||||
- name: id
|
||||
in: path
|
||||
required: true
|
||||
description: Unique identifier for the gun
|
||||
schema:
|
||||
type: string
|
||||
format: uuid
|
||||
requestBody:
|
||||
required: true
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/Gun'
|
||||
responses:
|
||||
'404':
|
||||
description: Gun not found
|
||||
'400':
|
||||
description: Invalid ID supplied or invalid input data
|
||||
'200':
|
||||
description: Successfully updated gun'
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/Response'
|
||||
|
||||
get:
|
||||
tags:
|
||||
- Gun Management
|
||||
operationId: getGunById
|
||||
description: Retrieves a gun by its unique identifier.
|
||||
summary: Get Gun by ID
|
||||
parameters:
|
||||
- name: id
|
||||
in: path
|
||||
required: true
|
||||
description: Unique identifier for the gun
|
||||
schema:
|
||||
type: string
|
||||
format: uuid
|
||||
responses:
|
||||
'404':
|
||||
description: Gun not found
|
||||
'400':
|
||||
description: Invalid ID supplied
|
||||
'200':
|
||||
description: A single gun object'
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/Response'
|
||||
|
||||
/gun:
|
||||
post:
|
||||
tags:
|
||||
- Gun Management
|
||||
operationId: createGun
|
||||
description: Creates a new gun in the system.
|
||||
summary: Create a new Gun
|
||||
requestBody:
|
||||
required: true
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/Gun'
|
||||
responses:
|
||||
'201':
|
||||
description: Successfully created gun'
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/Response'
|
||||
'400':
|
||||
description: Invalid input data
|
||||
get:
|
||||
tags:
|
||||
- Gun Management
|
||||
operationId: getAllGuns
|
||||
description: Retrieves all guns in the system.
|
||||
summary: Get all Guns
|
||||
responses:
|
||||
'200':
|
||||
description: A list of guns'
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: array
|
||||
items:
|
||||
$ref: '#/components/schemas/Response'
|
||||
|
||||
/gun/photo/{id}/{filename}:
|
||||
get:
|
||||
tags:
|
||||
- Photo Management
|
||||
operationId: getGunPhotoById
|
||||
description: Retrieves a photo of a gun by its unique identifier.
|
||||
summary: Get Gun Photo by ID
|
||||
parameters:
|
||||
- name: id
|
||||
in: path
|
||||
required: true
|
||||
description: Unique identifier for the photo
|
||||
schema:
|
||||
type: string
|
||||
format: uuid
|
||||
responses:
|
||||
'404':
|
||||
description: Photo not found
|
||||
'400':
|
||||
description: Invalid ID supplied
|
||||
'200':
|
||||
description: A single photo object
|
||||
content:
|
||||
application/octet-stream:
|
||||
schema:
|
||||
type: string
|
||||
format: binary
|
||||
|
||||
/gun/photo/{id}/{size}/{filename}:
|
||||
get:
|
||||
tags:
|
||||
- Photo Management
|
||||
operationId: getGunPhotoByIdAndSize
|
||||
description: Retrieves a photo of a gun by its unique identifier and size.
|
||||
summary: Get Gun Photo by ID and Size
|
||||
parameters:
|
||||
- name: id
|
||||
in: path
|
||||
required: true
|
||||
description: Unique identifier for the photo
|
||||
schema:
|
||||
type: string
|
||||
format: uuid
|
||||
example: "uuid-1234-5678"
|
||||
- name: size
|
||||
in: path
|
||||
required: true
|
||||
description: Size of the photo
|
||||
schema:
|
||||
type: number
|
||||
example: 1024
|
||||
- name: filename
|
||||
in: path
|
||||
required: true
|
||||
description: Name of the photo file
|
||||
schema:
|
||||
type: string
|
||||
example: "gun_photo.jpg"
|
||||
responses:
|
||||
'404':
|
||||
description: Photo not found
|
||||
'400':
|
||||
description: Invalid ID or size supplied
|
||||
'200':
|
||||
description: A single photo object with specified size
|
||||
content:
|
||||
application/octet-stream:
|
||||
schema:
|
||||
type: string
|
||||
format: binary
|
||||
|
||||
/gun/photo/{id}:
|
||||
delete:
|
||||
tags:
|
||||
- Photo Management
|
||||
operationId: deleteGunPhotoById
|
||||
description: Deletes a photo of a gun by its unique identifier.
|
||||
summary: Delete Gun Photo by ID
|
||||
parameters:
|
||||
- name: id
|
||||
in: path
|
||||
required: true
|
||||
description: Unique identifier for the photo
|
||||
schema:
|
||||
type: string
|
||||
format: uuid
|
||||
responses:
|
||||
'404':
|
||||
description: Photo not found
|
||||
'400':
|
||||
description: Invalid ID supplied
|
||||
'204':
|
||||
description: Successfully deleted photo
|
||||
post:
|
||||
tags:
|
||||
- Photo Management
|
||||
operationId: uploadGunPhoto
|
||||
description: Uploads a photo for a gun.
|
||||
summary: Upload Gun Photo
|
||||
parameters:
|
||||
- name: id
|
||||
in: path
|
||||
required: true
|
||||
description: Unique identifier for the gun
|
||||
schema:
|
||||
type: string
|
||||
format: uuid
|
||||
example: "uuid-1234-5678"
|
||||
requestBody:
|
||||
required: true
|
||||
content:
|
||||
multipart/form-data:
|
||||
schema:
|
||||
type: object
|
||||
properties:
|
||||
file:
|
||||
type: string
|
||||
format: binary
|
||||
description: The photo file to upload
|
||||
responses:
|
||||
'201':
|
||||
description: Successfully uploaded photo'
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/Response'
|
||||
'400':
|
||||
description: Invalid input data or missing file
|
||||
'404':
|
||||
description: Gun not found
|
||||
|
||||
components:
|
||||
schemas:
|
||||
Photo:
|
||||
type: object
|
||||
properties:
|
||||
Id:
|
||||
readOnly: true
|
||||
type: string
|
||||
description: Unique identifier for the photo
|
||||
example: "uuid-1234-5678"
|
||||
format: uuid
|
||||
FileName:
|
||||
required: true
|
||||
type: string
|
||||
description: The name of the photo file
|
||||
example: "gun_photo.jpg"
|
||||
Gun:
|
||||
type: object
|
||||
properties:
|
||||
id:
|
||||
readOnly: true
|
||||
type: string
|
||||
description: Unique identifier for the gun
|
||||
example: "uuid-1234-5678"
|
||||
format: uuid
|
||||
Make:
|
||||
required: true
|
||||
type: string
|
||||
description: The make of the gun
|
||||
example: "Smith & Wesson"
|
||||
Model:
|
||||
required: true
|
||||
type: string
|
||||
description: The model of the gun
|
||||
example: "M&P Shield"
|
||||
PurchaseAmount:
|
||||
required: true
|
||||
type: string
|
||||
description: The purchase amount of the gun
|
||||
example: "500.00"
|
||||
ValueAmount:
|
||||
required: true
|
||||
type: string
|
||||
description: The value of the gun
|
||||
example: "10.00"
|
||||
DatePurchased:
|
||||
required: true
|
||||
type: string
|
||||
format: date
|
||||
description: The date the gun was purchased
|
||||
example: "2023-01-01"
|
||||
SerialNumber:
|
||||
required: true
|
||||
type: string
|
||||
description: The serial number of the gun
|
||||
example: "SN123456789"
|
||||
Notes:
|
||||
required: true
|
||||
type: string
|
||||
description: Additional notes about the gun
|
||||
example: "This is a test gun"
|
||||
Photos:
|
||||
readOnly: true
|
||||
type: array
|
||||
items:
|
||||
$ref: '#/components/schemas/Photo'
|
||||
|
||||
Response:
|
||||
type: object
|
||||
properties:
|
||||
status:
|
||||
type: string
|
||||
description: The status of the response
|
||||
data:
|
||||
type: object
|
||||
oneOf:
|
||||
- $ref: '#/components/schemas/Gun'
|
||||
- type: array
|
||||
items:
|
||||
$ref: '#/components/schemas/Gun'
|
||||
@@ -3,7 +3,10 @@ package sql
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
sqlc "git.siteworxpro.com/gun-manager/sql/db"
|
||||
"github.com/golang-migrate/migrate/v4"
|
||||
"github.com/golang-migrate/migrate/v4/database/sqlite3"
|
||||
|
||||
_ "modernc.org/sqlite"
|
||||
)
|
||||
@@ -42,3 +45,22 @@ func NewDb(file string) (*Db, error) {
|
||||
func GetDb() *Db {
|
||||
return &dbConnection
|
||||
}
|
||||
|
||||
func (d *Db) Migrate() error {
|
||||
i, err := sqlite3.WithInstance(dbConnection.db, &sqlite3.Config{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
m, err := migrate.NewWithDatabaseInstance("file://migrations", "sqlite3", i)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = m.Up()
|
||||
if err != nil && !errors.Is(err, migrate.ErrNoChange) {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user