Compare commits

..

6 Commits

10 changed files with 496 additions and 29 deletions

View File

@@ -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 {

View File

@@ -1,8 +0,0 @@
package Photo
import (
"net/http"
)
func Get(w http.ResponseWriter, r *http.Request) {
}

15
go.mod
View File

@@ -2,8 +2,6 @@ module git.siteworxpro.com/gun-manager
go 1.24.4
toolchain go1.24.4
require (
github.com/labstack/echo/v4 v4.13.4
github.com/mattn/go-sqlite3 v1.14.29
@@ -11,14 +9,27 @@ 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
github.com/ncruces/go-strftime v0.1.9 // indirect
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
golang.org/x/sys v0.34.0 // indirect
golang.org/x/text v0.27.0 // indirect
golang.org/x/time v0.12.0 // indirect
modernc.org/libc v1.66.3 // indirect
modernc.org/mathutil v1.7.1 // indirect
modernc.org/memory v1.11.0 // indirect
modernc.org/sqlite v1.38.1 // indirect
)

28
go.sum
View File

@@ -1,8 +1,19 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
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=
@@ -26,11 +37,16 @@ github.com/mattn/go-sqlite3 v1.14.17 h1:mCRHCLDUBXgpKAqIKsaAaAsrAlbkeomtRFKXh2L6
github.com/mattn/go-sqlite3 v1.14.17/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
github.com/mattn/go-sqlite3 v1.14.29 h1:1O6nRLJKvsi1H2Sj0Hzdfojwt8GiGKm+LOfLaBFaouQ=
github.com/mattn/go-sqlite3 v1.14.29/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
github.com/ncruces/go-strftime v0.1.9 h1:bY0MQC28UADQmHmaF5dgpLmImcShSi2kHU9XLdhx/f4=
github.com/ncruces/go-strftime v0.1.9/go.mod h1:Fwc5htZGVVkseilnfgOVb9mKy6w1naJmn9CehxcKcls=
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 h1:zYyBkD/k9seD2A7fsi6Oo2LfFZAehjjQMERAvZLEDnQ=
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646/go.mod h1:jpp1/29i3P1S/RLdc7JQKbRpFeM1dOBd8T9ki5s+AY8=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
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=
@@ -39,10 +55,14 @@ 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=
golang.org/x/crypto v0.40.0/go.mod h1:Qr1vMER5WyS2dfPHAlsOj01wgLbsyWtFn/aY+5+ZdxY=
golang.org/x/exp v0.0.0-20250620022241-b7579e27df2b h1:M2rDM6z3Fhozi9O7NWsxAkg/yqS/lQJ6PmkyIV3YP+o=
golang.org/x/exp v0.0.0-20250620022241-b7579e27df2b/go.mod h1:3//PLf8L/X+8b4vuAfHzxeRUl04Adcb341+IGKfnqS8=
golang.org/x/net v0.14.0 h1:BONx9s002vGdD9umnlX1Po8vOZmrgH34qlHcD1MfK14=
golang.org/x/net v0.14.0/go.mod h1:PpSgVXXLK0OxS0F31C1/tv6XNguvCrnXIDrFMspZIUI=
golang.org/x/net v0.42.0 h1:jzkYrhi3YQWD6MLBJcsklgQsoAcw89EcZbJw8Z614hs=
@@ -69,3 +89,11 @@ gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
modernc.org/libc v1.66.3 h1:cfCbjTUcdsKyyZZfEUKfoHcP3S0Wkvz3jgSzByEWVCQ=
modernc.org/libc v1.66.3/go.mod h1:XD9zO8kt59cANKvHPXpx7yS2ELPheAey0vjIuZOhOU8=
modernc.org/mathutil v1.7.1 h1:GCZVGXdaN8gTqB1Mf/usp1Y/hSqgI2vAGGP4jZMCxOU=
modernc.org/mathutil v1.7.1/go.mod h1:4p5IwJITfppl0G4sUEDtCr4DthTaT47/N3aT6MhfgJg=
modernc.org/memory v1.11.0 h1:o4QC8aMQzmcwCK3t3Ux/ZHmwFPzE6hf2Y5LbkRs+hbI=
modernc.org/memory v1.11.0/go.mod h1:/JP4VbVC+K5sU2wZi9bHoq2MAkCnrt2r98UGeSK7Mjw=
modernc.org/sqlite v1.38.1 h1:jNnIjleVta+DKSAr3TnkKK87EEhjPhBLzi6hvIX9Bas=
modernc.org/sqlite v1.38.1/go.mod h1:cPTJYSlgg3Sfg046yBShXENNtPrWrDX8bsbAQBzgQ5E=

65
main.go
View File

@@ -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"))
}

View File

@@ -0,0 +1,2 @@
drop table photos;
drop table guns;

View 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
View 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'

View File

@@ -3,8 +3,12 @@ package sql
import (
"context"
"database/sql"
"errors"
sqlc "git.siteworxpro.com/gun-manager/sql/db"
_ "github.com/mattn/go-sqlite3"
"github.com/golang-migrate/migrate/v4"
"github.com/golang-migrate/migrate/v4/database/sqlite3"
_ "modernc.org/sqlite"
)
type Db struct {
@@ -20,7 +24,7 @@ func NewDb(file string) (*Db, error) {
return &dbConnection, nil
}
db, err := sql.Open("sqlite3", file)
db, err := sql.Open("sqlite", file)
if err != nil {
return nil, err
}
@@ -41,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
}