Skip to content

Instantly share code, notes, and snippets.

package main
import (
"net/http"
"github.com/bmizerany/pat"
"github.com/justinas/alice"
)
func (app *application) routes() http.Handler {
package jsonlog
import (
"encoding/json"
"io"
"os"
"runtime/debug"
"sync"
"time"
)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
<h1>Simple CORS</h1>
<output></output>
<script>
document.addEventListener('DOMContentLoaded', function() {
@alexedwards
alexedwards / main.go
Created November 15, 2020 10:38
JSON decoding benchmarks
package main
import (
"encoding/json"
"io/ioutil"
"net/http"
)
func createMovieHandlerUnmarshal(w http.ResponseWriter, r *http.Request) {
var input struct {
@alexedwards
alexedwards / main.go
Created November 10, 2020 10:20
JSON encoding benchmarks #2
package main
import (
"encoding/json"
"net/http"
)
func main() {}
func healthcheckHandlerMarshalIndent(w http.ResponseWriter, r *http.Request) {
@alexedwards
alexedwards / main.go
Last active February 19, 2024 07:16
JSON encoding benchmarks
package main
import (
"encoding/json"
"net/http"
)
func main() {}
func healthcheckHandlerEncoder(w http.ResponseWriter, r *http.Request) {
@alexedwards
alexedwards / _tree
Created October 27, 2020 07:52
Dependency injection via closure (handlers in multiple packages)
.
├── go.mod
├── handlers
│ ├── books
│ │ └── books.go
│ └── env.go
├── main.go
└── models
└── models.go
@alexedwards
alexedwards / _tree
Created October 27, 2020 07:40
Config package with global variable
.
├── config
│ └── config.go
├── go.mod
├── go.sum
├── main.go
└── models
└── books
└── books.go
package main
import (
"io"
"net/http"
"log"
"github.com/alexedwards/scs/v2"
"github.com/alexedwards/scs/redisstore"
"github.com/gomodule/redigo/redis"
sessionManager = scs.New()
sessionManager.Store = redisstore.New(pool)
standardMiddleware := alice.New(app.recoverPanic, app.logRequest, secureHeaders)
dynamicMiddleware := alice.New(sessionManager.LoadAndSave, noSurf, app.authenticate)
mux := pat.New()
mux.Get("/", dynamicMiddleware.ThenFunc(app.home))
mux.Get("/about", dynamicMiddleware.ThenFunc(app.about))