Skip to content

Commit

Permalink
HTTP Server BoilerPlate complete
Browse files Browse the repository at this point in the history
  • Loading branch information
joeyyy09 committed Jul 29, 2023
0 parents commit 0792fb6
Show file tree
Hide file tree
Showing 28 changed files with 3,911 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.vscode/
rssagg
.env
9 changes: 9 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module github.com/joeyyy09/rssagg

go 1.20

require (
github.com/go-chi/chi v1.5.4
github.com/go-chi/cors v1.2.1
github.com/joho/godotenv v1.5.1
)
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
github.com/go-chi/chi v1.5.4 h1:QHdzF2szwjqVV4wmByUnTcsbIg7UGaQ0tPF2t5GcAIs=
github.com/go-chi/chi v1.5.4/go.mod h1:uaf8YgoFazUOkPBG7fxPftUylNumIev9awIWOENIuEg=
github.com/go-chi/cors v1.2.1 h1:xEC8UT3Rlp2QuWNEr4Fs/c2EAGVKBwy/1vHx3bppil4=
github.com/go-chi/cors v1.2.1/go.mod h1:sSbTewc+6wYHBBCW7ytsFSn836hqM7JxpglAy2Vzc58=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
7 changes: 7 additions & 0 deletions handler_err.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

import "net/http"

func handlerErr(w http.ResponseWriter, r *http.Request) {
respondWithError(w, http.StatusInternalServerError, "Internal Server Error")
}
7 changes: 7 additions & 0 deletions handler_readiness.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

import "net/http"

func handlerReadiness(w http.ResponseWriter, r *http.Request) {
respondWithJSON(w, http.StatusOK, map[string]string{"status": "ok"})
}
30 changes: 30 additions & 0 deletions json.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package main
import (
"encoding/json"
"log"
"net/http"
)

func respondWithError(w http.ResponseWriter, code int, message string) {
if code > 499 {
log.Println("Respondong with error: ", message)
}
type errorResponse struct {
Error string `json:"error"`
}

respondWithJSON(w, code, errorResponse{Error: message})
}

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)
}

52 changes: 52 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package main

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

"github.com/go-chi/chi"
"github.com/go-chi/cors"
"github.com/joho/godotenv"
)

func main() {
godotenv.Load()
portString:= os.Getenv("PORT")
if portString == "" {
log.Fatal("$PORT must be set")
}

router:= chi.NewRouter()

router.Use(cors.Handler(cors.Options{
AllowedOrigins: []string{"https://*", "http://*"},
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
AllowedHeaders: []string{"*"},
ExposedHeaders: []string{"Link"},
AllowCredentials: false,
MaxAge: 300,
}))

v1Router := chi.NewRouter()
v1Router.Get("/healthz", handlerReadiness)
v1Router.Get("/err", handlerErr)

router.Mount("/v1", v1Router)

srv := &http.Server{
Handler: router,
Addr: ":" + portString,
}

log.Println("Server is running on port " + portString)

err := srv.ListenAndServe()
if err!=nil{
log.Fatal(err)
}




}
3 changes: 3 additions & 0 deletions vendor/github.com/go-chi/chi/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

269 changes: 269 additions & 0 deletions vendor/github.com/go-chi/chi/CHANGELOG.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 0792fb6

Please sign in to comment.