diff --git a/.drone.yml b/.drone.yml index 49c061d..eea9269 100644 --- a/.drone.yml +++ b/.drone.yml @@ -43,14 +43,9 @@ 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 @@ -58,4 +53,23 @@ steps: - master status: - success - - failure \ No newline at end of file + +- 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 \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 8c009f0..ca8efe3 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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" ] @@ -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" ] \ No newline at end of file +CMD [ "windlass-worker" ] \ No newline at end of file diff --git a/Taskfile.yml b/Taskfile.yml index 6423c65..12bb7f8 100755 --- a/Taskfile.yml +++ b/Taskfile.yml @@ -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 diff --git a/app/api/api.go b/app/api/api.go index 1909ea2..3f33d0d 100644 --- a/app/api/api.go +++ b/app/api/api.go @@ -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" ) @@ -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, }) }) diff --git a/app/api/models/response.go b/app/api/models/response.go new file mode 100644 index 0000000..a17f546 --- /dev/null +++ b/app/api/models/response.go @@ -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 +} diff --git a/app/models/project/project.go b/app/models/project/project.go index 826bbb9..426690c 100644 --- a/app/models/project/project.go +++ b/app/models/project/project.go @@ -4,6 +4,7 @@ import ( "errors" "net/http" "regexp" + "time" "github.com/UCCNetworkingSociety/Windlass-worker/app/models/container" ) @@ -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 } diff --git a/cmd/debug.sh b/cmd/debug.sh index 08a5309..e3a6c24 100644 --- a/cmd/debug.sh +++ b/cmd/debug.sh @@ -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 & \ No newline at end of file +dlv debug ./cmd/windlass-worker --build-flags '-mod vendor' -l 0.0.0.0:3456 --headless=true --api-version=2 & \ No newline at end of file diff --git a/cmd/main.go b/cmd/windlass-worker/main.go similarity index 100% rename from cmd/main.go rename to cmd/windlass-worker/main.go diff --git a/go.mod b/go.mod index 7ebc3b7..b3d9505 100644 --- a/go.mod +++ b/go.mod @@ -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