-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
184 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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{}, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.