Docker setup and read yaml

This commit is contained in:
2026-06-11 09:52:57 +02:00
parent a3f19462c3
commit 4659354856
6 changed files with 137 additions and 0 deletions
+30
View File
@@ -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
+17
View File
@@ -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"]
+5
View File
@@ -0,0 +1,5 @@
module rest-cobol
go 1.26.4
require gopkg.in/yaml.v3 v3.0.1
+4
View File
@@ -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=
+74
View File
@@ -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)
}
}
+7
View File
@@ -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