-
-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* check token earlier Signed-off-by: Ahmet Turkmen <[email protected]> * add /health endpoint Signed-off-by: Ahmet Turkmen <[email protected]> * add workflow to run tests Signed-off-by: Ahmet Turkmen <[email protected]> * return NoToken Err Signed-off-by: Ahmet Turkmen <[email protected]> * remove suffix Signed-off-by: Ahmet Turkmen <[email protected]>
- Loading branch information
Showing
9 changed files
with
218 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
name: Build Server & Test | ||
on: [push, pull_request] | ||
|
||
jobs: | ||
build: | ||
runs-on: ${{ matrix.os }} | ||
strategy: | ||
matrix: | ||
os: [ubuntu-16.04, ubuntu-18.04] | ||
services: | ||
postgres: | ||
image: postgres:alpine | ||
env: | ||
POSTGRES_PASSWORD: password | ||
POSTGRES_USER: user | ||
POSTGRES_DB: passwall | ||
options: >- | ||
--health-cmd pg_isready | ||
--health-interval 10s | ||
--health-timeout 5s | ||
--health-retries 5 | ||
ports: | ||
- 5432:5432 | ||
|
||
steps: | ||
- name: Set up Go 1.14 | ||
uses: actions/setup-go@v1 | ||
with: | ||
go-version: 1.14 | ||
id: go | ||
|
||
|
||
- name: Check out code into the Go module directory | ||
uses: actions/checkout@v1 | ||
|
||
- name: Get dependencies | ||
run: | | ||
go get -v -t -d ./... | ||
if [ -f Gopkg.toml ]; then | ||
curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh | ||
dep ensure | ||
fi | ||
- name: Build and Run Pass-Wall Server | ||
run: | | ||
go build -o passwall ./cmd/passwall-server/main.go | ||
chmod +x ./passwall | ||
./passwall & | ||
- name: Run Tests | ||
run: go test -v --race ./... | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package api | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/pass-wall/passwall-server/internal/config" | ||
"github.com/pass-wall/passwall-server/internal/storage" | ||
) | ||
|
||
var ( | ||
// should be improved | ||
Port = config.SetupConfigDefaults().Server.Port | ||
ServerAddress = "0.0.0.0" + ":" + Port | ||
) | ||
|
||
type HealthProp struct { | ||
StatusCode int | ||
Err error | ||
} | ||
|
||
type Services struct { | ||
API *HealthProp | ||
Database *HealthProp | ||
} | ||
|
||
func HealthCheck(s storage.Store) http.HandlerFunc { | ||
|
||
return func(w http.ResponseWriter, r *http.Request) { | ||
|
||
var checkResult Services | ||
var APIStatus *HealthProp | ||
var DBStatus *HealthProp | ||
|
||
if err := checkEndPoint(ServerAddress); err != nil { | ||
APIStatus = getStatus(http.StatusInternalServerError, err) | ||
} | ||
|
||
APIStatus = getStatus(http.StatusOK, nil) | ||
|
||
if err := s.Ping(); err != nil { | ||
DBStatus = getStatus(http.StatusInternalServerError, err) | ||
} | ||
|
||
DBStatus = getStatus(http.StatusOK, nil) | ||
|
||
checkResult = Services{ | ||
API: APIStatus, | ||
Database: DBStatus, | ||
} | ||
|
||
RespondWithJSON(w, checkResult.Database.StatusCode, checkResult) | ||
} | ||
|
||
} | ||
|
||
func checkEndPoint(url string) error { | ||
_, err := http.Get(url) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func getStatus(state int, err error) *HealthProp { | ||
return &HealthProp{ | ||
StatusCode: state, | ||
Err: err, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package api | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/pass-wall/passwall-server/internal/config" | ||
"github.com/pass-wall/passwall-server/internal/storage" | ||
) | ||
|
||
func TestHealthCheck(t *testing.T) { | ||
// create valid database config | ||
// should be same with the one on github actions | ||
|
||
mockDBConfig := &config.DatabaseConfiguration{ | ||
Driver: "postgres", | ||
Name: "passwall", | ||
Username: "user", | ||
Password: "password", | ||
Host: "localhost", | ||
Port: "5432", | ||
LogMode: false, | ||
} | ||
|
||
db, err := storage.New(mockDBConfig) | ||
|
||
req, err := http.NewRequest("GET", "/health", nil) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
rr := httptest.NewRecorder() | ||
handler := HealthCheck(db) | ||
|
||
handler.ServeHTTP(rr, req) | ||
// more test cases could be added | ||
expected := `{"API":{"StatusCode":200,"Err":null},"Database":{"StatusCode":200,"Err":null}}` | ||
|
||
if rr.Body.String() != expected { | ||
t.Errorf("handler returned unexpected body: got %v want %v", | ||
rr.Body.String(), expected) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters