Skip to content

Commit 459c8b9

Browse files
committed
2 parents f8c14c3 + 7cd6647 commit 459c8b9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

93 files changed

+1166
-7207
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.env

Dockerfile

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
FROM heroku/heroku:18-build as build
2+
3+
COPY . /app
4+
WORKDIR /app
5+
6+
# Setup buildpack
7+
RUN mkdir -p /tmp/buildpack/heroku/go /tmp/build_cache /tmp/env
8+
RUN curl https://codon-buildpacks.s3.amazonaws.com/buildpacks/heroku/go.tgz | tar xz -C /tmp/buildpack/heroku/go
9+
10+
#Execute Buildpack
11+
RUN STACK=heroku-18 /tmp/buildpack/heroku/go/bin/compile /app /tmp/build_cache /tmp/env
12+
13+
# Prepare final, minimal image
14+
FROM heroku/heroku:18
15+
16+
COPY --from=build /app /app
17+
ENV HOME /app
18+
WORKDIR /app
19+
RUN useradd -m heroku
20+
USER heroku
21+
CMD /app/bin/openterps

Makefile

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
GO_BUILD_ENV := CGO_ENABLED=0 GOOS=linux GOARCH=amd64
2+
DOCKER_BUILD=$(shell pwd)/.docker_build
3+
DOCKER_CMD=$(DOCKER_BUILD)/go-getting-started
4+
5+
$(DOCKER_CMD): clean
6+
mkdir -p $(DOCKER_BUILD)
7+
$(GO_BUILD_ENV) go build -v -o $(DOCKER_CMD) .
8+
9+
clean:
10+
rm -rf $(DOCKER_BUILD)
11+
12+
heroku: $(DOCKER_CMD)
13+
heroku container:push web
14+
15+
build:
16+
export GO111MODULE=on
17+
env GOOS=linux GOARCH=amd64 go build -o bin/openterps -v .

Procfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
web: bin/openterps

README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# OpenTerps 🍁
2+
3+
An open source API to categorize cannabis and it's terpenes/effects/tastes/smells.
4+
5+
[docs](https://app.swaggerhub.com/apis-docs/CountryFriedCoders/OpenTerps/0.0.2)
6+
7+
```
8+
|
9+
|.|
10+
|\./|
11+
|\./|
12+
. |\./| .
13+
\^.\ |\\.//| /.^/
14+
\--.|\ |\\.//| /|.--/
15+
\--.| \ |\\.//| / |.--/
16+
\---.|\ |\./| /|.---/
17+
\--.|\ |\./| /|.--/
18+
\ .\ |.| /. /
19+
_ -_^_^_^_- \ \\ // / -_^_^_^_- _
20+
- -/_/_/- ^ ^ | ^ ^ -\_\_\- -
21+
/-./ | \.-\
22+
/-/ | \-\
23+
;|' '|;
24+
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
25+
.---. ,---. ,---. .-. .-. _______ ,---. ,---. ,---. .---. ,--, ,-. ,-.
26+
/ .-. ) | .-.\ | .-' | \| ||__ __|| .-' | .-.\ | .-.\ ( .-._) .' .') | | |(|
27+
| | |(_)| |-' )| `-. | | | )| | | `-. | `-'/ | |-' )(_) \ | |(_) | | (_)
28+
| | | | | |--' | .-' | |\ | (_) | | .-' | ( | |--' _ \ \ \ \ | | | |
29+
\ `-' / | | | `--.| | |)| | | | `--.| |\ \ | | ( `-' ) \ `-. | `--. | |
30+
)---' /( /( __.'/( (_) `-' /( __.'|_| \)\ /( `----' \____\|( __.'`-'
31+
(_) (__) (__) (__) (__) (__)(__) (_)
32+
```

bin/openterps

18.2 MB
Binary file not shown.

categories/main.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package categories
2+
3+
import (
4+
"net/http"
5+
6+
"openterps/dbconnector"
7+
"openterps/models"
8+
9+
"github.com/gin-gonic/gin"
10+
)
11+
12+
// GET /categories
13+
// Get all categories
14+
func GetCategories(c *gin.Context) {
15+
var categories []models.Category
16+
dbconnector.DB.Find(&categories)
17+
18+
c.JSON(http.StatusOK, gin.H{"data": categories})
19+
}
20+
21+
type categoriesInput struct {
22+
Name string `json:"name" binding:"required"`
23+
}
24+
25+
// POST /categories
26+
// Create a categories
27+
func CreateCategories(c *gin.Context) {
28+
// Validate input
29+
var input categoriesInput
30+
if err := c.ShouldBindJSON(&input); err != nil {
31+
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
32+
return
33+
}
34+
35+
// Create categories
36+
categories := models.Category{
37+
Name: input.Name,
38+
}
39+
dbconnector.DB.Create(&categories)
40+
41+
c.JSON(http.StatusOK, gin.H{"data": categories})
42+
}
43+
44+
// PATCH /categories/:id
45+
// Update a categories
46+
func UpdateCategories(c *gin.Context) {
47+
var categories models.Category
48+
if err := dbconnector.DB.Where("id = ?", c.Param("id")).First(&categories).Error; err != nil {
49+
c.JSON(http.StatusBadRequest, gin.H{"error": "Category not found!"})
50+
return
51+
}
52+
53+
// Validate input
54+
var input categoriesInput
55+
if err := c.ShouldBindJSON(&input); err != nil {
56+
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
57+
return
58+
}
59+
60+
dbconnector.DB.Model(&categories).Updates(input)
61+
62+
c.JSON(http.StatusOK, gin.H{"data": categories})
63+
}
64+
65+
// DELETE /categories/:id
66+
// Delete a categories
67+
func DeleteCategories(c *gin.Context) {
68+
var categories models.Category
69+
if err := dbconnector.DB.Where("id = ?", c.Param("id")).First(&categories).Error; err != nil {
70+
c.JSON(http.StatusBadRequest, gin.H{"error": "Category not found!"})
71+
return
72+
}
73+
74+
dbconnector.DB.Delete(&categories)
75+
76+
c.JSON(http.StatusOK, gin.H{"data": true})
77+
}

dbConnector/main.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package dbconnector
2+
3+
import (
4+
"fmt"
5+
6+
"gorm.io/driver/postgres"
7+
"gorm.io/gorm"
8+
)
9+
10+
// TODO turn this stuff into env vars
11+
12+
var DB *gorm.DB
13+
14+
// ConnectDatabase - connects to database
15+
// returns gorm DB instance, error
16+
func ConnectDatabase(Host, Port, User, Password, Name string) *gorm.DB {
17+
fmt.Println("Host", Host)
18+
dsn := fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=disable", Host, Port, User, Password, Name)
19+
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
20+
21+
if err != nil {
22+
fmt.Println("Error connecting to databse: ", dsn)
23+
}
24+
25+
DB = db
26+
27+
return DB
28+
}

effects/main.go

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package effects
2+
3+
import (
4+
"net/http"
5+
6+
"openterps/dbconnector"
7+
"openterps/models"
8+
"openterps/simpleauth"
9+
10+
"github.com/gin-gonic/gin"
11+
)
12+
13+
// GET /effects
14+
// Get all effects
15+
func GetEffects(c *gin.Context) {
16+
var effects []models.Effect
17+
dbconnector.DB.Find(&effects)
18+
19+
c.JSON(http.StatusOK, gin.H{"data": effects})
20+
}
21+
22+
type effectInput struct {
23+
Name string `json:"name" binding:"required"`
24+
}
25+
26+
// POST /effect
27+
// Create a effect
28+
func CreateEffect(c *gin.Context) {
29+
userIsLoggedIn := simpleauth.ValidateRequest(c)
30+
31+
if userIsLoggedIn {
32+
// Validate input
33+
var input effectInput
34+
if err := c.ShouldBindJSON(&input); err != nil {
35+
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
36+
return
37+
}
38+
39+
// Create effect
40+
effect := models.Effect{
41+
Name: input.Name,
42+
}
43+
dbconnector.DB.Create(&effect)
44+
45+
c.JSON(http.StatusOK, gin.H{"data": effect})
46+
}
47+
}
48+
49+
// PATCH /effects/:id
50+
// Update a effect
51+
func UpdateEffect(c *gin.Context) {
52+
userIsLoggedIn := simpleauth.ValidateRequest(c)
53+
54+
if userIsLoggedIn {
55+
var effect models.Effect
56+
if err := dbconnector.DB.Where("id = ?", c.Param("id")).First(&effect).Error; err != nil {
57+
c.JSON(http.StatusBadRequest, gin.H{"error": "Effect not found!"})
58+
return
59+
}
60+
61+
// Validate input
62+
var input effectInput
63+
if err := c.ShouldBindJSON(&input); err != nil {
64+
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
65+
return
66+
}
67+
68+
dbconnector.DB.Model(&effect).Updates(input)
69+
70+
c.JSON(http.StatusOK, gin.H{"data": effect})
71+
}
72+
}
73+
74+
// DELETE /effects/:id
75+
// Delete a effects
76+
func DeleteEffect(c *gin.Context) {
77+
userIsLoggedIn := simpleauth.ValidateRequest(c)
78+
79+
if userIsLoggedIn {
80+
var effect models.Effect
81+
if err := dbconnector.DB.Where("id = ?", c.Param("id")).First(&effect).Error; err != nil {
82+
c.JSON(http.StatusBadRequest, gin.H{"error": "Effect not found!"})
83+
return
84+
}
85+
86+
dbconnector.DB.Delete(&effect)
87+
88+
c.JSON(http.StatusOK, gin.H{"data": true})
89+
}
90+
}

go-server/.swagger-codegen-ignore

Lines changed: 0 additions & 23 deletions
This file was deleted.

0 commit comments

Comments
 (0)