Feat(ktm-booking): Initial commit
Some checks failed
ktm-booking-bot/ktm-booking-bot/pipeline/head Something is wrong with the build of this commit
Some checks failed
ktm-booking-bot/ktm-booking-bot/pipeline/head Something is wrong with the build of this commit
This commit is contained in:
31
backend/cmd/server/health.go
Normal file
31
backend/cmd/server/health.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/go-chi/render"
|
||||
)
|
||||
|
||||
type HealthStatus struct {
|
||||
Status string `json:"status"`
|
||||
}
|
||||
|
||||
func (*HealthStatus) Render(w http.ResponseWriter, r *http.Request) error {
|
||||
// Pre-processing before a response is marshalled and sent across the wire
|
||||
return nil
|
||||
}
|
||||
|
||||
// Health Check``
|
||||
// @Summary Responds to health check
|
||||
// @Description Description
|
||||
// @Tags Base
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Success 200 {object} string
|
||||
// @Failure 404 {object} string
|
||||
// @Router /health [get]
|
||||
func healthHandler(w http.ResponseWriter, r *http.Request) {
|
||||
var okRender HealthStatus
|
||||
okRender.Status = "ok"
|
||||
render.Render(w, r, &okRender)
|
||||
}
|
||||
39
backend/cmd/server/health_test.go
Normal file
39
backend/cmd/server/health_test.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestHealthStatus(t *testing.T) {
|
||||
handler := http.HandlerFunc(healthHandler)
|
||||
req, err := http.NewRequest("GET", "/health", nil)
|
||||
if err != nil {
|
||||
t.Errorf("Error creating a new request: %v", err)
|
||||
}
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
handler.ServeHTTP(rr, req)
|
||||
|
||||
if rr.Result().StatusCode != 200 {
|
||||
t.Errorf("Health did not respond with status-code 200")
|
||||
}
|
||||
|
||||
responseBytes, err := ioutil.ReadAll(rr.Result().Body)
|
||||
if err != nil {
|
||||
t.Errorf("Error reading response body: %v", err)
|
||||
}
|
||||
|
||||
var results map[string]any
|
||||
err = json.Unmarshal(responseBytes, &results)
|
||||
if err != nil {
|
||||
t.Errorf("Error decoding response body: %v", err)
|
||||
}
|
||||
|
||||
if results["status"] != "ok" {
|
||||
t.Errorf("Health status is not ok")
|
||||
}
|
||||
}
|
||||
64
backend/cmd/server/main.go
Normal file
64
backend/cmd/server/main.go
Normal file
@@ -0,0 +1,64 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
_ "git.samuelpua.com/telboon/ktm-train-bot/backend/docs"
|
||||
"git.samuelpua.com/telboon/ktm-train-bot/backend/internal/common"
|
||||
"git.samuelpua.com/telboon/ktm-train-bot/backend/internal/ktmtrainbot"
|
||||
"git.samuelpua.com/telboon/ktm-train-bot/backend/internal/user"
|
||||
"github.com/go-chi/chi"
|
||||
"github.com/joho/godotenv"
|
||||
httpSwagger "github.com/swaggo/http-swagger"
|
||||
)
|
||||
|
||||
// Root Handler - Test
|
||||
// @Summary This is test
|
||||
// @Description Description
|
||||
// @Tags Base
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Success 200 {object} string
|
||||
// @Failure 404 {object} string
|
||||
// @Router / [get]
|
||||
func rootHandler(w http.ResponseWriter, r *http.Request) {
|
||||
w.Write([]byte("hello world"))
|
||||
}
|
||||
|
||||
// @title KTM Train Booking Bot
|
||||
// @version 1.0
|
||||
// @description API for frontend - built on Go-chi
|
||||
// @BasePath /
|
||||
// @contact.name Samuel Pua
|
||||
// @contact.url https://git.samuelpua.com/telboon
|
||||
func main() {
|
||||
err := godotenv.Load()
|
||||
if err != nil {
|
||||
log.Fatal("Error loading .env file")
|
||||
}
|
||||
environment := os.Getenv("ENVIRONMENT")
|
||||
|
||||
db := common.InitDB()
|
||||
db.AutoMigrate(&user.User{})
|
||||
db.AutoMigrate(&user.Profile{})
|
||||
db.AutoMigrate(&user.Session{})
|
||||
db.AutoMigrate(&ktmtrainbot.Booking{})
|
||||
|
||||
r := chi.NewRouter()
|
||||
|
||||
if environment == "dev" {
|
||||
r.Mount("/docs", httpSwagger.WrapHandler)
|
||||
}
|
||||
|
||||
r.Mount("/api/v1/user", user.UserRoutes(db))
|
||||
r.Mount("/api/v1/ktmtrainbot", ktmtrainbot.KTMTrainBotRoutes(db))
|
||||
|
||||
r.Get("/", rootHandler)
|
||||
r.Get("/health", healthHandler)
|
||||
|
||||
server_str := ":8000"
|
||||
log.Printf("Starting server at %s\n", server_str)
|
||||
http.ListenAndServe(server_str, r)
|
||||
}
|
||||
Reference in New Issue
Block a user