Skip to content

Commit

Permalink
feat: Separate implementation logic & create networks endpoint 🚀
Browse files Browse the repository at this point in the history
  • Loading branch information
luislvasquez committed May 22, 2022
1 parent 5129de2 commit 174c599
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 26 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ main

# Go workspace file
go.work
go.sum
.vscode
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,15 @@
* [x] Boot single endpoint
* [x] Containerize project
* [x] Add hot reload for dev
* [ ] Extra: Look for pydantic alternative for q. params
* [x] Configure backend API in seperate files
* [ ] Extra: Look for pydantic alternative for q. params and payload

#### Summary
* `Go` local modules import was a nightmare to setup but makes sense after it
* `Gin` official documentation is uglier than latinoamerica's corruption stats
* Coming from `python`, syntax makes sense and shows huge potential

### Milestone 2
* [ ] Research async implementation approach
* [ ] Use schema/model validation on queries
* [ ] Try simple connection with a sqlite
8 changes: 4 additions & 4 deletions dockerfiles/backend.Dockerfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
FROM golang:latest
FROM golang:1.17

RUN mkdir /app
WORKDIR /app
ADD src /app

RUN go get github.com/githubnemo/CompileDaemon
RUN go install github.com/githubnemo/CompileDaemon
RUN go install github.com/gin-gonic/gin
RUN go install github.com/githubnemo/CompileDaemon@latest

# Needs go.mod file (go mod init main + go mod tidy)
RUN go get -u
ENTRYPOINT CompileDaemon --build="go build main.go" --command=./main
19 changes: 19 additions & 0 deletions src/api/api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package api

import (
"github.com/gin-gonic/gin"
networks "main/api/networks"
)

func InitRouters() *gin.Engine {
router := gin.Default()
setUpRouter(router)
return router
}

func setUpRouter(router *gin.Engine) {
api := router.Group("/api")
{
networks.RegisterRouter(api.Group("/networks"))
}
}
13 changes: 13 additions & 0 deletions src/api/networks/controler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package networks

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

func addNetworks(c *gin.Context) {
c.JSON(200, gin.H{"msg": "Hello from addNetworks"})
}

func getNetworks(c *gin.Context) {
c.JSON(200, gin.H{"msg": "Hello from getNetworks"})
}
10 changes: 10 additions & 0 deletions src/api/networks/router.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package networks

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

func RegisterRouter(r *gin.RouterGroup) {
r.POST("/", addNetworks)
r.GET("/", getNetworks)
}
2 changes: 1 addition & 1 deletion src/go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module go-lang-research
module main

go 1.17

Expand Down
22 changes: 2 additions & 20 deletions src/main.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,10 @@
package main

import (
"net/http"
"github.com/gin-gonic/gin"
"main/api"
)

type album struct {
ID string `json:"id"`
Title string `json:"title"`
Artist string `json:"artist"`
Price float32 `json:"price"`
}

var albums = []album {
{ID: "1", Title: "Blue Train", Artist: "John Coltrane", Price: 56.99},
{ID: "2", Title: "Jeru", Artist: "Gerry Mulligan", Price: 17.99},
{ID: "3", Title: "Sarah Vaughan and Clifford Brown", Artist: "Sarah Vaughan", Price: 39.99},
{ID: "4", Title: "Lord of the Rings", Artist: "J.R.R. Tolkien", Price: 59.99},
}

func main() {
r := gin.New()
r.GET("/albums", func(c *gin.Context) {
c.JSON(http.StatusOK, albums)
})
r := api.InitRouters()
r.Run()
}

0 comments on commit 174c599

Please sign in to comment.