Skip to content

Commit b2b7ed0

Browse files
author
Ben Turbes
committed
add example apis that interact with a database.
1 parent 6615a96 commit b2b7ed0

File tree

9 files changed

+91
-5
lines changed

9 files changed

+91
-5
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
demo_api
1+
demo_api
2+
demo.db

Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM golang:alpine
1+
FROM golang
22

33
RUN mkdir -p /go/src/github.com/bturbes/demo_api
44
WORKDIR /go/src/github.com/bturbes/demo_api

articles/create.go

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package articles
2+
3+
import (
4+
"net/http"
5+
6+
"github.com/labstack/echo"
7+
8+
"github.com/bturbes/demo_api/database"
9+
"github.com/bturbes/demo_api/models"
10+
)
11+
12+
// Create a new article.
13+
func Create(c echo.Context) error {
14+
a := new(models.Article)
15+
if err := c.Bind(a); err != nil {
16+
return err
17+
}
18+
database.Db.Create(&a)
19+
return c.String(http.StatusOK, "Created the article")
20+
}

articles/list.go

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package articles
2+
3+
import (
4+
"net/http"
5+
6+
"github.com/labstack/echo"
7+
8+
"github.com/bturbes/demo_api/database"
9+
"github.com/bturbes/demo_api/models"
10+
)
11+
12+
// List all the articles.
13+
func List(c echo.Context) error {
14+
articles := []models.Article{}
15+
if err := database.Db.Find(&articles).Error; err != nil {
16+
return c.String(http.StatusInternalServerError, "There was an error creating article.")
17+
}
18+
return c.JSONPretty(http.StatusOK, &articles, " ")
19+
}

database/db.go

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package database
2+
3+
import (
4+
"github.com/jinzhu/gorm"
5+
)
6+
7+
var (
8+
// Db holds the database connection info.
9+
Db *gorm.DB
10+
)

docker-compose.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
version: '2'
22
services:
33
web:
4-
image: golang:alpine
4+
image: golang
55
command: sh -c "go build && ./demo_api"
66
ports:
7-
- "9000:8080"
8-
volumes:
7+
- "8080:8080"
8+
volumes:
99
- ".:/go/src/github.com/bturbes/demo_api"
1010
working_dir: /go/src/github.com/bturbes/demo_api

main.go

+15
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,36 @@ import (
44
"log"
55
"net/http"
66

7+
"github.com/jinzhu/gorm"
8+
_ "github.com/jinzhu/gorm/dialects/sqlite"
79
"github.com/labstack/echo"
810
"github.com/labstack/echo/middleware"
911

12+
"github.com/bturbes/demo_api/articles"
13+
"github.com/bturbes/demo_api/database"
1014
myEcho "github.com/bturbes/demo_api/echo"
1115
"github.com/bturbes/demo_api/formats"
1216
"github.com/bturbes/demo_api/math"
17+
"github.com/bturbes/demo_api/models"
1318
)
1419

1520
func main() {
21+
var err error
22+
database.Db, err = gorm.Open("sqlite3", "demo.db")
23+
if err != nil {
24+
log.Fatal(err)
25+
}
26+
defer database.Db.Close()
27+
models.MigrateAll()
28+
1629
e := echo.New()
1730
e.GET("/", Index)
1831
e.GET("/add/:a/:b", math.Add)
1932
e.GET("/subtract/:a/:b", math.Subtract)
2033
e.GET("/sampledata", formats.SampleData)
2134
e.POST("/echo", myEcho.Echo)
35+
e.POST("/articles/create", articles.Create)
36+
e.GET("/articles/list", articles.List)
2237

2338
e.Use(middleware.Logger())
2439
e.Use(middleware.CORS())

models/article.go

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package models
2+
3+
import (
4+
"github.com/jinzhu/gorm"
5+
)
6+
7+
// Article contains the Title, Author, and Body of an article
8+
type Article struct {
9+
gorm.Model
10+
Title string
11+
Author string
12+
Body string
13+
}

models/migrations.go

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package models
2+
3+
import "github.com/bturbes/demo_api/database"
4+
5+
// MigrateAll runs all the migrations for the models
6+
func MigrateAll() {
7+
database.Db.AutoMigrate(&Article{})
8+
}

0 commit comments

Comments
 (0)