diff --git a/.drone.yaml b/.drone.yaml deleted file mode 100644 index 480adb7..0000000 --- a/.drone.yaml +++ /dev/null @@ -1,9 +0,0 @@ -kind: pipeline -name: default - -steps: -- name: test - image: golang - commands: - - go test -v ./... - - go build -o ./bin/drone-example . diff --git a/.drone.yml b/.drone.yml new file mode 100644 index 0000000..cf85fd5 --- /dev/null +++ b/.drone.yml @@ -0,0 +1,35 @@ +kind: pipeline +name: default + +steps: +- name: test + image: golang + commands: + - go test -v ./... + - go build -v -o ./bin/drone-example . + +--- +kind: pipeline +name: deploy + +steps: +- name: deploy + image: docker:cli + volumes: + - name: docker-socket + path: /var/run/docker.sock + commands: + - docker build -t drone-example . + - docker rm -f drone-example + - docker run -d -p 127.241.212.77:1098 --restart always --name drone-example drone-example + +volumes: +- name: docker-socket + host: + path: /var/run/docker.sock + +trigger: + event: + - promote + target: + - production diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..b45d9d3 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,11 @@ +FROM golang:latest + +WORKDIR /app + +COPY go.mod go.sum ./ +RUN go mod download && go mod verify + +COPY . . +RUN go build -v -o ./bin/drone-example . + +CMD ["./bin/drone-example"] \ No newline at end of file diff --git a/main.go b/main.go index 8fca152..f097d72 100644 --- a/main.go +++ b/main.go @@ -1,24 +1,46 @@ package main import ( - "fmt" + "encoding/json" "log" + "mime" + "net/http" + "strconv" "git.phc.dm.unipi.it/phc/drone-example/foo" ) func main() { - var a, b int - - fmt.Printf("a = ") - if _, err := fmt.Scanln("%d", &a); err != nil { - log.Fatal(err) - } - fmt.Printf("b = ") - if _, err := fmt.Scanln("%d", &b); err != nil { - log.Fatal(err) - } - - result := foo.Sum(a, b) - log.Printf("a + b = %v", result) + r := http.NewServeMux() + r.HandleFunc("/foo", func(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodGet { + http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) + return + } + + aStr := r.URL.Query().Get("a") + bStr := r.URL.Query().Get("b") + + a, err := strconv.Atoi(aStr) + if err != nil { + http.Error(w, `the query param "a" must be an integer`, http.StatusUnprocessableEntity) + return + } + + b, err := strconv.Atoi(bStr) + if err != nil { + http.Error(w, `the query param "b" must be an integer`, http.StatusUnprocessableEntity) + return + } + + result := foo.Sum(a, b) + + w.Header().Set("Content-Type", mime.TypeByExtension(".json")) + if err := json.NewEncoder(w).Encode(result); err != nil { + log.Fatal(err) + return + } + }) + + log.Fatal(http.ListenAndServe(":5000", r)) }