-
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.
- Loading branch information
0 parents
commit 0792fb6
Showing
28 changed files
with
3,911 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,3 @@ | ||
.vscode/ | ||
rssagg | ||
.env |
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,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 | ||
) |
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,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= |
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,7 @@ | ||
package main | ||
|
||
import "net/http" | ||
|
||
func handlerErr(w http.ResponseWriter, r *http.Request) { | ||
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,7 @@ | ||
package main | ||
|
||
import "net/http" | ||
|
||
func handlerReadiness(w http.ResponseWriter, r *http.Request) { | ||
respondWithJSON(w, http.StatusOK, map[string]string{"status": "ok"}) | ||
} |
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,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) | ||
} | ||
|
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,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) | ||
} | ||
|
||
|
||
|
||
|
||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.