Skip to content

Commit

Permalink
functional api
Browse files Browse the repository at this point in the history
  • Loading branch information
adesokanayo committed Jan 8, 2021
0 parents commit 4c52a22
Show file tree
Hide file tree
Showing 20 changed files with 951 additions and 0 deletions.
17 changes: 17 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Launch",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${fileDirname}",
"env": {},
"args": []
}
]
}
25 changes: 25 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"go.useLanguageServer": true,
"[go]": {
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.organizeImports": true,
},
// Optional: Disable snippets, as they conflict with completion ranking.
"editor.snippetSuggestions": "none",
},
"[go.mod]": {
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.organizeImports": true,
},
},
"gopls": {
// Add parameter placeholders when completing a function.
"usePlaceholders": true,

// If true, enable additional analyses with staticcheck.
// Warning: This will significantly increase memory usage.
"staticcheck": false,
}
}
68 changes: 68 additions & 0 deletions controller/post-controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package controller

import (
"encoding/json"
"net/http"

"github.com/adesokanayo/innovation/entity"
"github.com/adesokanayo/innovation/errors"
"github.com/adesokanayo/innovation/services"
)

type controller struct{}

//NewPostController creates a controller instance
func NewPostController() PostController {
return &controller{}
}

var (
postService services.PostService = services.NewPostService()
)

//PostController is
type PostController interface {
GetPosts(response http.ResponseWriter, request *http.Request)
AddPosts(response http.ResponseWriter, request *http.Request)
}

//GetPosts retrieve all posts
func (*controller) GetPosts(resp http.ResponseWriter, req *http.Request) {
resp.Header().Set("Content-type", "application/json")

posts, err := postService.FindAll()
if err != nil {
resp.WriteHeader(http.StatusInternalServerError)
json.NewEncoder(resp).Encode(errors.ServiceError{"Error getting the post"})
}
resp.WriteHeader(http.StatusOK)
json.NewEncoder(resp).Encode(posts)

}

func (*controller) AddPosts(resp http.ResponseWriter, req *http.Request) {
resp.Header().Set("Content-type", "application/json")
var post entity.Post
err := json.NewDecoder(req.Body).Decode(&post)
if err != nil {
resp.WriteHeader(http.StatusInternalServerError)
json.NewEncoder(resp).Encode(errors.ServiceError{Message: "Error adding the posts"})
return
}

err1 := postService.Validate(&post)
if err1 != nil {
resp.WriteHeader(http.StatusInternalServerError)
json.NewEncoder(resp).Encode(errors.ServiceError{Message: err1.Error()})
return
}
result, err2 := postService.Create(&post)
if err2 != nil {
resp.WriteHeader(http.StatusInternalServerError)
json.NewEncoder(resp).Encode(errors.ServiceError{Message: err2.Error()})
return
}
postService.Create(&post)
resp.WriteHeader(http.StatusOK)
json.NewEncoder(resp).Encode(result)
}
33 changes: 33 additions & 0 deletions deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-go-app
spec:
replicas: 1
selector:
matchLabels:
app: go-app
template:
metadata:
labels:
app: go-app
spec:
containers:
- name: go-app-container
image: adesokanayo/go-api-img:1.0.0
volumeMounts:
- name: storage
mountPath: temp/keys/
resources:
limits:
memory: "128Mi"
cpu: "500m"
ports:
- containerPort: 3000
securityContext:
capabilities:
add: ["SYS_ADMIN"]
volumes:
- name: storage
emptyDir: {}

16 changes: 16 additions & 0 deletions dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
FROM golang:alpine as builder
RUN apk update && apk add ca-certificates && rm -rf /var/cache/apk/*
LABEL name=Ayo
LABEL [email protected]
RUN mkdir /app
WORKDIR /app
COPY . .

RUN go get .
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app .
RUN apk add --no-cache ca-certificates
FROM scratch
WORKDIR /home/innovation
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
COPY --from=builder /app .
CMD ["./app"]
8 changes: 8 additions & 0 deletions entity/entity.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package entity

//Post holds the definition of a Post item
type Post struct {
ID int64 `json:"id"`
Title string `json:"title"`
Text string `json:"text"`
}
5 changes: 5 additions & 0 deletions errors/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package errors

type ServiceError struct {
Message string `json:message`
}
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module Users/odunayo/Desktop/innovation

go 1.15

require github.com/stretchr/testify v1.6.1
3 changes: 3 additions & 0 deletions go.mod 21-56-43-425.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/adesokanayo/innovation

go 1.15
Loading

0 comments on commit 4c52a22

Please sign in to comment.