Skip to content

Commit 3065dab

Browse files
committed
add sample api
1 parent 461b1c6 commit 3065dab

File tree

8 files changed

+184
-0
lines changed

8 files changed

+184
-0
lines changed

.github/workflows/build-image.yml

+5
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ jobs:
3333
with:
3434
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
3535

36+
- name: Build and push Docker image
37+
run: |
38+
docker build sample-api -t ${{env.REGISTRY}}/krassegorganisation/sample-api:latest
39+
docker push ${{env.REGISTRY}}/krassegorganisation/sample-api:latest
40+
3641
- name: Build and push Docker image
3742
run: |
3843
docker build blubb -t ${{env.REGISTRY}}/krassegorganisation/blubb:latest

sample-api/Dockerfile

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
FROM golang:1.22-alpine as build
2+
3+
4+
WORKDIR /app
5+
6+
COPY go.mod go.sum ./
7+
8+
9+
RUN go mod download
10+
11+
COPY . .
12+
13+
14+
RUN GOOS=linux go build .
15+
16+
FROM alpine:latest as runtime
17+
18+
WORKDIR /app
19+
20+
21+
COPY --from=build /app/sample-api /app/sample-api
22+
23+
CMD ["/app/sample-api"]

sample-api/db/entities/entry.go

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package entities
2+
3+
import (
4+
"encoding/json"
5+
6+
"gorm.io/gorm"
7+
)
8+
9+
type Entry struct {
10+
gorm.Model
11+
Name string
12+
}
13+
14+
func (e Entry) MarshalJSON() ([]byte, error) {
15+
return json.Marshal(map[string]any{
16+
"ID": e.ID,
17+
"Name": e.Name,
18+
"CreatedAt": e.CreatedAt,
19+
})
20+
}

sample-api/db/init.go

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package db
2+
3+
import (
4+
"os"
5+
"sample-api/db/entities"
6+
7+
"gorm.io/driver/mysql"
8+
"gorm.io/gorm"
9+
)
10+
11+
var DB *gorm.DB
12+
13+
func Initialize() {
14+
dsn := os.Getenv("DB_USER") + ":" + os.Getenv("DB_PASS") + "@tcp(" + os.Getenv("DB_HOST") + ":" + os.Getenv("DB_PORT") + ")/?charset=utf8mb4&parseTime=True&loc=Local"
15+
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
16+
if err != nil {
17+
panic(err)
18+
}
19+
20+
DB = db
21+
22+
DB.Exec("CREATE DATABASE IF NOT EXISTS sample")
23+
DB.Exec("USE sample")
24+
25+
DB.AutoMigrate(
26+
&entities.Entry{},
27+
)
28+
}

sample-api/go.mod

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
module sample-api
2+
3+
go 1.22.2
4+
5+
require (
6+
github.com/joho/godotenv v1.5.1
7+
gorm.io/gorm v1.25.9
8+
)
9+
10+
require github.com/go-sql-driver/mysql v1.7.0 // indirect
11+
12+
require (
13+
github.com/goombaio/namegenerator v0.0.0-20181006234301-989e774b106e
14+
github.com/jinzhu/inflection v1.0.0 // indirect
15+
github.com/jinzhu/now v1.1.5 // indirect
16+
gorm.io/driver/mysql v1.5.6
17+
)

sample-api/go.sum

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
github.com/go-sql-driver/mysql v1.7.0 h1:ueSltNNllEqE3qcWBTD0iQd3IpL/6U+mJxLkazJ7YPc=
2+
github.com/go-sql-driver/mysql v1.7.0/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
3+
github.com/goombaio/namegenerator v0.0.0-20181006234301-989e774b106e h1:XmA6L9IPRdUr28a+SK/oMchGgQy159wvzXA5tJ7l+40=
4+
github.com/goombaio/namegenerator v0.0.0-20181006234301-989e774b106e/go.mod h1:AFIo+02s+12CEg8Gzz9kzhCbmbq6JcKNrhHffCGA9z4=
5+
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
6+
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
7+
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
8+
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
9+
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
10+
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
11+
gorm.io/driver/mysql v1.5.6 h1:Ld4mkIickM+EliaQZQx3uOJDJHtrd70MxAUqWqlx3Y8=
12+
gorm.io/driver/mysql v1.5.6/go.mod h1:sEtPWMiqiN1N1cMXoXmBbd8C6/l+TESwriotuRRpkDM=
13+
gorm.io/gorm v1.25.7/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8=
14+
gorm.io/gorm v1.25.9 h1:wct0gxZIELDk8+ZqF/MVnHLkA1rvYlBWUMv2EdsK1g8=
15+
gorm.io/gorm v1.25.9/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8=

sample-api/main.go

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"log"
6+
"net/http"
7+
"os"
8+
"sample-api/db"
9+
"sample-api/db/entities"
10+
"time"
11+
12+
"github.com/goombaio/namegenerator"
13+
"github.com/joho/godotenv"
14+
)
15+
16+
var nameGenerator namegenerator.Generator
17+
18+
func main() {
19+
20+
godotenv.Load()
21+
22+
db.Initialize()
23+
24+
addr := os.Getenv("ADDR")
25+
if addr == "" {
26+
addr = ":80"
27+
}
28+
29+
seed := time.Now().UTC().UnixNano()
30+
nameGenerator = namegenerator.NewNameGenerator(seed)
31+
32+
http.HandleFunc("/", index)
33+
http.HandleFunc("/list", listAllEntries)
34+
http.HandleFunc("/create", createEntry)
35+
36+
log.Println("HTTP Server will start on " + addr)
37+
err := http.ListenAndServe(addr, nil)
38+
if err != nil {
39+
log.Println("ERROR ON STARTUP")
40+
}
41+
}
42+
43+
func listAllEntries(w http.ResponseWriter, r *http.Request) {
44+
entries := []entities.Entry{}
45+
if err := db.DB.Find(&entries).Error; err != nil {
46+
log.Println(err)
47+
}
48+
49+
data, err := json.Marshal(entries)
50+
51+
if err != nil {
52+
log.Println("ERROR", err)
53+
w.WriteHeader(http.StatusInternalServerError)
54+
return
55+
}
56+
57+
w.Write(data)
58+
}
59+
60+
func createEntry(w http.ResponseWriter, r *http.Request) {
61+
entry := entities.Entry{
62+
Name: nameGenerator.Generate(),
63+
}
64+
65+
if err := db.DB.Save(&entry).Error; err != nil {
66+
log.Println(err)
67+
w.WriteHeader(http.StatusInternalServerError)
68+
return
69+
}
70+
71+
w.Write([]byte("Created Successfully"))
72+
}
73+
74+
func index(w http.ResponseWriter, r *http.Request) {
75+
w.Write([]byte(`<h1>Sample Application</h1><hr/><a href="/list">List all Persons</a><br /><a href="/create">Create a new Person</a>`))
76+
}

sample-api/sample-api

9.29 MB
Binary file not shown.

0 commit comments

Comments
 (0)