-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Kelby Amandy
committed
Aug 20, 2024
1 parent
90bd103
commit 4367cb7
Showing
11 changed files
with
113 additions
and
0 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,2 @@ | ||
.env | ||
gogreggator |
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,8 @@ | ||
run: | ||
@./bin/gogreggator | ||
|
||
build: | ||
@go build -o ./bin/gogreggator ./cmd/app/main.go | ||
|
||
test: | ||
@go test -v ./... |
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
# gogreggator | ||
|
||
A simple blog aggregator written in Go. |
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 @@ | ||
# This bin directory is where your go binaries will be stored temporarily |
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,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) | ||
} | ||
} |
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,5 @@ | ||
module github.com/kelbwah/gogreggator | ||
|
||
go 1.22.1 | ||
|
||
require github.com/joho/godotenv v1.5.1 // indirect |
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,2 @@ | ||
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= | ||
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= |
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,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") | ||
} |
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,8 @@ | ||
package handlers | ||
|
||
import "net/http" | ||
|
||
func HandlersInit(mux *http.ServeMux) { | ||
mux.HandleFunc("GET /v1/healthz", readinessHandler) | ||
mux.HandleFunc("GET /v1/err", errorHandler) | ||
} |
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,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) | ||
} |
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,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) | ||
} |