Merge pull request #12 from rodneyosodo/foodApiDocker

This commit is contained in:
b1ackd0t
2023-06-28 14:18:54 +03:00
committed by GitHub
8 changed files with 328 additions and 31 deletions
+102 -1
View File
@@ -1 +1,102 @@
# uber4freefood
# uber4freefood
## Food Api
###### Running
Clone files into go src directory
run `docker-compose up`
###### port `*:8080`
###### Api Routes
`GET /food`
This return all the records from the table
```
{
"data": [
{
"id": 1,
"foodName": "",
"dietType": "",
"description": "",
"specialIngridients": "",
"serving": "",
"SpecialNote": "",
"foodImageId": "",
"locationLat": "",
"locationLong": "",
"status": ""
}
]
}
```
`POST /food`
This is used to create a record
```
{
"data": [
{
"foodName": "",
"dietType": "",
"description": "",
"specialIngridients": "",
"serving": "",
"SpecialNote": "",
"foodImageId": "",
"locationLat": "",
"locationLong": "",
"status": ""
}
]
}
```
`GET /food/:id`
return specific record from given id
```
{
"data": [
{
"foodName": "",
"dietType": "",
"description": "",
"specialIngridients": "",
"serving": "",
"SpecialNote": "",
"foodImageId": "",
"locationLat": "",
"locationLong": "",
"status": ""
}
]
}
```
PATCH /food/:id
Edit staus for food
`{"Status": ""}`
returns
```
{
"data": [
{
"id": 1,
"foodName": "",
"dietType": "",
"description": "",
"specialIngridients": "",
"serving": "",
"SpecialNote": "",
"foodImageId": "",
"locationLat": "",
"locationLong": "",
"status": ""
}
]
}
```
`DELETE /food/:id`
delete specified record
```
{
"data": true
}
```
+27
View File
@@ -0,0 +1,27 @@
FROM golang@sha256:0991060a1447cf648bab7f6bb60335d1243930e38420bee8fec3db1267b84cfa as builder
ENV GO111MODULE=on
RUN apk update && apk add --no-cache git ca-certificates tzdata && update-ca-certificates
ENV USER=appuser
ENV UID=10001
RUN adduser \
--disabled-password \
--gecos "" \
--home "/nonexistent" \
--shell "/sbin/nologin" \
--no-create-home \
--uid "${UID}" \
"${USER}"
WORKDIR $GOPATH/src/mypackage/myapp/
COPY . .
RUN go mod vendor
RUN ls
RUN CGO_ENABLED=0 GOOS=linux go build -ldflags="-w -s" -o /go/bin/hello -mod vendor main.go
FROM scratch
COPY --from=builder /usr/share/zoneinfo /usr/share/zoneinfo
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
COPY --from=builder /etc/passwd /etc/passwd
COPY --from=builder /etc/group /etc/group
COPY --from=builder /go/bin/hello /go/bin/hello
USER appuser:appuser
ENTRYPOINT ["/go/bin/hello"]
EXPOSE 8080
+113
View File
@@ -0,0 +1,113 @@
# uber4freefood
## Food Api
###### Running
Clone files into go src directory
run `docker-compose up`
###### port `*:8080`
###### Api Routes
`GET /food`
This return all the records from the table
```
{
"data": [
{
"id": 1,
"foodName": "",
"dietType": "",
"description": "",
"specialIngridients": "",
"serving": "",
"SpecialNote": "",
"foodImageId": "",
"locationLat": "",
"locationLong": "",
"angelUserID": "",
"humanUserID": "",
"status": "",
"CreatedAt": "",
"UpdatedAt": "",
"DeletedAt": ""
}
]
}
```
`POST /food`
This is used to create a record
```
{
"data": [
{
"foodName": "",
"dietType": "",
"description": "",
"specialIngridients": "",
"serving": "",
"SpecialNote": "",
"foodImageId": "",
"locationLat": "",
"locationLong": "",
"angelUserID": "",
"humanUserID": "",
"status": ""
}
]
}
```
`GET /food/:id`
return specific record from given id
```
{
"data": [
{
"foodName": "",
"dietType": "",
"description": "",
"specialIngridients": "",
"serving": "",
"SpecialNote": "",
"foodImageId": "",
"locationLat": "",
"locationLong": "",
"angelUserID": "",
"humanUserID": "",
"status": ""
}
]
}
```
PATCH /food/:id
Edit staus for food
`{"Status": ""}`
returns
```
{
"data": [
{
"id": 1,
"foodName": "",
"dietType": "",
"description": "",
"specialIngridients": "",
"serving": "",
"SpecialNote": "",
"foodImageId": "",
"locationLat": "",
"locationLong": "",
"angelUserID": "",
"humanUserID": "",
"status": ""
}
]
}
```
`DELETE /food/:id`
delete specified record
```
{
"data": true
}
```
+1 -1
View File
@@ -26,7 +26,7 @@ func CreateFood(c *gin.Context) {
return
}
//create food post
food := models.Food{FoodName: input.FoodName, DietType: input.DietType, Description: input.Description, SpecialIngridients: input.SpecialIngridients, Serving: input.Serving, SpecialNote: input.SpecialNote, FoodImageId: input.FoodImageId, LocationLat: input.LocationLat, LocationLong: input.LocationLong, Status: input.Status}
food := models.Food{FoodName: input.FoodName, DietType: input.DietType, Description: input.Description, SpecialIngridients: input.SpecialIngridients, Serving: input.Serving, SpecialNote: input.SpecialNote, FoodImageId: input.FoodImageId, LocationLat: input.LocationLat, LocationLong: input.LocationLong, AngelUserID:input.AngelUserID, HumanUserID: input.HumanUserID, Status: input.Status}
db.Create(&food)
c.JSON(http.StatusOK, gin.H{"data": food})
}
+5
View File
@@ -0,0 +1,5 @@
POSTGRES_DB=postgres
POSTGRES_PASSWORD=postgres
POSTGRES_USER=postgres
POSTGRES_HOST=db
POSTGRES_PORT=5432
+30
View File
@@ -0,0 +1,30 @@
version: '3.7'
volumes:
database_data:
driver: local
services:
db:
image: 'postgres:latest' # use latest official postgres version
ports:
- '5432:5432'
expose:
- 5432
environment:
POSTGRES_DB: postgres
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
volumes:
- database_data:/var/lib/postgresql/data
web:
build: .
ports:
- '8080:8080'
environment:
POSTGRES_HOST: db
POSTGRES_PORT: 5432
env_file:
- database.env # configure postgres
links:
- db
+43 -23
View File
@@ -1,30 +1,50 @@
package models
import "time"
type Food struct {
ID uint `json:"id" gorm:"primary_key"`
FoodName string `json:"foodName"`
DietType string `json:"dietType"`
Description string `json:"description"`
SpecialIngridients string `json:"specialIngridients"`
Serving string `json:"serving"`
SpecialNote string `json:specialNote"`
FoodImageId string `json:"foodImageId"`
LocationLat string `json:"locationLat"`
LocationLong string `json:"locationLong"`
Status string `json:"status"`
ID uint `json:"id" gorm:"primary_key"`
FoodName string `json:"foodName"`
DietType string `json:"dietType"`
Description string `json:"description"`
SpecialIngridients string `json:"specialIngridients"`
Serving string `json:"serving"`
SpecialNote string `json:"specialNote"`
FoodImageId string `json:"foodImageId"`
LocationLat string `json:"locationLat"`
LocationLong string `json:"locationLong"`
AngelUserID string `json:"angelUserID"`
HumanUserID string `json:"humanUserID"`
Status string `json:"status"`
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt *time.Time
}
type CreateFoodInput struct {
FoodName string `json:"foodName" binding:"required"`
DietType string `json:"dietType"`
Description string `json:"description" binding:"required"`
SpecialIngridients string `json:"specialIngridients"`
Serving string `json:"serving"`
SpecialNote string `json:specialNote"`
FoodImageId string `json:"foodImageId"`
LocationLat string `json:"locationLat" binding:"required"`
LocationLong string `json:"locationLong" binding:"required"`
Status string `json:"status"`
FoodName string `json:"foodName" binding:"required"`
DietType string `json:"dietType"`
Description string `json:"description" binding:"required"`
SpecialIngridients string `json:"specialIngridients"`
Serving string `json:"serving"`
SpecialNote string `json:"specialNote"`
FoodImageId string `json:"foodImageId"`
LocationLat string `json:"locationLat" binding:"required"`
LocationLong string `json:"locationLong" binding:"required"`
AngelUserID string `json:"angelUserID"`
HumanUserID string `json:"humanUserID"`
Status string `json:"status"`
}
type UpdateStatusInput struct {
Status string `json:"status"`
}
FoodName string `json:"foodName"`
DietType string `json:"dietType"`
Description string `json:"description"`
SpecialIngridients string `json:"specialIngridients"`
Serving string `json:"serving"`
SpecialNote string `json:"specialNote"`
FoodImageId string `json:"foodImageId"`
LocationLat string `json:"locationLat"`
LocationLong string `json:"locationLong"`
AngelUserID string `json:"angelUserID"`
HumanUserID string `json:"humanUserID"`
Status string `json:"status"`
}
+7 -6
View File
@@ -3,16 +3,17 @@ package models
"fmt"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/postgres"
//"github.com/spf13/viper"
"github.com/spf13/viper"
)
func SetupModels () *gorm.DB {
viper.AutomaticEnv()
//read .env file
viper_user := "postgres" //viper.Get("POSTGRES_USER")
viper_password := "#Avrilla8" //viper.Get("POSTGRES_PASSWORD")
viper_db := "foodApi" //viper.Get("POSTGRES_DB")
viper_host := "localhost" //viper.Get("POSTGRES_HOST")
viper_port := "5432" //viper.Get("POSTGRES_PORT")
viper_user := viper.Get("POSTGRES_USER")
viper_password := viper.Get("POSTGRES_PASSWORD")
viper_db := viper.Get("POSTGRES_DB")
viper_host := viper.Get("POSTGRES_HOST")
viper_port := viper.Get("POSTGRES_PORT")
//formart
prosgret_conname := fmt.Sprintf("host=%v port=%v user=%v dbname=%v password=%v sslmode=disable", viper_host, viper_port, viper_user, viper_db, viper_password)
fmt.Println("conname is\t\t", prosgret_conname)