Initial Commit

This commit is contained in:
Daniel Legt 2022-05-15 12:43:42 +03:00
commit 21b8605fc0
21 changed files with 282 additions and 0 deletions

4
.dockerignore Normal file
View File

@ -0,0 +1,4 @@
dev
.gitignore
LICENSE
README.md

6
.env.example Normal file
View File

@ -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

4
.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
dev/*
!dev/.keep
.env
docker-compose.yaml

21
Dockerfile Normal file
View File

@ -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"]

0
LICENSE Normal file
View File

0
README.md Normal file
View File

View File

@ -0,0 +1 @@
DROP TABLE IF EXISTS t_posts;

View File

@ -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
;

0
dev/.keep Normal file
View File

View File

@ -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

5
run.sh Normal file
View File

@ -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 .;

0
src/.gitignore vendored Normal file
View File

View File

@ -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
}

View File

@ -0,0 +1,6 @@
package post
type Post struct {
Name string `json:"name"`
Content string `json:"content"`
}

5
src/go.mod Normal file
View File

@ -0,0 +1,5 @@
module github.com/JustKato/FreePad
go 1.15
require github.com/gin-gonic/gin v1.7.7

54
src/go.sum Normal file
View File

@ -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=

24
src/main.go Normal file
View File

@ -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")
}

42
src/routes/routes_api.go Normal file
View File

@ -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())
})
}

View File

@ -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",
})
}

0
src/templates/index.html Normal file
View File

6
src/types/types_error.go Normal file
View File

@ -0,0 +1,6 @@
package types
type FreeError struct {
Error string `json:"error"`
Message string `json:"message"`
}