From 465935485645e8d0cd43b482c1f996ac4dfcf82e Mon Sep 17 00:00:00 2001 From: Simon Oberzier Date: Thu, 11 Jun 2026 09:52:57 +0200 Subject: [PATCH] Docker setup and read yaml --- .gitignore | 30 +++++++++++++++++++++ Dockerfile | 17 ++++++++++++ code/go.mod | 5 ++++ code/go.sum | 4 +++ code/main.go | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++ justfile | 7 +++++ 6 files changed, 137 insertions(+) create mode 100644 .gitignore create mode 100644 code/go.mod create mode 100644 code/go.sum create mode 100644 justfile diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..bc57e3a --- /dev/null +++ b/.gitignore @@ -0,0 +1,30 @@ +# --- OS Generated Files --- +.DS_Store +Thumbs.db + +# --- Go / Golang --- +main +code/main +*.exe +*.exe~ +*.dll +*.so +*.dylib +*.test +*.out + +# --- COBOL / C --- +*.out +*.o +*.obj +*.so +*.dll +*.gnt +*.int +*.lst +*.dylib + +# --- Environment / Secrets --- +.env +.env.* +!.env.example \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index e69de29..478ccf3 100644 --- a/Dockerfile +++ b/Dockerfile @@ -0,0 +1,17 @@ +FROM golang:latest AS builder +WORKDIR /app +COPY code/go.mod code/go.sum ./ +RUN go mod download +COPY code/main.go . +RUN CGO_ENABLED=0 GOOS=linux go build -o server main.go + +FROM alpine:latest +RUN apk add --no-cache \ + gnucobol \ + gcc \ + musl-dev +COPY api.yaml /api.yaml +COPY src/ /src/ +COPY --from=builder /app/server /server +EXPOSE 8080 +ENTRYPOINT ["/server"] \ No newline at end of file diff --git a/code/go.mod b/code/go.mod new file mode 100644 index 0000000..04c17d0 --- /dev/null +++ b/code/go.mod @@ -0,0 +1,5 @@ +module rest-cobol + +go 1.26.4 + +require gopkg.in/yaml.v3 v3.0.1 diff --git a/code/go.sum b/code/go.sum new file mode 100644 index 0000000..a62c313 --- /dev/null +++ b/code/go.sum @@ -0,0 +1,4 @@ +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/code/main.go b/code/main.go index e69de29..a7de661 100644 --- a/code/main.go +++ b/code/main.go @@ -0,0 +1,74 @@ +package main + +import ( + "encoding/json" + "fmt" + "log" + "net/http" + "io/ioutil" + "gopkg.in/yaml.v3" +) + +type ApiConfig struct { + Version string `yaml:"version"` + Project string `yaml:"project"` + Endpoints []Endpoint `yaml:"endpoints"` +} + +type Endpoint struct { + Path string `yaml:"path"` + Method string `yaml:"method"` + CobolSource string `yaml:"cobol_source"` + Procedure string `yaml:"procedure"` + Arguments []Argument `yaml:"arguments"` + ResponseCopybook string `yaml:"response_copybook"` +} + +type Argument struct { + Name string `yaml:"name"` + Target string `yaml:"target"` + Type string `yaml:"type"` + Source string `yaml:"source"` +} + +func readConf(filename string) (*ApiConfig, error) { + buf, err := ioutil.ReadFile(filename) + if err != nil { + return nil, err + } + + c := &ApiConfig{} + err = yaml.Unmarshal(buf, c) + if err != nil { + return nil, fmt.Errorf("in file %q: %w", filename, err) + } + + return c, err +} + +func projectHandler(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodGet { + http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) + return + } + + w.Header().Set("Content-Type", "application/json") + + api, err := readConf("api.yaml") + if err != nil { + log.Fatal(err) + } + + response := map[string]string{"project": api.Project} + json.NewEncoder(w).Encode(response) +} + +func main() { + http.HandleFunc("/project", projectHandler) + + port := ":8080" + fmt.Printf("Server starting on port %s...\n", port) + if err := http.ListenAndServe(port, nil); err != nil { + log.Fatalf("Server failed to start: %v", err) + } +} \ No newline at end of file diff --git a/justfile b/justfile new file mode 100644 index 0000000..12720b5 --- /dev/null +++ b/justfile @@ -0,0 +1,7 @@ +# Build the docker image +build: + docker build -t rest-cobol . + +# Run the image +run: build + docker run -p 8080:8080 rest-cobol \ No newline at end of file