Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/Banjerr/OpenTerps.git
Browse files Browse the repository at this point in the history
  • Loading branch information
Banjerr committed Nov 23, 2020
2 parents f8c14c3 + 7cd6647 commit 459c8b9
Show file tree
Hide file tree
Showing 93 changed files with 1,166 additions and 7,207 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.env
21 changes: 21 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
FROM heroku/heroku:18-build as build

COPY . /app
WORKDIR /app

# Setup buildpack
RUN mkdir -p /tmp/buildpack/heroku/go /tmp/build_cache /tmp/env
RUN curl https://codon-buildpacks.s3.amazonaws.com/buildpacks/heroku/go.tgz | tar xz -C /tmp/buildpack/heroku/go

#Execute Buildpack
RUN STACK=heroku-18 /tmp/buildpack/heroku/go/bin/compile /app /tmp/build_cache /tmp/env

# Prepare final, minimal image
FROM heroku/heroku:18

COPY --from=build /app /app
ENV HOME /app
WORKDIR /app
RUN useradd -m heroku
USER heroku
CMD /app/bin/openterps
17 changes: 17 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
GO_BUILD_ENV := CGO_ENABLED=0 GOOS=linux GOARCH=amd64
DOCKER_BUILD=$(shell pwd)/.docker_build
DOCKER_CMD=$(DOCKER_BUILD)/go-getting-started

$(DOCKER_CMD): clean
mkdir -p $(DOCKER_BUILD)
$(GO_BUILD_ENV) go build -v -o $(DOCKER_CMD) .

clean:
rm -rf $(DOCKER_BUILD)

heroku: $(DOCKER_CMD)
heroku container:push web

build:
export GO111MODULE=on
env GOOS=linux GOARCH=amd64 go build -o bin/openterps -v .
1 change: 1 addition & 0 deletions Procfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
web: bin/openterps
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# OpenTerps 🍁

An open source API to categorize cannabis and it's terpenes/effects/tastes/smells.

[docs](https://app.swaggerhub.com/apis-docs/CountryFriedCoders/OpenTerps/0.0.2)

```
|
|.|
|\./|
|\./|
. |\./| .
\^.\ |\\.//| /.^/
\--.|\ |\\.//| /|.--/
\--.| \ |\\.//| / |.--/
\---.|\ |\./| /|.---/
\--.|\ |\./| /|.--/
\ .\ |.| /. /
_ -_^_^_^_- \ \\ // / -_^_^_^_- _
- -/_/_/- ^ ^ | ^ ^ -\_\_\- -
/-./ | \.-\
/-/ | \-\
;|' '|;
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
.---. ,---. ,---. .-. .-. _______ ,---. ,---. ,---. .---. ,--, ,-. ,-.
/ .-. ) | .-.\ | .-' | \| ||__ __|| .-' | .-.\ | .-.\ ( .-._) .' .') | | |(|
| | |(_)| |-' )| `-. | | | )| | | `-. | `-'/ | |-' )(_) \ | |(_) | | (_)
| | | | | |--' | .-' | |\ | (_) | | .-' | ( | |--' _ \ \ \ \ | | | |
\ `-' / | | | `--.| | |)| | | | `--.| |\ \ | | ( `-' ) \ `-. | `--. | |
)---' /( /( __.'/( (_) `-' /( __.'|_| \)\ /( `----' \____\|( __.'`-'
(_) (__) (__) (__) (__) (__)(__) (_)
```
Binary file added bin/openterps
Binary file not shown.
77 changes: 77 additions & 0 deletions categories/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package categories

import (
"net/http"

"openterps/dbconnector"
"openterps/models"

"github.com/gin-gonic/gin"
)

// GET /categories
// Get all categories
func GetCategories(c *gin.Context) {
var categories []models.Category
dbconnector.DB.Find(&categories)

c.JSON(http.StatusOK, gin.H{"data": categories})
}

type categoriesInput struct {
Name string `json:"name" binding:"required"`
}

// POST /categories
// Create a categories
func CreateCategories(c *gin.Context) {
// Validate input
var input categoriesInput
if err := c.ShouldBindJSON(&input); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}

// Create categories
categories := models.Category{
Name: input.Name,
}
dbconnector.DB.Create(&categories)

c.JSON(http.StatusOK, gin.H{"data": categories})
}

// PATCH /categories/:id
// Update a categories
func UpdateCategories(c *gin.Context) {
var categories models.Category
if err := dbconnector.DB.Where("id = ?", c.Param("id")).First(&categories).Error; err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Category not found!"})
return
}

// Validate input
var input categoriesInput
if err := c.ShouldBindJSON(&input); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}

dbconnector.DB.Model(&categories).Updates(input)

c.JSON(http.StatusOK, gin.H{"data": categories})
}

// DELETE /categories/:id
// Delete a categories
func DeleteCategories(c *gin.Context) {
var categories models.Category
if err := dbconnector.DB.Where("id = ?", c.Param("id")).First(&categories).Error; err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Category not found!"})
return
}

dbconnector.DB.Delete(&categories)

c.JSON(http.StatusOK, gin.H{"data": true})
}
28 changes: 28 additions & 0 deletions dbConnector/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package dbconnector

import (
"fmt"

"gorm.io/driver/postgres"
"gorm.io/gorm"
)

// TODO turn this stuff into env vars

var DB *gorm.DB

// ConnectDatabase - connects to database
// returns gorm DB instance, error
func ConnectDatabase(Host, Port, User, Password, Name string) *gorm.DB {
fmt.Println("Host", Host)
dsn := fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=disable", Host, Port, User, Password, Name)
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})

if err != nil {
fmt.Println("Error connecting to databse: ", dsn)
}

DB = db

return DB
}
90 changes: 90 additions & 0 deletions effects/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package effects

import (
"net/http"

"openterps/dbconnector"
"openterps/models"
"openterps/simpleauth"

"github.com/gin-gonic/gin"
)

// GET /effects
// Get all effects
func GetEffects(c *gin.Context) {
var effects []models.Effect
dbconnector.DB.Find(&effects)

c.JSON(http.StatusOK, gin.H{"data": effects})
}

type effectInput struct {
Name string `json:"name" binding:"required"`
}

// POST /effect
// Create a effect
func CreateEffect(c *gin.Context) {
userIsLoggedIn := simpleauth.ValidateRequest(c)

if userIsLoggedIn {
// Validate input
var input effectInput
if err := c.ShouldBindJSON(&input); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}

// Create effect
effect := models.Effect{
Name: input.Name,
}
dbconnector.DB.Create(&effect)

c.JSON(http.StatusOK, gin.H{"data": effect})
}
}

// PATCH /effects/:id
// Update a effect
func UpdateEffect(c *gin.Context) {
userIsLoggedIn := simpleauth.ValidateRequest(c)

if userIsLoggedIn {
var effect models.Effect
if err := dbconnector.DB.Where("id = ?", c.Param("id")).First(&effect).Error; err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Effect not found!"})
return
}

// Validate input
var input effectInput
if err := c.ShouldBindJSON(&input); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}

dbconnector.DB.Model(&effect).Updates(input)

c.JSON(http.StatusOK, gin.H{"data": effect})
}
}

// DELETE /effects/:id
// Delete a effects
func DeleteEffect(c *gin.Context) {
userIsLoggedIn := simpleauth.ValidateRequest(c)

if userIsLoggedIn {
var effect models.Effect
if err := dbconnector.DB.Where("id = ?", c.Param("id")).First(&effect).Error; err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Effect not found!"})
return
}

dbconnector.DB.Delete(&effect)

c.JSON(http.StatusOK, gin.H{"data": true})
}
}
23 changes: 0 additions & 23 deletions go-server/.swagger-codegen-ignore

This file was deleted.

1 change: 0 additions & 1 deletion go-server/.swagger-codegen/VERSION

This file was deleted.

0 comments on commit 459c8b9

Please sign in to comment.