Skip to content

Instantly share code, notes, and snippets.

View wemgl's full-sized avatar

Wembley Leach wemgl

View GitHub Profile
@wemgl
wemgl / main.dart
Last active April 14, 2022 00:42
Hangman Game
String updateGuessState(String correctWord, String guessState, String guess) {
// Update the guess state to account for the latest guessed letter.
var newGuessState = '';
for (var i = 0; i < correctWord.length; i++) {
if (correctWord[i] == guess) {
newGuessState += guess;
} else {
newGuessState += guessState[i];
}
}
@wemgl
wemgl / Dockerfile
Created November 25, 2020 12:31
Elixir Umbrella App Dockerfile
FROM elixir:1.11.1-alpine AS build
# install build dependencies
RUN apk add --no-cache build-base npm git
# prepare build dir
WORKDIR /app
# install hex + rebar
RUN mix local.hex --force && \
@wemgl
wemgl / createSimpleServerIndexHandlerTest.go
Created March 10, 2019 14:59
Example of testing the index handler for simple server
func TestIndex(t *testing.T) {
req, err := http.NewRequest(http.MethodGet, "/", nil)
if err != nil {
t.Fatalf("TestIndex: couldn't create HTTP GET request: %v", err)
}
rec := httptest.NewRecorder()
index().ServeHTTP(rec, req)
@wemgl
wemgl / createSimpleFileServer.go
Created March 10, 2019 14:49
Example of serving static content in Go simple server
// public serves static assets such as CSS and JavaScript to clients.
func public() http.Handler {
return http.StripPrefix("/public/", http.FileServer(http.Dir("./public")))
}
@wemgl
wemgl / createSimpleServerBaseTemplate.html
Created March 10, 2019 14:38
Example of simple HTML templates in Go
{{define "base"}}
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="shortcut icon" type="image/x-icon" href="/public/assets/images/favicon.ico">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Work+Sans:400,500,600,700">
@wemgl
wemgl / createSimpleIndexHandler.go
Last active March 10, 2019 14:29
Example of parsing templates and handling HTTP requests
// templates references the specified templates and caches the parsed results
// to help speed up response times.
var templates = template.Must(template.ParseFiles("./templates/base.html", "./templates/body.html"))
// index is the handler responsible for rending the index page for the site.
func index() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
b := struct {
Title template.HTML
BusinessName string
@wemgl
wemgl / createSimpleMiddleware.go
Created March 10, 2019 14:00
Example of creating a simple middleware function
// logging is middleware for wrapping any handler we want to track response
// times for and to see what resources are requested.
func logging(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
req := fmt.Sprintf("%s %s", r.Method, r.URL)
log.Println(req)
next.ServeHTTP(w, r)
log.Println(req, "completed in", time.Now().Sub(start))
})
@wemgl
wemgl / createSimpleServer.go
Last active March 10, 2019 13:59
main.go for the simple-server medium post
func main() {
mux := http.NewServeMux()
mux.Handle("/public/", logging(public()))
mux.Handle("/", logging(index()))
port, ok := os.LookupEnv("PORT")
if !ok {
port = "8080"
}
c, err := db.Collection(collName).Find(ctx, bson.D{})
if err != nil {
return fmt.Errorf("readTasks: couldn't list all to-dos: %v", err)
}
defer c.Close(ctx)
tw := tabwriter.NewWriter(os.Stdout, 24, 2, 4, ' ', tabwriter.TabIndent)
_, _ = fmt.Fprintln(tw, "ID\tCreated At\tModified At\tTask\t")
for c.Next(ctx) {
elem := &bson.D{}
if err = c.Decode(elem); err != nil {
objectIDS, err := primitive.ObjectIDFromHex(id)
if err != nil {
return 0, fmt.Errorf("deleteTask: couldn't convert to-do ID from input: %v", err)
}
idDoc := bson.D{{"_id", objectIDS}}
res, err := db.Collection(collName).DeleteOne(ctx, idDoc)
if err != nil {
return 0, fmt.Errorf("deleteTask: couldn't delete to-do from db: %v", err)
}