~ols/avgen

6d168d34ebd39e44b8433b7ea5bb34f3b55c62b4 — Oliver Leaver-Smith 2 months ago 6cd0d80 master
Add README, gitignore, and Makefile
4 files changed, 39 insertions(+), 3 deletions(-)

A .gitignore
A Makefile
A README.md
M main.go
A .gitignore => .gitignore +1 -0
@@ 0,0 1,1 @@
avgen

A Makefile => Makefile +2 -0
@@ 0,0 1,2 @@
avgen:
	go build -ldflags "-X main.commitHash=$$(git rev-parse --short HEAD) -X main.commitDate=$$(git log -1 --format=%ct)"

A README.md => README.md +11 -0
@@ 0,0 1,11 @@
# avgen

Build with
```
make
```

Run with
```
PORT=1314 ./avgen
```

M main.go => main.go +25 -3
@@ 2,15 2,36 @@ package main

import (
	"bytes"
	"encoding/json"
	"image"
	"image/png"
	"log"
	"net/http"
	"os"

	adorable "github.com/ipsn/go-adorable"
)

func Handle(w http.ResponseWriter, r *http.Request) {
var (
	commitHash string
	commitDate string
)

type Info struct {
	CommitHash string `json:"commit"`
	CommitDate string `json:"date"`
}

func getInfo(w http.ResponseWriter, r *http.Request) {
	info := Info{
		CommitHash: commitHash,
		CommitDate: commitDate,
	}
	w.Header().Set("Content-Type", "application/json")
	json.NewEncoder(w).Encode(info)
}

func generateAvatar(w http.ResponseWriter, r *http.Request) {
	seed := r.URL.Query().Get("seed")
	log.Println(seed)
	w.Header().Set("Content-Type", "image/png")


@@ 20,6 41,7 @@ func Handle(w http.ResponseWriter, r *http.Request) {
}

func main() {
	http.HandleFunc("/", Handle)
	http.ListenAndServe(":1314", nil)
	http.HandleFunc("/info", getInfo)
	http.HandleFunc("/", generateAvatar)
	http.ListenAndServe(":"+os.Getenv("PORT"), nil)
}