move template to ./template;
added global README.md;
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
**/.classpath
|
||||
**/.dockerignore
|
||||
**/.env
|
||||
**/.git
|
||||
**/.gitignore
|
||||
**/.project
|
||||
**/.settings
|
||||
**/.toolstarget
|
||||
**/.vs
|
||||
**/.vscode
|
||||
**/*.*proj.user
|
||||
**/*.dbmdl
|
||||
**/*.jfm
|
||||
**/azds.yaml
|
||||
**/bin
|
||||
**/charts
|
||||
**/docker-compose*
|
||||
**/Dockerfile*
|
||||
**/node_modules
|
||||
**/npm-debug.log
|
||||
**/obj
|
||||
**/secrets.dev.yaml
|
||||
**/values.dev.yaml
|
||||
LICENSE
|
||||
README.md
|
||||
|
||||
|
||||
## Add any files that are not relevant for docker to build your image
|
||||
@@ -0,0 +1,23 @@
|
||||
#########################
|
||||
###### build stage ######
|
||||
#########################
|
||||
FROM golang:1.13.1-alpine AS builder
|
||||
WORKDIR /app
|
||||
RUN apk add --no-cache git
|
||||
# Prevent downloads on every build.
|
||||
# Only downloads the dependencies if they actually change
|
||||
COPY go.mod go.sum ./
|
||||
RUN go mod download
|
||||
# Build the app
|
||||
COPY . .
|
||||
RUN go build -v -o api
|
||||
|
||||
|
||||
#########################
|
||||
###### final stage ######
|
||||
#########################
|
||||
FROM alpine:latest
|
||||
RUN apk --no-cache add ca-certificates
|
||||
COPY --from=builder /app/api /app/api
|
||||
EXPOSE 3000
|
||||
ENTRYPOINT ./app/api
|
||||
@@ -0,0 +1,5 @@
|
||||
module hyde.infosys.tuwien.ac.at/aic19/template
|
||||
|
||||
go 1.13
|
||||
|
||||
require github.com/gorilla/mux v1.7.3
|
||||
@@ -0,0 +1,2 @@
|
||||
github.com/gorilla/mux v1.7.3 h1:gnP5JzjVOuiZD07fKKToCAOjS0yOpj/qPETTXCCS6hw=
|
||||
github.com/gorilla/mux v1.7.3/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
|
||||
@@ -0,0 +1,75 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/gorilla/mux"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// resolve port to use
|
||||
port := os.Getenv("HTTP_PORT")
|
||||
if port == "" {
|
||||
log.Println("No HTTP_PORT environment variable specified. Using default port 3000")
|
||||
port = "3000" // default port
|
||||
}
|
||||
demoDataAccess() // demo accessing the json data from the external file
|
||||
|
||||
// setup routing
|
||||
r := mux.NewRouter()
|
||||
r.HandleFunc("/health", HealthHandler)
|
||||
r.HandleFunc("/color/random", RandomColorHandler)
|
||||
http.Handle("/", r)
|
||||
|
||||
// start api server (blocking)
|
||||
if err := http.ListenAndServe(":"+port, nil); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
// RandomColorHandler will "proxy" the request and call the color-service for a random color
|
||||
func RandomColorHandler(writer http.ResponseWriter, request *http.Request) {
|
||||
resp, err := http.Get("http://color-service:4000/color")
|
||||
if err != nil {
|
||||
writer.WriteHeader(http.StatusInternalServerError)
|
||||
log.Println("Failed to fetch random color")
|
||||
return
|
||||
}
|
||||
writer.Header().Add("Content-Type", resp.Header.Get("Content-Type"))
|
||||
_, _ = io.Copy(writer, resp.Body)
|
||||
}
|
||||
|
||||
// ColorData is simply a typing for the sample json data in data.json
|
||||
type ColorData struct {
|
||||
Colors []struct {
|
||||
Color string `json:"color"`
|
||||
Value string `json:"value"`
|
||||
} `json:"colors"`
|
||||
}
|
||||
|
||||
// demoDataAccess shows that you can mount a data file into the container and access it
|
||||
func demoDataAccess() {
|
||||
bytes, err := ioutil.ReadFile("/app/data.json")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
var colorData ColorData
|
||||
if err = json.Unmarshal(bytes, &colorData); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
log.Println("The first color: ", colorData.Colors[0])
|
||||
}
|
||||
|
||||
// HealthHandler responds to all requests with "ok" to signal that this application is ready to receive traffic
|
||||
func HealthHandler(writer http.ResponseWriter, request *http.Request) {
|
||||
writer.Header().Add("Access-Control-Allow-Origin", "*") // CORS example ... allow requests from all domains
|
||||
_, err := writer.Write([]byte("ok"))
|
||||
if err != nil {
|
||||
writer.WriteHeader(http.StatusInternalServerError)
|
||||
log.Println("Failed to produce health check")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user