Skip to content

Commit

Permalink
Inital code
Browse files Browse the repository at this point in the history
  • Loading branch information
jimbydamonk committed Jul 5, 2016
1 parent ef0bb63 commit 474c2e7
Show file tree
Hide file tree
Showing 6 changed files with 213 additions and 0 deletions.
1 change: 1 addition & 0 deletions Godeps
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
github.com/NYTimes/gizmo 1cdaffd9dba57da6b53cd23db00f5b940aac02f1
81 changes: 81 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#
# Makefile
#
# The kickoff point for all project management commands.
#

# Program version
VERSION := $(shell grep "const Version " version.go | sed -E 's/.*"(.+)"$$/\1/')

# Binary name for bintray
BIN_NAME=mock-ec2-metadata

# Project owner for bintray
OWNER=NYTimes

# Project name for bintray
PROJECT_NAME=mock-ec2-metadata

# Project url used for builds
# examples: github.com, bitbucket.org
REPO_HOST_URL=github.com

# Grab the current commit
GIT_COMMIT=$(shell git rev-parse HEAD)

# Check if there are uncommited changes
GIT_DIRTY=$(shell test -n "`git status --porcelain`" && echo "+CHANGES" || true)

# Use a local vendor directory for any dependencies; comment this out to
# use the global GOPATH instead
GOPATH=$(PWD)/.vendor

INSTALL_PATH=$(GOPATH)/src/github.com/NYTimes/mock-ec2-metadata

default: build

help:
@echo 'Management commands for mock-ec2-metadata:'
@echo
@echo 'Usage:'
@echo ' make build Compile the project.'
@echo ' make link Symlink this project into the GOPATH.'
@echo ' make test Run tests on a compiled project.'
@echo ' make fmt Reformat the source tree with gofmt.'
@echo ' make clean Clean the directory tree.'
@echo

build: .git $(GOPATH)/bin/gogpm $(INSTALL_PATH)
@echo "building ${OWNER} ${BIN_NAME} ${VERSION}"
@echo "GOPATH=${GOPATH}"
$(GOPATH)/bin/gogpm install &&\
go build -ldflags "-X main.GitCommit=${GIT_COMMIT}${GIT_DIRTY}" -o bin/${BIN_NAME} main.go

clean:
@test ! -e bin/${BIN_NAME} || rm bin/${BIN_NAME}

.git:
git init
git add -A .
git commit -m 'Initial scaffolding.'

link:
# relink into the go path
if [ ! $(INSTALL_PATH) -ef . ]; then \
mkdir -p `dirname $(INSTALL_PATH)`; \
ln -s $(PWD) $(INSTALL_PATH); \
fi

$(INSTALL_PATH):
make link

$(GOPATH)/bin/gogpm:
go get github.com/mtibben/gogpm

test:
go test ./...

fmt:
find . -name '*.go' -not -path './.vendor/*' -exec gofmt -w=true {} ';'

.PHONY: build dist clean test help default link fmt
15 changes: 15 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"Server": {
"HTTPPort": 8111,
"Log": "./app.log",
"HTTPAccessLog": "./access.log"
},

"MetadataItems": {
"/latest": "meta-data",
"/2009-04-04": "meta-data",
"/2009-04-04/meta-data": "hostname",
"/2009-04-04/meta-data/hostname": "metadata-hostname"

}
}
24 changes: 24 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package main

import (
"github.com/NYTimes/mock-ec2-metadata/service"
"github.com/NYTimes/gizmo/config"
"github.com/NYTimes/gizmo/server"
)

func main() {
// showing 1 way of managing gizmo/config: importing from a local file
var cfg *service.Config
config.LoadJSONFile("./config.json", &cfg)

server.Init("mock-ec2-metadata", cfg.Server)
err := server.Register(service.NewMetadataService(cfg))
if err != nil {
server.Log.Fatal("unable to register service: ", err)
}

err = server.Run()
if err != nil {
server.Log.Fatal("server encountered a fatal error: ", err)
}
}
80 changes: 80 additions & 0 deletions service/service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package service

import (
"net/http"
"github.com/NYTimes/gizmo/server"
"github.com/Sirupsen/logrus"
)

type (

Config struct {
Server *server.Config
MetadataItems map[string]interface{}
}
MetadataService struct {
config *Config
}
)

func NewMetadataService(cfg *Config) *MetadataService {
return &MetadataService{cfg}
}

func (s *MetadataService) Middleware(h http.Handler) http.Handler {
return h
}

func (s *MetadataService) JSONMiddleware(j server.JSONEndpoint) server.JSONEndpoint {
return func(r *http.Request) (int, interface{}, error) {

status, res, err := j(r)
if err != nil {
server.LogWithFields(r).WithFields(logrus.Fields{
"error": err,
}).Error("problems with serving request")
return http.StatusServiceUnavailable, nil, &jsonErr{"sorry, this service is unavailable"}
}

server.LogWithFields(r).Info("success!")
return status, res, nil
}
}

func (s *MetadataService) GetMetadataItem(r *http.Request) (int, interface{}, error) {
res := s.config.MetadataItems[r.URL.Path]
return http.StatusOK, res, nil
}

func (s *MetadataService) GetIndex(r *http.Request) (int, interface{}, error) {
return http.StatusOK, "Mock EC2 Metadata Service", nil
}

// JSONEndpoints is a listing of all endpoints available in the MetadataService.
func (s *MetadataService) Endpoints() map[string]map[string]http.HandlerFunc {

handlers := make(map[string]map[string]http.HandlerFunc)
for url, value := range s.config.MetadataItems {
server.Log.Info("adding route for url", url, " value ", value)

handlers[url] = map[string]http.HandlerFunc {
"GET": server.JSONToHTTP(s.GetMetadataItem).ServeHTTP,
}
}
handlers["/"] = map[string]http.HandlerFunc {
"GET": server.JSONToHTTP(s.GetIndex).ServeHTTP,
}
return handlers
}

func (s *MetadataService) Prefix() string {
return "/"
}

type jsonErr struct {
Err string `json:"error"`
}

func (e *jsonErr) Error() string {
return e.Err
}
12 changes: 12 additions & 0 deletions version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package main

// The git commit that was compiled. This will be filled in by the compiler.
var GitCommit string

// The main version number that is being run at the moment.
const Version = "0.1.0"

// A pre-release marker for the version. If this is "" (empty string)
// then it means that it is a final release. Otherwise, this is a pre-release
// such as "dev" (in development), "beta", "rc1", etc.
const VersionPrerelease = ""

0 comments on commit 474c2e7

Please sign in to comment.