Skip to content

Commit

Permalink
Merge pull request #2 from itishrishikesh/postgres
Browse files Browse the repository at this point in the history
I wrote biolerplate code for gofeed, ground work for setting up postg…
  • Loading branch information
itishrishikesh authored Dec 6, 2023
2 parents e5c0286 + 11972ff commit 07321bd
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 10 deletions.
1 change: 1 addition & 0 deletions constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ const CONTENT_TYPE string = "Content-Type"
const APPLICATION_JSON string = "application/json"
const HTTP_SUCCESS int = 200
const HTTP_ERROR int = 500
const HTTP_BAD_REQUEST int = 400
13 changes: 13 additions & 0 deletions error_handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package main

import (
"log"
"net/http"

"github.com/itishrishikesh/gofeed/constants"
)

func errorHandler(writer http.ResponseWriter, request *http.Request) {
log.Println("D#1ONS9C - Request received:", request)
respondWithError(writer, constants.HTTP_BAD_REQUEST, "Something went wrong!")
}
4 changes: 3 additions & 1 deletion handler_readiness.go → health_handler.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package main

import (
"log"
"net/http"

"github.com/itishrishikesh/gofeed/constants"
)

func handlerReadiness(writer http.ResponseWriter, request *http.Request) {
func healthCheckHandler(writer http.ResponseWriter, request *http.Request) {
log.Println("D#1ONRAR - Request received:", request)
respondWithJSON(writer, constants.HTTP_SUCCESS, struct{}{})
}
12 changes: 12 additions & 0 deletions json.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
)

func respondWithJSON(writer http.ResponseWriter, code int, payload interface{}) {
log.Println("I#1ONRD6 - Trying to respond with json payload", payload)
data, err := json.Marshal(payload)
if err != nil {
log.Fatalln("E#1OLY17 - failed to marshal JSON response", payload, err)
Expand All @@ -18,3 +19,14 @@ func respondWithJSON(writer http.ResponseWriter, code int, payload interface{})
writer.WriteHeader(code)
writer.Write(data)
}

func respondWithError(writer http.ResponseWriter, code int, msg string) {
if code > 499 {
log.Println("I#1ONRP0 - Responding with 5XX error", msg)
}
type errResponse struct {
Error string `json:"error"`
}

respondWithJSON(writer, code, errResponse{Error: msg})
}
19 changes: 10 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,8 @@ func main() {

router := chi.NewRouter()

server := &http.Server{
Handler: router,
Addr: ":" + portNumber,
}

log.Println("I#1OLYVV - Server starting on port number", portNumber)

v1Router := chi.NewRouter()
v1Router.Get("/healthz", handlerReadiness)
v1Router.Mount("/v1", router)

router.Use(cors.Handler(cors.Options{
AllowedOrigins: []string{"https://*", "http://*"},
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
Expand All @@ -41,6 +32,16 @@ func main() {
MaxAge: 300,
}))

v1Router := chi.NewRouter()
v1Router.Get("/healthz", healthCheckHandler)
v1Router.Get("/err", errorHandler)
router.Mount("/v1", v1Router)

server := &http.Server{
Handler: router,
Addr: ":" + portNumber,
}

err := server.ListenAndServe()

if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions sql/queries/users.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-- name: CreateUser :one
INSERT INTO users (id, created_at, updated_at, name)
VALUES ($1, $2, $3, $4)
RETURNING *;
10 changes: 10 additions & 0 deletions sql/schema/001_users.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
-- +goose Up
CREATE TABLE users (
id UUID PRIMARY KEY,
created_at TIMESTAMP NOT NULL,
updated_at TIMESTAMP NOT NULL,
name TEXT NOT NULL
);

-- +goose Down
DROP TABLE users;
8 changes: 8 additions & 0 deletions sqlc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
version: "2"
sql:
- schema: "sql/schema"
queries: "sql/queries"
engine: "postgresql"
gen:
go:
out: "internal/database"

0 comments on commit 07321bd

Please sign in to comment.