Skip to content

Instantly share code, notes, and snippets.

@scottcagno
scottcagno / rest-api.go
Last active March 25, 2022 23:05
rest api package somewhat working
package main
import (
"fmt"
"log"
"net/http"
"sort"
"strconv"
"strings"
)
@scottcagno
scottcagno / json_query.go
Last active March 17, 2022 02:52
Fast JSON Searching
package ndjson
import (
"bytes"
"errors"
"fmt"
"unicode"
"unicode/utf8"
)
@scottcagno
scottcagno / middleware.go
Created March 15, 2022 18:34
Simple middleware pattern
// Middleware is our type definition of a middleware function.
type Middleware func(http.Handler) http.Handler
// Tupperware is a basic middleware boilerplate that is of the
// `Middleware` type because it has its type signature.
func Tupperware(next http.Handler) http.Handler {
// `fn` has the type signature of a `http.HandlerFunc` type.
fn := func(w http.ResponseWriter, r *http.Request) {
// Do http stuff in here, and then choose where you want to
// call the next handler in the chain. It could be in here,
@scottcagno
scottcagno / index.gohtml
Created March 8, 2022 20:11
Go templates
{{ define "title" }}Index Page{{ end }}
{{ define "body" }}
<div class="container-fluid">
<h1>Welcome to the INDEX page</h1>
Feel free to check out my <a href="/home">home</a> page!
</div>
{{ end }}
@scottcagno
scottcagno / poller_v1.go
Created March 6, 2022 00:05
Poller (general purpose)
package main
import (
"fmt"
"log"
"os"
"time"
)
// this is version 1
@scottcagno
scottcagno / filewatcher.go
Created March 5, 2022 23:04
FileWatcher is a general purpose file watcher
package filewatcher
import (
"fmt"
"io/fs"
"log"
"os"
"path/filepath"
"regexp"
"strings"
@scottcagno
scottcagno / go-simple-http-server.go
Created March 1, 2022 19:57
Go version of Python's SimpleHTTPServer - for just starting up an HTTP server and serving up anything around you
func RunSimpleHTTPServer(addr, path string) {
// setup logger
l := log.New(os.Stderr, "", log.LstdFlags)
// check args
if addr == "" {
l.Panicln(errors.New("no listening address specified"))
}
// get current dir
wd, err := os.Getwd()
if err != nil {
@scottcagno
scottcagno / microservice-main.go
Last active February 25, 2022 23:56
Simple "generic" microservice example
package main
import (
"encoding/json"
"encoding/xml"
"fmt"
"log"
"net/http"
"net/url"
"strings"
@scottcagno
scottcagno / simple-lru.go
Created February 13, 2022 09:49
simple lru
package main
import (
"fmt"
"strings"
)
func main() {
// 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5
l := NewLRU(3)
@scottcagno
scottcagno / README.md
Last active January 20, 2022 22:16
complete session (cookie) manager

import it

 import "somerepo/pkg/sessions"

initialize it

store = sessions.NewSessionStore(&sessions.SessionConfig{
  SessionID: "sess-id",
 Timeout: time.Duration(15) * time.Minute,