Skip to content

Instantly share code, notes, and snippets.

View nkreiger's full-sized avatar
🏠
Working from home

Noah Kreiger nkreiger

🏠
Working from home
View GitHub Profile
`public/config.js`
const config = (() => {
return {
"TEST_ONE": "hello",
"TEST_TWO": "world"
};
})();
`src/index.template.html`
import (
"github.com/joomcode/errorx"
"sync"
)
// Merge fans multiple error channels in to a single error channel
func Merge(errChans ...<-chan error) <-chan error {
mergedChan := make(chan error)
var errors []error
@nkreiger
nkreiger / main.go
Created June 24, 2021 00:48
final go url
func main() {
conn := weatherAPI{
Scheme: "https",
Host: "api.weatherapi.com",
Endpoints: weatherEndpoints{
Forecast: endpoint{
Path: "/forecast.json",
Method: "GET",
},
Sports: endpoint{
@nkreiger
nkreiger / main.go
Last active June 23, 2021 02:34
dynamic url simple example
type Formatter func() (string, *url.URL)
func (w *weatherAPI) ForecastURL(date, region string) Formatter {
return func() (string, *url.URL) {
u := &url.URL{
Scheme: w.Scheme,
Host: w.Host,
Path: w.Endpoints.Forecast.Path,
}
@nkreiger
nkreiger / main.go
Created June 23, 2021 02:16
url basic
package main
import (
"log"
)
type endpoint struct {
Path string `yaml:"path"`
Method string `yaml:"method"`
}
@nkreiger
nkreiger / config.yaml
Created June 22, 2021 02:16
Boiler plate weather api config.yaml
weatherapi:
scheme: https
host: http://api.weatherapi.com/v1
endpoints:
forecast:
path: /forecast.json
method: GET
sports:
path: /sports.json
method: GET
@nkreiger
nkreiger / main.go
Created June 22, 2021 02:09
go-urls basic
// main.go
package main
import (
"app/connections"
"log"
)
func main() {
log.Println(connections.Connections.WeatherAPI)
@nkreiger
nkreiger / graceful.go
Created December 29, 2020 17:49
graceful listen and serve
func GracefullyListenAndServe(ctx context.Context, servePort string, rtr *mux.Router) {
http.Handle("/", rtr)
h := &http.Server{
Addr: fmt.Sprintf(":%v", servePort),
Handler: handlers.CORS()(rtr),
}
sig := make(chan os.Signal, 1)
var ReadCollectionPayload = func(w http.ResponseWriter, r *http.Request) {
(w).Header().Set("Content-Type", "application/json")
var payload mongo.QueryInputs
// Try to decode the request body into the struct. If there is an error,
// respond to the client with the error message and a 400 status code.
err := json.NewDecoder(r.Body).Decode(&payload)
if err != nil {
writeStatus(&w, http.StatusBadRequest, "invalid query payload")
package mongo
import (
"fmt"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo/options"
"log"
)
// GetEvents executes a Find query with query parameters that don't include operations