implement db

This commit is contained in:
0xADB 2025-03-06 23:28:53 +01:00
parent 73fc9a4908
commit 5b5a62282b
8 changed files with 116 additions and 98 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
test.db

View File

@ -39,7 +39,7 @@ type NewPet struct {
// Pet defines model for Pet. // Pet defines model for Pet.
type Pet struct { type Pet struct {
// Id Unique id of the pet // Id Unique id of the pet
Id int64 `json:"id"` Id int64 `gorm:"primarykey" json:"id"`
// Name Name of the pet // Name Name of the pet
Name string `json:"name"` Name string `json:"name"`
@ -344,34 +344,34 @@ func HandlerWithOptions(si ServerInterface, options ChiServerOptions) http.Handl
// Base64 encoded, gzipped, json marshaled Swagger object // Base64 encoded, gzipped, json marshaled Swagger object
var swaggerSpec = []string{ var swaggerSpec = []string{
"H4sIAAAAAAAC/+RXW48budH9KwV+32OnNbEXedBTvB4vICBrT+LdvKznoYZdkmrBSw9Z1FgY6L8HRbZu", "H4sIAAAAAAAC/+RXW48budH9KwV+32NPa2Iv8qCneD1eQEDWnsS7eVn7oYZdkmrDm8miZgRD/z0odus2",
"I3k2QYIgQV506WY1T51zqlj9bGz0YwwUJJv5s8l2TR7rzw8pxaQ/xhRHSsJUL9s4kH4PlG3iUTgGM2+L", "kmcTJAgS5EWXbrJ56pzD4umvxkafYqAgxcy/mmLX5LH9fJdzzPoj5ZgoC1O7bONA+j1QsZmTcAxmPg6G",
"od7rzDImj2LmhoO8fWM6I9uR2l9aUTK7znjKGVfffND+9iE0S+KwMrtdZxI9Fk40mPkvZtpwv/x+15mP", "dq8zy5g9ipkbDvL6lemMbBONf2lF2ew646kUXH3zQfvbh6lFMoeV2e06k+lL5UyDmf9ipgX3wz/vOvOe",
"9HRHcok7oL+y3Uf0BHEJsiYYSS437Izg6jLup+34etwLoHV3hTdhQ+c+Lc38l2fz/4mWZm7+b3YUYjap", "Hu9JLnEH9FeWe4+eIC5B1gSJ5HLBzgiuLuf9tE0vz3sGtK2u8CZs6NyHpZn/8tX8f6almZv/mx2Fm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nswYzxaYnXyD+F5Ccb4cnBl4RroKWlr1R48julMqV6T4pUAodhSLFeixttMKC2z",
"ZH1Jwx2JegyHQb8OsM3plCyp0O6f9MxvWuW/xxoXgtf7dR6dPfOwaxZxJFdewNp1jc0cVq6+tcADapuN", "BXrUsfsw1nKNnsEjdh2iec65+EjDhVnfDOpVM6ZTKvJ9HLb/Mhb2+fyShnsS9RgOg34dYJvTlCy50u6f",
"zTWLW8hFc7rikdsa3Wzyakdb3GoPGZu2E5apf+gAfWwfPFwo/a1ecv1t6rKXfHeZtQJpKIb/JCFvD2JU", "9MxvWuW/xxoXgrf7LY/OvvKwGy3iSK68xo3XdW7hsHLt3QceUNtsHF2zuINStaYrHrlrs0ebvNjRFnfa",
"FbawuFV4r79QnCt20HFx+63j5/ttvff367Ukset/m1z/s2X8QtGmfl1CabOX6fyteP9S3p+82err6e5+", "Q9Ko7YRl6h8aoI/tg4cLpb/VS669k13rJd9dVq1ARhTDf5KQdwcxmgpbWNwpvJdfKM4VO+i4uPvW8fP9",
"97cAAAD//ykDnxlaEgAA", "tt37+/Vaktj1v02u/9lt/EzRUf02hPJmL9P5W/H+pbw/ebPV19Pd593fAgAA//+hlBGVoBIAAA==",
} }
// GetSwagger returns the content of the embedded swagger specification file // GetSwagger returns the content of the embedded swagger specification file

View File

@ -4,26 +4,17 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"net/http" "net/http"
"sync"
"gorm.io/gorm"
) )
type PetStore struct { type PetStore struct {
Pets map[int64]Pet DB *gorm.DB
NextId int64
Lock sync.Mutex
} }
// Make sure we conform to ServerInterface // Make sure we conform to ServerInterface
var _ ServerInterface = (*PetStore)(nil) var _ ServerInterface = (*PetStore)(nil)
func NewPetStore() *PetStore {
return &PetStore{
Pets: make(map[int64]Pet),
NextId: 1000,
}
}
// sendPetStoreError wraps sending of an error in the Error format, and // sendPetStoreError wraps sending of an error in the Error format, and
// handling the failure to marshal that. // handling the failure to marshal that.
func sendPetStoreError(w http.ResponseWriter, code int, message string) { func sendPetStoreError(w http.ResponseWriter, code int, message string) {
@ -37,35 +28,24 @@ func sendPetStoreError(w http.ResponseWriter, code int, message string) {
// FindPets implements all the handlers in the ServerInterface // FindPets implements all the handlers in the ServerInterface
func (p *PetStore) FindPets(w http.ResponseWriter, r *http.Request, params FindPetsParams) { func (p *PetStore) FindPets(w http.ResponseWriter, r *http.Request, params FindPetsParams) {
p.Lock.Lock() var pets []Pet
defer p.Lock.Unlock() query := p.DB
var result []Pet if params.Tags != nil {
query = query.Where("tag IN ?", *params.Tags)
}
for _, pet := range p.Pets { if params.Limit != nil {
if params.Tags != nil { query = query.Limit(int(*params.Limit))
// If we have tags, filter pets by tag }
for _, t := range *params.Tags {
if pet.Tag != nil && (*pet.Tag == t) {
result = append(result, pet)
}
}
} else {
// Add all pets if we're not filtering
result = append(result, pet)
}
if params.Limit != nil { if err := query.Find(&pets).Error; err != nil {
l := int(*params.Limit) sendPetStoreError(w, http.StatusInternalServerError, "Database error")
if len(result) >= l { return
// We're at the limit
break
}
}
} }
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
_ = json.NewEncoder(w).Encode(result) _ = json.NewEncoder(w).Encode(pets)
} }
func (p *PetStore) AddPet(w http.ResponseWriter, r *http.Request) { func (p *PetStore) AddPet(w http.ResponseWriter, r *http.Request) {
@ -76,21 +56,15 @@ func (p *PetStore) AddPet(w http.ResponseWriter, r *http.Request) {
return return
} }
// We now have a pet, let's add it to our "database". pet := Pet{
Name: newPet.Name,
Tag: newPet.Tag,
}
// We're always asynchronous, so lock unsafe operations below if err := p.DB.Create(&pet).Error; err != nil {
p.Lock.Lock() sendPetStoreError(w, http.StatusInternalServerError, "Failed to create pet")
defer p.Lock.Unlock() return
}
// We handle pets, not NewPets, which have an additional ID field
var pet Pet
pet.Name = newPet.Name
pet.Tag = newPet.Tag
pet.Id = p.NextId
p.NextId++
// Insert into map
p.Pets[pet.Id] = pet
// Now, we have to return the NewPet // Now, we have to return the NewPet
w.WriteHeader(http.StatusCreated) w.WriteHeader(http.StatusCreated)
@ -98,12 +72,9 @@ func (p *PetStore) AddPet(w http.ResponseWriter, r *http.Request) {
} }
func (p *PetStore) FindPetByID(w http.ResponseWriter, r *http.Request, id int64) { func (p *PetStore) FindPetByID(w http.ResponseWriter, r *http.Request, id int64) {
p.Lock.Lock() var pet Pet
defer p.Lock.Unlock() if err := p.DB.First(&pet, id).Error; err != nil {
sendPetStoreError(w, http.StatusNotFound, fmt.Sprintf("Pet with ID %d not found", id))
pet, found := p.Pets[id]
if !found {
sendPetStoreError(w, http.StatusNotFound, fmt.Sprintf("Could not find pet with ID %d", id))
return return
} }
@ -112,15 +83,10 @@ func (p *PetStore) FindPetByID(w http.ResponseWriter, r *http.Request, id int64)
} }
func (p *PetStore) DeletePet(w http.ResponseWriter, r *http.Request, id int64) { func (p *PetStore) DeletePet(w http.ResponseWriter, r *http.Request, id int64) {
p.Lock.Lock() if err := p.DB.Delete(&Pet{}, id).Error; err != nil {
defer p.Lock.Unlock() sendPetStoreError(w, http.StatusNotFound, fmt.Sprintf("Pet with ID %d not found", id))
_, found := p.Pets[id]
if !found {
sendPetStoreError(w, http.StatusNotFound, fmt.Sprintf("Could not find pet with ID %d", id))
return return
} }
delete(p.Pets, id)
w.WriteHeader(http.StatusNoContent) w.WriteHeader(http.StatusNoContent)
} }

View File

@ -138,6 +138,9 @@ components:
type: integer type: integer
format: int64 format: int64
description: Unique id of the pet description: Unique id of the pet
x-go-type: int64
x-oapi-codegen-extra-tags:
gorm: primarykey
NewPet: NewPet:
required: required:
@ -146,9 +149,15 @@ components:
name: name:
type: string type: string
description: Name of the pet description: Name of the pet
# x-go-type:
# type: string
# structTags: 'json:"name"'
tag: tag:
type: string type: string
description: Type of the pet description: Type of the pet
# x-go-type:
# type: "*string"
# structTags: 'json:"tag,omitempty"'
Error: Error:
required: required:

26
db/db.go Normal file
View File

@ -0,0 +1,26 @@
package db
import (
"log"
"example/hello/api"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
// ConnectDB initializes the database connection
func ConnectDB() *gorm.DB {
db, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{})
if err != nil {
panic("failed to connect database")
}
log.Println("Database connected successfully")
return db
}
func Migrate(db *gorm.DB) {
db.AutoMigrate(&api.Pet{})
log.Println("Database migrated successfully")
}

6
go.mod
View File

@ -12,12 +12,18 @@ require (
github.com/go-openapi/swag v0.23.0 // indirect github.com/go-openapi/swag v0.23.0 // indirect
github.com/google/uuid v1.5.0 // indirect github.com/google/uuid v1.5.0 // indirect
github.com/gorilla/mux v1.8.1 // indirect github.com/gorilla/mux v1.8.1 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/josharian/intern v1.0.0 // indirect github.com/josharian/intern v1.0.0 // indirect
github.com/mailru/easyjson v0.7.7 // indirect github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-sqlite3 v1.14.24 // indirect
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect
github.com/oapi-codegen/runtime v1.1.1 // indirect github.com/oapi-codegen/runtime v1.1.1 // indirect
github.com/oasdiff/yaml v0.0.0-20241210131133-6b86fb107d80 // indirect github.com/oasdiff/yaml v0.0.0-20241210131133-6b86fb107d80 // indirect
github.com/oasdiff/yaml3 v0.0.0-20241210130736-a94c01f36349 // indirect github.com/oasdiff/yaml3 v0.0.0-20241210130736-a94c01f36349 // indirect
github.com/perimeterx/marshmallow v1.1.5 // indirect github.com/perimeterx/marshmallow v1.1.5 // indirect
golang.org/x/text v0.23.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect
gorm.io/driver/sqlite v1.5.7 // indirect
gorm.io/gorm v1.25.12 // indirect
) )

12
go.sum
View File

@ -15,11 +15,17 @@ github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU=
github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY= github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY=
github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ= github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ=
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY= github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y= github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
github.com/juju/gnuflag v0.0.0-20171113085948-2ce1bb71843d/go.mod h1:2PavIy+JPciBPrBUjwbNvtwB6RQlve+hkpll6QSNmOE= github.com/juju/gnuflag v0.0.0-20171113085948-2ce1bb71843d/go.mod h1:2PavIy+JPciBPrBUjwbNvtwB6RQlve+hkpll6QSNmOE=
github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0= github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc= github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
github.com/mattn/go-sqlite3 v1.14.24 h1:tpSp2G2KyMnnQu99ngJ47EIkWVmliIizyZBfPrBWDRM=
github.com/mattn/go-sqlite3 v1.14.24/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 h1:RWengNIwukTxcDr9M+97sNutRR1RKhG96O6jWumTTnw= github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 h1:RWengNIwukTxcDr9M+97sNutRR1RKhG96O6jWumTTnw=
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826/go.mod h1:TaXosZuwdSHYgviHp1DAtfrULt5eUgsSMsZf+YrPgl8= github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826/go.mod h1:TaXosZuwdSHYgviHp1DAtfrULt5eUgsSMsZf+YrPgl8=
github.com/oapi-codegen/nethttp-middleware v1.0.2 h1:A5tfAcKJhWIbIPnlQH+l/DtfVE1i5TFgPlQAiW+l1vQ= github.com/oapi-codegen/nethttp-middleware v1.0.2 h1:A5tfAcKJhWIbIPnlQH+l/DtfVE1i5TFgPlQAiW+l1vQ=
@ -36,6 +42,12 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/spkg/bom v0.0.0-20160624110644-59b7046e48ad/go.mod h1:qLr4V1qq6nMqFKkMo8ZTx3f+BZEkzsRUY10Xsm2mwU0= github.com/spkg/bom v0.0.0-20160624110644-59b7046e48ad/go.mod h1:qLr4V1qq6nMqFKkMo8ZTx3f+BZEkzsRUY10Xsm2mwU0=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= 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.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
golang.org/x/text v0.23.0 h1:D71I7dUrlY+VX0gQShAThNGHFxZ13dGLBHQLVl1mJlY=
golang.org/x/text v0.23.0/go.mod h1:/BLNzu4aZCJ1+kcD0DNRotWKage4q2rGVAg4o22unh4=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gorm.io/driver/sqlite v1.5.7 h1:8NvsrhP0ifM7LX9G4zPB97NwovUakUxc+2V2uuf3Z1I=
gorm.io/driver/sqlite v1.5.7/go.mod h1:U+J8craQU6Fzkcvu8oLeAQmi50TkwPEhHDEjQZXDah4=
gorm.io/gorm v1.25.12 h1:I0u8i2hWQItBq1WfE0o2+WuL9+8L21K9e2HHSTE/0f8=
gorm.io/gorm v1.25.12/go.mod h1:xh7N7RHfYlNc5EmcI/El95gXusucDrQnHXe0+CgWcLQ=

12
main.go
View File

@ -9,6 +9,7 @@ import (
"os" "os"
"example/hello/api" "example/hello/api"
"example/hello/db"
"github.com/go-chi/chi/v5" "github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware" "github.com/go-chi/chi/v5/middleware"
@ -19,6 +20,9 @@ func main() {
port := flag.String("port", "3000", "Port for test HTTP server") port := flag.String("port", "3000", "Port for test HTTP server")
flag.Parse() flag.Parse()
DB := db.ConnectDB()
db.Migrate(DB)
swagger, err := api.GetSwagger() swagger, err := api.GetSwagger()
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "Error loading swagger spec\n: %s", err) fmt.Fprintf(os.Stderr, "Error loading swagger spec\n: %s", err)
@ -26,17 +30,11 @@ func main() {
} }
swagger.Servers = nil swagger.Servers = nil
petStore := api.NewPetStore()
r := chi.NewRouter() r := chi.NewRouter()
r.Use(m.OapiRequestValidator(swagger)) r.Use(m.OapiRequestValidator(swagger))
r.Use(middleware.Logger) r.Use(middleware.Logger)
// r.Get("/", func(w http.ResponseWriter, r *http.Request) {
// w.Write([]byte("Hello World!"))
// })
petStore := &api.PetStore{DB: DB}
api.HandlerFromMux(petStore, r) api.HandlerFromMux(petStore, r)
s := &http.Server{ s := &http.Server{