Skip to content
This repository has been archived by the owner on Dec 11, 2020. It is now read-only.

Commit

Permalink
Using go-chi/render hell yea
Browse files Browse the repository at this point in the history
Added response struct from Windlass
Added CreationDate and UpdateDate to project
  • Loading branch information
Strum355 committed Jul 23, 2019
1 parent 7bbe7a9 commit f33e2aa
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 25 deletions.
30 changes: 22 additions & 8 deletions .drone.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,33 @@ steps:
from_secret: discord_webhook_token
settings:
username: Netsoc CI
avatar_url: https://noahsc.xyz/public_images/drone.png
avatar_url: https://raw.githubusercontent.com/drone/brand/3051b0d85318a2a20b62927ba19fc07e24c0d751/logos/png/white/drone-logo-png-white-256.png
color: "#42f483"
message: >
{{#success build.status}}
Windlass Worker successfully built and pushed. Build num {{build.number}}.
{{else}}
Windlass Worker failed to build. Build num {{build.number}}.
{{/success}}
message: Windlass Worker successfully built and pushed. Build num {{build.number}}. [Link]({{build.link}})
when:
event:
- push
branch:
- master
status:
- success
- failure

- name: discord_notif
image: appleboy/drone-discord
environment:
WEBHOOK_ID:
from_secret: discord_webhook_id
WEBHOOK_TOKEN:
from_secret: discord_webhook_token
settings:
username: Netsoc CI
avatar_url: https://raw.githubusercontent.com/drone/brand/3051b0d85318a2a20b62927ba19fc07e24c0d751/logos/png/white/drone-logo-png-white-256.png
color: "#e04414"
message: Windlass Worker failed for {{build.author}}. Build num {{build.number}}. [Link]({{build.link}})
when:
event:
- push
branch:
- master
status:
- failure
10 changes: 6 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ RUN go mod download

COPY . .

RUN go install github.com/UCCNetworkingSociety/Windlass-worker/cmd
RUN go install github.com/UCCNetworkingSociety/Windlass-worker/cmd/windlass-worker

RUN go mod vendor && (vend || true)
RUN go mod vendor && vend

ENV GO111MODULES=off

CMD [ "go", "run", "cmd/main.go" ]

Expand All @@ -27,6 +29,6 @@ WORKDIR /bin

RUN mkdir /lib64 && ln -s /lib/libc.musl-x86_64.so.1 /lib64/ld-linux-x86-64.so.2

COPY --from=dev /go/bin/Windlass-worker ./Windlass
COPY --from=dev /go/bin/windlass-worker ./windlass-worker

CMD [ "Windlass-worker" ]
CMD [ "windlass-worker" ]
2 changes: 1 addition & 1 deletion Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ tasks:
live-reload:
cmds:
- ps -ef | grep go-build | grep -v grep | awk '{print $2}' | xargs -r kill
- go run cmd/main.go
- go run cmd/windlass-worker/main.go
sources:
- app/**/*.go
- cmd/**/*.go
Expand Down
15 changes: 9 additions & 6 deletions app/api/api.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package api

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

"github.com/UCCNetworkingSociety/Windlass-worker/app/api/models"
"github.com/go-chi/render"

"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
middlechi "github.com/go-chi/chi/middleware"
)

Expand All @@ -20,13 +23,13 @@ func NewAPI(router chi.Router) *API {

func (api *API) Init() {
api.routes.Use(middlechi.RealIP)
//api.routes.Use(middlechi.DefaultLogger)
api.routes.Use(middlechi.DefaultLogger)
api.routes.Use(middleware.Recoverer)
api.routes.Use(render.SetContentType(render.ContentTypeJSON))

api.routes.Get("/health", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(map[string]string{
"apiVersion": "v1",
"message": "cool and well",
render.Render(w, r, &models.APIResponse{
Status: http.StatusOK,
})
})

Expand Down
32 changes: 32 additions & 0 deletions app/api/models/response.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package models

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

type APIResponse struct {
Status int `json:"status"`
Content interface{} `json:"content"`
}

type apiResponse struct {
Status int `json:"status"`
Content interface{} `json:"content"`
Time time.Time `json:"time"`
}

func (resp APIResponse) MarshalJSON() ([]byte, error) {
timed := apiResponse{
Status: resp.Status,
Content: resp.Content,
Time: time.Now(),
}
return json.Marshal(timed)
}

func (resp APIResponse) Render(w http.ResponseWriter, r *http.Request) error {
w.WriteHeader(resp.Status)
return nil
}
11 changes: 7 additions & 4 deletions app/models/project/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"net/http"
"regexp"
"time"

"github.com/UCCNetworkingSociety/Windlass-worker/app/models/container"
)
Expand All @@ -18,13 +19,15 @@ var (
)

type Project struct {
Name string `json:"name"`
Namespace string `json:"namespace"`
Containers container.Containers `json:"containers"`
Name string `json:"name"`
Namespace string `json:"namespace"`
Containers container.Containers `json:"containers"`
CreationDate time.Time `json:"createdAt"`
UpdatedDate time.Time `json:"updatedAt"`
}

func (p *Project) Bind(r *http.Request) error {
if !projectName.MatchString(p.Namespace + "_" + p.Name) {
if !projectName.MatchString(p.Namespace + "-" + p.Name) {
return ErrInvalidFormat
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/debug.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ reset=$(ps -ef | grep 'dlv' | grep -v grep | awk '{print $2}' | xargs -r kill)
eval $reset

echo "Starting Delve"
dlv debug ./cmd --build-flags '-mod vendor' -l 0.0.0.0:3456 --headless=true --api-version=2 &
dlv debug ./cmd/windlass-worker --build-flags '-mod vendor' -l 0.0.0.0:3456 --headless=true --api-version=2 &
File renamed without changes.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ require (
github.com/flosch/pongo2 v0.0.0-20190707114632-bbf5a6c351f4 // indirect
github.com/go-check/check v1.0.0-20180628173108-788fd7840127 // indirect
github.com/go-chi/chi v4.0.2+incompatible
github.com/go-chi/render v1.0.1 // indirect
github.com/go-chi/render v1.0.1
github.com/golang/protobuf v1.3.2 // indirect
github.com/google/go-cmp v0.3.0 // indirect
github.com/hashicorp/consul/api v1.1.0
Expand Down

0 comments on commit f33e2aa

Please sign in to comment.