File tree 9 files changed +91
-5
lines changed
9 files changed +91
-5
lines changed Original file line number Diff line number Diff line change 1
- demo_api
1
+ demo_api
2
+ demo.db
Original file line number Diff line number Diff line change 1
- FROM golang:alpine
1
+ FROM golang
2
2
3
3
RUN mkdir -p /go/src/github.com/bturbes/demo_api
4
4
WORKDIR /go/src/github.com/bturbes/demo_api
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ )
Original file line number Diff line number Diff line change 1
1
version : ' 2'
2
2
services :
3
3
web :
4
- image : golang:alpine
4
+ image : golang
5
5
command : sh -c "go build && ./demo_api"
6
6
ports :
7
- - " 9000 :8080"
8
- volumes :
7
+ - " 8080 :8080"
8
+ volumes :
9
9
- " .:/go/src/github.com/bturbes/demo_api"
10
10
working_dir : /go/src/github.com/bturbes/demo_api
Original file line number Diff line number Diff line change @@ -4,21 +4,36 @@ import (
4
4
"log"
5
5
"net/http"
6
6
7
+ "github.com/jinzhu/gorm"
8
+ _ "github.com/jinzhu/gorm/dialects/sqlite"
7
9
"github.com/labstack/echo"
8
10
"github.com/labstack/echo/middleware"
9
11
12
+ "github.com/bturbes/demo_api/articles"
13
+ "github.com/bturbes/demo_api/database"
10
14
myEcho "github.com/bturbes/demo_api/echo"
11
15
"github.com/bturbes/demo_api/formats"
12
16
"github.com/bturbes/demo_api/math"
17
+ "github.com/bturbes/demo_api/models"
13
18
)
14
19
15
20
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
+
16
29
e := echo .New ()
17
30
e .GET ("/" , Index )
18
31
e .GET ("/add/:a/:b" , math .Add )
19
32
e .GET ("/subtract/:a/:b" , math .Subtract )
20
33
e .GET ("/sampledata" , formats .SampleData )
21
34
e .POST ("/echo" , myEcho .Echo )
35
+ e .POST ("/articles/create" , articles .Create )
36
+ e .GET ("/articles/list" , articles .List )
22
37
23
38
e .Use (middleware .Logger ())
24
39
e .Use (middleware .CORS ())
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments