diff --git a/.github/workflows/build-image.yml b/.github/workflows/build-image.yml index 836b541..2208bee 100644 --- a/.github/workflows/build-image.yml +++ b/.github/workflows/build-image.yml @@ -33,6 +33,11 @@ jobs: with: images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} + - name: Build and push Docker image + run: | + docker build sample-api -t ${{env.REGISTRY}}/krassegorganisation/sample-api:latest + docker push ${{env.REGISTRY}}/krassegorganisation/sample-api:latest + - name: Build and push Docker image run: | docker build blubb -t ${{env.REGISTRY}}/krassegorganisation/blubb:latest diff --git a/sample-api/Dockerfile b/sample-api/Dockerfile new file mode 100644 index 0000000..3feca4a --- /dev/null +++ b/sample-api/Dockerfile @@ -0,0 +1,23 @@ +FROM golang:1.22-alpine as build + + +WORKDIR /app + +COPY go.mod go.sum ./ + + +RUN go mod download + +COPY . . + + +RUN GOOS=linux go build . + +FROM alpine:latest as runtime + +WORKDIR /app + + +COPY --from=build /app/sample-api /app/sample-api + +CMD ["/app/sample-api"] \ No newline at end of file diff --git a/sample-api/db/entities/entry.go b/sample-api/db/entities/entry.go new file mode 100644 index 0000000..f95cbd5 --- /dev/null +++ b/sample-api/db/entities/entry.go @@ -0,0 +1,20 @@ +package entities + +import ( + "encoding/json" + + "gorm.io/gorm" +) + +type Entry struct { + gorm.Model + Name string +} + +func (e Entry) MarshalJSON() ([]byte, error) { + return json.Marshal(map[string]any{ + "ID": e.ID, + "Name": e.Name, + "CreatedAt": e.CreatedAt, + }) +} diff --git a/sample-api/db/init.go b/sample-api/db/init.go new file mode 100644 index 0000000..0a44408 --- /dev/null +++ b/sample-api/db/init.go @@ -0,0 +1,28 @@ +package db + +import ( + "os" + "sample-api/db/entities" + + "gorm.io/driver/mysql" + "gorm.io/gorm" +) + +var DB *gorm.DB + +func Initialize() { + dsn := os.Getenv("DB_USER") + ":" + os.Getenv("DB_PASS") + "@tcp(" + os.Getenv("DB_HOST") + ":" + os.Getenv("DB_PORT") + ")/?charset=utf8mb4&parseTime=True&loc=Local" + db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{}) + if err != nil { + panic(err) + } + + DB = db + + DB.Exec("CREATE DATABASE IF NOT EXISTS sample") + DB.Exec("USE sample") + + DB.AutoMigrate( + &entities.Entry{}, + ) +} diff --git a/sample-api/go.mod b/sample-api/go.mod new file mode 100644 index 0000000..9aa0652 --- /dev/null +++ b/sample-api/go.mod @@ -0,0 +1,17 @@ +module sample-api + +go 1.22.2 + +require ( + github.com/joho/godotenv v1.5.1 + gorm.io/gorm v1.25.9 +) + +require github.com/go-sql-driver/mysql v1.7.0 // indirect + +require ( + github.com/goombaio/namegenerator v0.0.0-20181006234301-989e774b106e + github.com/jinzhu/inflection v1.0.0 // indirect + github.com/jinzhu/now v1.1.5 // indirect + gorm.io/driver/mysql v1.5.6 +) diff --git a/sample-api/go.sum b/sample-api/go.sum new file mode 100644 index 0000000..955417d --- /dev/null +++ b/sample-api/go.sum @@ -0,0 +1,15 @@ +github.com/go-sql-driver/mysql v1.7.0 h1:ueSltNNllEqE3qcWBTD0iQd3IpL/6U+mJxLkazJ7YPc= +github.com/go-sql-driver/mysql v1.7.0/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI= +github.com/goombaio/namegenerator v0.0.0-20181006234301-989e774b106e h1:XmA6L9IPRdUr28a+SK/oMchGgQy159wvzXA5tJ7l+40= +github.com/goombaio/namegenerator v0.0.0-20181006234301-989e774b106e/go.mod h1:AFIo+02s+12CEg8Gzz9kzhCbmbq6JcKNrhHffCGA9z4= +github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= +github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= +github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= +github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= +github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= +github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= +gorm.io/driver/mysql v1.5.6 h1:Ld4mkIickM+EliaQZQx3uOJDJHtrd70MxAUqWqlx3Y8= +gorm.io/driver/mysql v1.5.6/go.mod h1:sEtPWMiqiN1N1cMXoXmBbd8C6/l+TESwriotuRRpkDM= +gorm.io/gorm v1.25.7/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8= +gorm.io/gorm v1.25.9 h1:wct0gxZIELDk8+ZqF/MVnHLkA1rvYlBWUMv2EdsK1g8= +gorm.io/gorm v1.25.9/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8= diff --git a/sample-api/main.go b/sample-api/main.go new file mode 100644 index 0000000..2651476 --- /dev/null +++ b/sample-api/main.go @@ -0,0 +1,76 @@ +package main + +import ( + "encoding/json" + "log" + "net/http" + "os" + "sample-api/db" + "sample-api/db/entities" + "time" + + "github.com/goombaio/namegenerator" + "github.com/joho/godotenv" +) + +var nameGenerator namegenerator.Generator + +func main() { + + godotenv.Load() + + db.Initialize() + + addr := os.Getenv("ADDR") + if addr == "" { + addr = ":80" + } + + seed := time.Now().UTC().UnixNano() + nameGenerator = namegenerator.NewNameGenerator(seed) + + http.HandleFunc("/", index) + http.HandleFunc("/list", listAllEntries) + http.HandleFunc("/create", createEntry) + + log.Println("HTTP Server will start on " + addr) + err := http.ListenAndServe(addr, nil) + if err != nil { + log.Println("ERROR ON STARTUP") + } +} + +func listAllEntries(w http.ResponseWriter, r *http.Request) { + entries := []entities.Entry{} + if err := db.DB.Find(&entries).Error; err != nil { + log.Println(err) + } + + data, err := json.Marshal(entries) + + if err != nil { + log.Println("ERROR", err) + w.WriteHeader(http.StatusInternalServerError) + return + } + + w.Write(data) +} + +func createEntry(w http.ResponseWriter, r *http.Request) { + entry := entities.Entry{ + Name: nameGenerator.Generate(), + } + + if err := db.DB.Save(&entry).Error; err != nil { + log.Println(err) + w.WriteHeader(http.StatusInternalServerError) + return + } + + w.Write([]byte("Created Successfully")) +} + +func index(w http.ResponseWriter, r *http.Request) { + w.Write([]byte(`

Sample Application


List all Persons
Create a new Person`)) +} diff --git a/sample-api/sample-api b/sample-api/sample-api new file mode 100755 index 0000000..f1c1a70 Binary files /dev/null and b/sample-api/sample-api differ