Skip to content

Commit

Permalink
add sample api
Browse files Browse the repository at this point in the history
  • Loading branch information
Buh13246 committed Apr 25, 2024
1 parent 461b1c6 commit 3065dab
Show file tree
Hide file tree
Showing 8 changed files with 184 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/build-image.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 23 additions & 0 deletions sample-api/Dockerfile
Original file line number Diff line number Diff line change
@@ -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"]
20 changes: 20 additions & 0 deletions sample-api/db/entities/entry.go
Original file line number Diff line number Diff line change
@@ -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,
})
}
28 changes: 28 additions & 0 deletions sample-api/db/init.go
Original file line number Diff line number Diff line change
@@ -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{},
)
}
17 changes: 17 additions & 0 deletions sample-api/go.mod
Original file line number Diff line number Diff line change
@@ -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
)
15 changes: 15 additions & 0 deletions sample-api/go.sum
Original file line number Diff line number Diff line change
@@ -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=
76 changes: 76 additions & 0 deletions sample-api/main.go
Original file line number Diff line number Diff line change
@@ -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(`<h1>Sample Application</h1><hr/><a href="/list">List all Persons</a><br /><a href="/create">Create a new Person</a>`))
}
Binary file added sample-api/sample-api
Binary file not shown.

0 comments on commit 3065dab

Please sign in to comment.