Skip to content

Commit

Permalink
Add healthcheck & conf endpoint (#6)
Browse files Browse the repository at this point in the history
## Summary
Add a healthcheck and a conf endpoint.

## Details and comments
- [x] Add healthcheck endpoint with data about API
- [x] Add conf endpoint

---
Closes #5
  • Loading branch information
mickahell authored Sep 19, 2023
1 parent 3258990 commit 731171f
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ repos:
- id: trailing-whitespace
exclude: vendor
- id: end-of-file-fixer
exclude: vendor
exclude: cocas/VERSION.txt
- repo: https://github.com/dnephin/pre-commit-golang
rev: v0.4.0
hooks:
Expand Down
1 change: 1 addition & 0 deletions cocas/VERSION.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.0.1
18 changes: 12 additions & 6 deletions cocas/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"gopkg.in/cas.v2"
)

var err error

func (h *myHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-type", "application/json;charset=UTF-8")
if !cas.IsAuthenticated(r) {
Expand All @@ -20,16 +22,20 @@ func (h *myHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}

if r.URL.Path == "/reload" {
w.WriteHeader(http.StatusOK)

if r.URL.Path == "/healthcheck" {
err = json.NewEncoder(w).Encode(GetHealth())
} else if r.URL.Path == "/reload" {
ExternalLoadConf()
logger.GetLogger().LogInfo("cocas", "Reloaded conf")
return
err = json.NewEncoder(w).Encode(GalaxyUsers)
} else if r.URL.Path == "/conf" {
err = json.NewEncoder(w).Encode(GalaxyUsers)
} else {
err = json.NewEncoder(w).Encode(GetProfile(r))
}

esner := GetProfile(r)

w.WriteHeader(http.StatusOK)
err := json.NewEncoder(w).Encode(esner)
if err != nil {
logger.GetLogger().LogError("cocas", "problem with encoder.", err)
} else {
Expand Down
23 changes: 22 additions & 1 deletion cocas/model.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package cocas

import (
_ "embed"
"net/http"

"gopkg.in/cas.v2"
)

// swagger:model Profile
//go:embed VERSION.txt
var version_file string

type Profile struct {
Username string `json:"username"`
First string `json:"firstname"`
Expand All @@ -27,6 +30,13 @@ type Profile struct {

var ESNer Profile

type Health struct {
Title string `json:"title"`
Version string `json:"version"`
Name string `json:"name"`
Message string `json:"message"`
}

func GetProfile(r *http.Request) *Profile {
ESNer = Profile{
Username: cas.Username(r),
Expand Down Expand Up @@ -78,3 +88,14 @@ func GetProfile(r *http.Request) *Profile {

return &ESNer
}

func GetHealth() *Health {
var hea Health

hea.Title = "IT works !"
hea.Name = "coCAS"
hea.Version = string(version_file)
hea.Message = "Gocas is feeling connected today :)"

return &hea
}
1 change: 1 addition & 0 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ docker-build:
docker build . -f docker/Dockerfile --tag $(PROJECT_NAME):latest

docker-start:
docker rm -f $(PROJECT_NAME) 2> /dev/null
docker run \
--name $(PROJECT_NAME) \
-v $(PWD)/test:/etc/$(PROJECT_PATH)/conf \
Expand Down

0 comments on commit 731171f

Please sign in to comment.