Skip to content

Commit

Permalink
Added two testing endpoints
Browse files Browse the repository at this point in the history
Kelby Amandy committed Aug 20, 2024
1 parent 90bd103 commit 4367cb7
Showing 11 changed files with 113 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.env
gogreggator
8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
run:
@./bin/gogreggator

build:
@go build -o ./bin/gogreggator ./cmd/app/main.go

test:
@go test -v ./...
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# gogreggator

A simple blog aggregator written in Go.
1 change: 1 addition & 0 deletions bin/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# This bin directory is where your go binaries will be stored temporarily
29 changes: 29 additions & 0 deletions cmd/app/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package main

import (
"log"
"net/http"
"os"

"github.com/joho/godotenv"
"github.com/kelbwah/gogreggator/internal/handlers"
)

func main() {
if err := godotenv.Load(".env"); err != nil {
log.Fatalf("Error loading .env vars: %v", err)
}
port := os.Getenv("PORT")
mux := http.NewServeMux()
handlers.HandlersInit(mux)

server := &http.Server{
Addr: ":" + port,
Handler: mux,
}

log.Printf("Listening on port ':%s'\n", port)
if err := server.ListenAndServe(); err != nil {
log.Fatalf("Server failed to start: %v\n", err)
}
}
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/kelbwah/gogreggator

go 1.22.1

require github.com/joho/godotenv v1.5.1 // indirect
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
11 changes: 11 additions & 0 deletions internal/handlers/error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package handlers

import (
"net/http"

"github.com/kelbwah/gogreggator/utils"
)

func errorHandler(w http.ResponseWriter, r *http.Request) {
utils.RespondWithError(w, http.StatusInternalServerError, "Internal Server Error")
}
8 changes: 8 additions & 0 deletions internal/handlers/handlers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package handlers

import "net/http"

func HandlersInit(mux *http.ServeMux) {
mux.HandleFunc("GET /v1/healthz", readinessHandler)
mux.HandleFunc("GET /v1/err", errorHandler)
}
14 changes: 14 additions & 0 deletions internal/handlers/readiness.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package handlers

import (
"net/http"

"github.com/kelbwah/gogreggator/utils"
)

func readinessHandler(w http.ResponseWriter, r *http.Request) {
payload := map[string]string{
"status": "ok",
}
utils.RespondWithJSON(w, http.StatusOK, payload)
}
32 changes: 32 additions & 0 deletions utils/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package utils

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

type errorResponse struct {
Error string `json:"error"`
}

func RespondWithError(w http.ResponseWriter, code int, msg string) {
if code > 499 {
log.Printf("Responding with 5XX error: %s", msg)
}
RespondWithJSON(w, code, errorResponse{
Error: msg,
})
}

func RespondWithJSON(w http.ResponseWriter, code int, payload interface{}) {
w.Header().Set("Content-Type", "application/json")
dat, err := json.Marshal(payload)
if err != nil {
log.Printf("Error marshalling JSON: %s", err)
w.WriteHeader(500)
return
}
w.WriteHeader(code)
w.Write(dat)
}

0 comments on commit 4367cb7

Please sign in to comment.