commit
de367073d6
@ -0,0 +1,14 @@
|
||||
# syntax=docker/dockerfile:1
|
||||
|
||||
FROM golang:1.17
|
||||
|
||||
WORKDIR /factorial
|
||||
|
||||
COPY go.mod ./
|
||||
COPY go.sum ./
|
||||
|
||||
RUN go mod download
|
||||
|
||||
COPY *.go ./
|
||||
|
||||
RUN go build -o /factorial_serv
|
||||
@ -0,0 +1,4 @@
|
||||
# Factorial RESTful API
|
||||
Takes a POST Request that takes json with the following structure: {"a":#,"b":#} (where # is an int) and
|
||||
returns a json string containing the factorial of a and b in the same structure. When a negative number or
|
||||
other incorrect input is given, the API returns an error.
|
||||
@ -0,0 +1,5 @@
|
||||
module github.com/ghfarrell/factorial_server
|
||||
|
||||
go 1.17
|
||||
|
||||
require github.com/julienschmidt/httprouter v1.3.0 // direct
|
||||
@ -0,0 +1,2 @@
|
||||
github.com/julienschmidt/httprouter v1.3.0 h1:U0609e9tgbseu3rBINet9P48AI/D3oJs4dN7jwJOQ1U=
|
||||
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
|
||||
@ -0,0 +1,74 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"github.com/julienschmidt/httprouter"
|
||||
)
|
||||
|
||||
// type ContextKeyString is a type that is a string meant for use as the key in an https context value
|
||||
type ContextKeyString string
|
||||
type args struct {
|
||||
A int `json:"a"`
|
||||
B int `json:"b"`
|
||||
}
|
||||
type requestError struct {
|
||||
Err string `json:"error"`
|
||||
}
|
||||
|
||||
func factorial(a int, ch chan int) {
|
||||
r := 1
|
||||
for f := 1; f <= a; f++ {
|
||||
r *= f
|
||||
}
|
||||
ch <- r
|
||||
}
|
||||
|
||||
func postError(w http.ResponseWriter, r *http.Request, err string) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
json.NewEncoder(w).Encode(requestError{Err: err})
|
||||
}
|
||||
|
||||
func middleware(f httprouter.Handle) httprouter.Handle {
|
||||
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
|
||||
var v args
|
||||
dec := json.NewDecoder(r.Body)
|
||||
dec.DisallowUnknownFields()
|
||||
err := dec.Decode(&v)
|
||||
if err != nil || v.A < 0 || v.B < 0 {
|
||||
postError(w, r, "Incorrect input")
|
||||
} else {
|
||||
ctx := r.Context()
|
||||
// using a new type definition to avoid compiler warnings
|
||||
ctx = context.WithValue(ctx, ContextKeyString("a"), v.A)
|
||||
ctx = context.WithValue(ctx, ContextKeyString("b"), v.B)
|
||||
r = r.WithContext(ctx)
|
||||
f(w, r, p)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func calculate(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
||||
var v args
|
||||
v.A = r.Context().Value(ContextKeyString("a")).(int)
|
||||
v.B = r.Context().Value(ContextKeyString("b")).(int)
|
||||
chanA := make(chan int)
|
||||
chanB := make(chan int)
|
||||
go factorial(v.A, chanA)
|
||||
go factorial(v.B, chanB)
|
||||
v.B = <-chanB
|
||||
v.A = <-chanA
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(v)
|
||||
}
|
||||
|
||||
func main() {
|
||||
router := httprouter.New()
|
||||
router.POST("/calculate", middleware(calculate))
|
||||
|
||||
log.Fatal(http.ListenAndServe(":8989", router))
|
||||
}
|
||||
Loading…
Reference in new issue