From 21b8605fc0f1466099663a54ed5cf50974397bc6 Mon Sep 17 00:00:00 2001 From: Kato Twofold Date: Sun, 15 May 2022 12:43:42 +0300 Subject: [PATCH] Initial Commit --- .dockerignore | 4 +++ .env.example | 6 ++++ .gitignore | 4 +++ Dockerfile | 21 +++++++++++ LICENSE | 0 README.md | 0 db/migrations/000001_posts.down.sql | 1 + db/migrations/000001_posts.up.sql | 11 ++++++ dev/.keep | 0 docker-compose.example.yaml | 35 +++++++++++++++++++ run.sh | 5 +++ src/.gitignore | 0 src/controllers/post/post.go | 40 +++++++++++++++++++++ src/controllers/post/post_types.go | 6 ++++ src/go.mod | 5 +++ src/go.sum | 54 +++++++++++++++++++++++++++++ src/main.go | 24 +++++++++++++ src/routes/routes_api.go | 42 ++++++++++++++++++++++ src/routes/routes_health.go | 18 ++++++++++ src/templates/index.html | 0 src/types/types_error.go | 6 ++++ 21 files changed, 282 insertions(+) create mode 100644 .dockerignore create mode 100644 .env.example create mode 100644 .gitignore create mode 100644 Dockerfile create mode 100644 LICENSE create mode 100644 README.md create mode 100644 db/migrations/000001_posts.down.sql create mode 100644 db/migrations/000001_posts.up.sql create mode 100644 dev/.keep create mode 100644 docker-compose.example.yaml create mode 100644 run.sh create mode 100644 src/.gitignore create mode 100644 src/controllers/post/post.go create mode 100644 src/controllers/post/post_types.go create mode 100644 src/go.mod create mode 100644 src/go.sum create mode 100644 src/main.go create mode 100644 src/routes/routes_api.go create mode 100644 src/routes/routes_health.go create mode 100644 src/templates/index.html create mode 100644 src/types/types_error.go diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..5b1d23c --- /dev/null +++ b/.dockerignore @@ -0,0 +1,4 @@ +dev +.gitignore +LICENSE +README.md \ No newline at end of file diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..08a12af --- /dev/null +++ b/.env.example @@ -0,0 +1,6 @@ +MYSQL_ROOT_PASSWORD=example-dev +MYSQL_DATABASE=freepad +MYSQL_USER=freepad +MYSQL_PASSWORD=example-dev +MYSQL_URL=mariadb +MYSQL_PORT=3306 \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0457541 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +dev/* +!dev/.keep +.env +docker-compose.yaml diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..519bc71 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,21 @@ +FROM golang:1.18.2-alpine + +ENV GO111MODULE=on + +ENV MYSQL_USER=${MYSQL_USER} +ENV MYSQL_PASSWORD=${MYSQL_PASSWORD} +ENV MYSQL_DATABASE=${MYSQL_DATABASE} + +RUN mkdir /app + +WORKDIR /app +COPY . /app + +# Install & Run migrations +RUN wget -O /tmp/migrations.tar.gz "https://github.com/golang-migrate/migrate/releases/download/v4.15.2/migrate.linux-386.tar.gz" && \ + tar -xzvf /tmp/migrations.tar.gz -C /tmp/ && \ + mv /tmp/migrate /usr/bin/migrate + +EXPOSE 8080 + +CMD ["sh", "run.sh"] \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..e69de29 diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 diff --git a/db/migrations/000001_posts.down.sql b/db/migrations/000001_posts.down.sql new file mode 100644 index 0000000..8a64ff3 --- /dev/null +++ b/db/migrations/000001_posts.down.sql @@ -0,0 +1 @@ +DROP TABLE IF EXISTS t_posts; \ No newline at end of file diff --git a/db/migrations/000001_posts.up.sql b/db/migrations/000001_posts.up.sql new file mode 100644 index 0000000..37b365c --- /dev/null +++ b/db/migrations/000001_posts.up.sql @@ -0,0 +1,11 @@ +CREATE TABLE IF NOT EXISTS `t_posts` ( + `id` INT(11) NOT NULL, + `name` VARCHAR(256) NOT NULL DEFAULT '' COLLATE 'latin1_swedish_ci', + `content` MEDIUMTEXT NOT NULL COLLATE 'latin1_swedish_ci', + `ts` DATETIME NOT NULL DEFAULT current_timestamp(), + `ts_updated` DATETIME NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(), + PRIMARY KEY (`id`) USING BTREE +) +COLLATE='latin1_swedish_ci' +ENGINE=InnoDB +; diff --git a/dev/.keep b/dev/.keep new file mode 100644 index 0000000..e69de29 diff --git a/docker-compose.example.yaml b/docker-compose.example.yaml new file mode 100644 index 0000000..94deb52 --- /dev/null +++ b/docker-compose.example.yaml @@ -0,0 +1,35 @@ +version: '3.4' +services: + + freepad: + build: . + networks: + - backend + environment: + - MYSQL_URL + - MYSQL_ROOT_PASSWORD + - MYSQL_DATABASE + - MYSQL_USER + - MYSQL_PASSWORD + - MYSQL_PORT + depends_on: + - mariadb + + mariadb: + image: mariadb:10.2 + environment: + - MYSQL_ROOT_PASSWORD + - MYSQL_DATABASE + - MYSQL_USER + - MYSQL_PASSWORD + restart: unless-stopped + networks: + - backend + volumes: + - ./dev/mariadb:/var/lib/mysql + ports: + - 3306:3306 + +networks: + backend: + driver: bridge \ No newline at end of file diff --git a/run.sh b/run.sh new file mode 100644 index 0000000..89ce884 --- /dev/null +++ b/run.sh @@ -0,0 +1,5 @@ +echo "Running Migrations up"; +migrate -path /app/db/migrations/ -database "mysql://$MYSQL_USER:$MYSQL_PASSWORD@tcp($MYSQL_URL:$MYSQL_PORT)/$MYSQL_DATABASE" up + +echo "Running FreePad"; +cd src && go run .; \ No newline at end of file diff --git a/src/.gitignore b/src/.gitignore new file mode 100644 index 0000000..e69de29 diff --git a/src/controllers/post/post.go b/src/controllers/post/post.go new file mode 100644 index 0000000..1e01afa --- /dev/null +++ b/src/controllers/post/post.go @@ -0,0 +1,40 @@ +package post + +import "errors" + +var postList []*Post = []*Post{} + +var postMap map[string]Post = make(map[string]Post) + +func GetPostList() []*Post { + return postList +} + +func Create(name string, content string) (*Post, error) { + + if len(name) < 1 { + return nil, errors.New("the name of the post must contain at least 1 character") + } + + if len(name) > 256 { + return nil, errors.New("the name of the post must not exceed 256 characters") + } + + if len(content) > 16777200 { + return nil, errors.New("provided content is too long, please do not exceed ") + } + + // Initialize the post + myPost := Post{ + Name: name, + Content: content, + } + + // Set the post by name + postMap[name] = myPost + + // Add the post to the database + + // Return the post + return &myPost, nil +} diff --git a/src/controllers/post/post_types.go b/src/controllers/post/post_types.go new file mode 100644 index 0000000..61e19b1 --- /dev/null +++ b/src/controllers/post/post_types.go @@ -0,0 +1,6 @@ +package post + +type Post struct { + Name string `json:"name"` + Content string `json:"content"` +} diff --git a/src/go.mod b/src/go.mod new file mode 100644 index 0000000..0c368d5 --- /dev/null +++ b/src/go.mod @@ -0,0 +1,5 @@ +module github.com/JustKato/FreePad + +go 1.15 + +require github.com/gin-gonic/gin v1.7.7 diff --git a/src/go.sum b/src/go.sum new file mode 100644 index 0000000..5ee9be1 --- /dev/null +++ b/src/go.sum @@ -0,0 +1,54 @@ +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= +github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= +github.com/gin-gonic/gin v1.7.7 h1:3DoBmSbJbZAWqXJC3SLjAPfutPJJRN1U5pALB7EeTTs= +github.com/gin-gonic/gin v1.7.7/go.mod h1:axIBovoeJpVj8S3BwE0uPMTeReE4+AfFtqpqaZ1qq1U= +github.com/go-playground/assert/v2 v2.0.1 h1:MsBgLAaY856+nPRTKrp3/OZK38U/wa0CcBYNjji3q3A= +github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= +github.com/go-playground/locales v0.13.0 h1:HyWk6mgj5qFqCT5fjGBuRArbVDfE4hi8+e8ceBS/t7Q= +github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTMfnGwq/HNwmWNS8= +github.com/go-playground/universal-translator v0.17.0 h1:icxd5fm+REJzpZx7ZfpaD876Lmtgy7VtROAbHHXk8no= +github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA= +github.com/go-playground/validator/v10 v10.4.1 h1:pH2c5ADXtd66mxoE0Zm9SUhxE20r7aM3F26W0hOn+GE= +github.com/go-playground/validator/v10 v10.4.1/go.mod h1:nlOn6nFhuKACm19sB/8EGNn9GlaMV7XkbRSipzJ0Ii4= +github.com/golang/protobuf v1.3.3 h1:gyjaxf+svBWX08ZjK86iN9geUJF0H6gp2IRKX6Nf6/I= +github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw= +github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/json-iterator/go v1.1.9 h1:9yzud/Ht36ygwatGx56VwCZtlI/2AD15T1X2sjSuGns= +github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= +github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y= +github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII= +github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY= +github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= +github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc= +github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 h1:Esafd1046DLDQ0W1YjYsBW+p8U2u7vzgW2SQVmlNazg= +github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= +github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo= +github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw= +github.com/ugorji/go/codec v1.1.7 h1:2SvQaVZ1ouYrrKKwoSk2pzd4A9evlKJb9oTL+OaLUSs= +github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 h1:psW17arqaxU48Z5kZ0CQnkZWQJsqcURM6tKiBApRjXI= +golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200116001909-b77594299b42 h1:vEOn+mP2zCOVzKckCZy6YsCtDblrpj/w7B9nxGNELpg= +golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +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.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10= +gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/src/main.go b/src/main.go new file mode 100644 index 0000000..48d04d9 --- /dev/null +++ b/src/main.go @@ -0,0 +1,24 @@ +package main + +import ( + "github.com/JustKato/FreePad/routes" + "github.com/gin-gonic/gin" +) + +func main() { + + // Initialize the router + router := gin.Default() + + // Read HTML Templates + router.LoadHTMLGlob("templates/*") + + // Add Routes + // Bind health checks + routes.HealthRoutes(router) + // Bind /api + routes.ApiRoutes(router.Group("/api")) + + router.Run(":8080") + +} diff --git a/src/routes/routes_api.go b/src/routes/routes_api.go new file mode 100644 index 0000000..3922068 --- /dev/null +++ b/src/routes/routes_api.go @@ -0,0 +1,42 @@ +package routes + +import ( + "github.com/JustKato/FreePad/controllers/post" + "github.com/JustKato/FreePad/types" + "github.com/gin-gonic/gin" +) + +func ApiRoutes(route *gin.RouterGroup) { + + // Add in + route.GET("/", func(ctx *gin.Context) { + ctx.JSON(200, gin.H{ + "message": "Ok!", + }) + }) + + route.POST("/post", func(ctx *gin.Context) { + // Get the name of the post + postName := ctx.PostForm("name") + // Get the content of the post + postContent := ctx.PostForm("content") + + // Create my post + myPost, err := post.Create(postName, postContent) + if err != nil { + ctx.JSON(400, types.FreeError{ + Error: err.Error(), + Message: "There has been an error processing your request", + }) + } + + ctx.JSON(200, myPost) + + }) + + route.GET("/posts", func(ctx *gin.Context) { + // Return the post list + ctx.JSON(200, post.GetPostList()) + }) + +} diff --git a/src/routes/routes_health.go b/src/routes/routes_health.go new file mode 100644 index 0000000..a707ba5 --- /dev/null +++ b/src/routes/routes_health.go @@ -0,0 +1,18 @@ +package routes + +import "github.com/gin-gonic/gin" + +func HealthRoutes(route *gin.Engine) { + + // Add in + route.GET("/health", healthCheck) + +} + +func healthCheck(ctx *gin.Context) { + + ctx.JSON(200, gin.H{ + "message": "Healthy", + }) + +} diff --git a/src/templates/index.html b/src/templates/index.html new file mode 100644 index 0000000..e69de29 diff --git a/src/types/types_error.go b/src/types/types_error.go new file mode 100644 index 0000000..8495bc7 --- /dev/null +++ b/src/types/types_error.go @@ -0,0 +1,6 @@ +package types + +type FreeError struct { + Error string `json:"error"` + Message string `json:"message"` +}