diff --git a/README.md b/README.md index 484e2ba..fe0661a 100644 --- a/README.md +++ b/README.md @@ -49,6 +49,7 @@ application with a Rust backend and a Postgres database. - [`Spring / PostgreSQL`](spring-postgres) - Sample Java application with Spring framework and a Postgres database. +- [`Vue / Golang / MySQL`](vue-golang-mysql/) - Vue3 application with Go backend and mysql database ## Single service samples diff --git a/vue-golang-mysql/README.md b/vue-golang-mysql/README.md new file mode 100644 index 0000000..2f3e8c6 --- /dev/null +++ b/vue-golang-mysql/README.md @@ -0,0 +1,62 @@ +## Compose sample application + +### Vue3 application with Go backend and mysql database + +Project structure +```yaml +. +├── docker-compose.yml +├── README.md +├── server +└── web +``` + +[docker-compose.yml](docker-compose.yml) +```yaml +version: '3' +services: + mydb: + restart: always + image: mysql:8.0 + ... + server: + build: + context: ./server + ... + web: + build: + context: ./web + ... +``` + + +## Deploy with docker composeTech + +```console +➜ sudo docker compose up -d +[+] Running 3/3 + ⠿ Container web Started 1.2s + ⠿ Container mydb Started 0.4s + ⠿ Container server Started 0.4s +``` + + +## Expected result + +Listing containers must show two containers running and the port mapping as below: +```console +CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES +2eed57a008a0 vue-golang-mysql-web "docker-entrypoint.s…" 3 minutes ago Up 3 minutes 0.0.0.0:80->3000/tcp, :::80->3000/tcp web +5bdbeda7b734 vue-golang-mysql-server "./code-paste" 3 minutes ago Up 3 minutes 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp server +d07aeb384642 mysql:8.0 "docker-entrypoint.s…" 3 minutes ago Up 3 minutes 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp mydb +``` + +After the application starts, navigate to http://localhost in your web browser to use app. + +![](https://s1.ax1x.com/2022/09/03/voZz3q.png) + +## Stop and remove the containers + +```console +docker compose down +``` \ No newline at end of file diff --git a/vue-golang-mysql/docker-compose.yml b/vue-golang-mysql/docker-compose.yml new file mode 100644 index 0000000..9e1f51b --- /dev/null +++ b/vue-golang-mysql/docker-compose.yml @@ -0,0 +1,35 @@ +version: '3' +services: + mydb: + restart: always + image: mysql:8.0 + environment: + MYSQL_ROOT_PASSWORD: '114514' + MYSQL_DATABASE: code_paste + container_name: mydb + ports: + - "3306:3306" + tty: true + server: + build: + context: ./server + restart: on-failure:10 + container_name: server + ports: + - "8080:8080" + environment: + GIN_MODE: release + tty: true + depends_on: + - mydb + web: + build: + context: ./web + args: + VITE_API_BASE_URL: http://127.0.0.1:8080/api + container_name: web + ports: + - "80:3000" + tty: true + depends_on: + - server \ No newline at end of file diff --git a/vue-golang-mysql/server/.gitignore b/vue-golang-mysql/server/.gitignore new file mode 100644 index 0000000..26ff970 --- /dev/null +++ b/vue-golang-mysql/server/.gitignore @@ -0,0 +1,2 @@ +log.txt +code-paste \ No newline at end of file diff --git a/vue-golang-mysql/server/Dockerfile b/vue-golang-mysql/server/Dockerfile new file mode 100644 index 0000000..d11fd4f --- /dev/null +++ b/vue-golang-mysql/server/Dockerfile @@ -0,0 +1,8 @@ +FROM golang:latest +ENV GO111MODULE=on +WORKDIR /server +COPY . . +RUN go env -w GOPROXY=https://goproxy.cn +RUN go build +EXPOSE 8080 +CMD ["./code-paste"] \ No newline at end of file diff --git a/vue-golang-mysql/server/config.toml b/vue-golang-mysql/server/config.toml new file mode 100644 index 0000000..93e55bf --- /dev/null +++ b/vue-golang-mysql/server/config.toml @@ -0,0 +1,8 @@ +[server] +port = 8080 + +[database] +host = "mydb" +port = 3306 +user = "root" +password = "114514" diff --git a/vue-golang-mysql/server/controllers/create.go b/vue-golang-mysql/server/controllers/create.go new file mode 100644 index 0000000..6eda3d3 --- /dev/null +++ b/vue-golang-mysql/server/controllers/create.go @@ -0,0 +1,30 @@ +package controllers + +import ( + "code-paste/database" + "code-paste/model" + "net/http" + "time" + + "github.com/gin-gonic/gin" + "github.com/google/uuid" +) + +func generateUUID() string { + return uuid.New().String() +} + +func CreatePaste(c *gin.Context) { + var p model.Paste + if err := c.BindJSON(&p); err != nil { + c.AbortWithStatus(http.StatusBadRequest) + return + } + p.ExpiredAt = time.Now().AddDate(0, 0, p.ExpiredDays) + p.ID = generateUUID() + database.DB.Create(&p) + c.JSON(http.StatusAccepted, gin.H{ + "status": "ok", + "uuid": p.ID, + }) +} diff --git a/vue-golang-mysql/server/controllers/read.go b/vue-golang-mysql/server/controllers/read.go new file mode 100644 index 0000000..9745227 --- /dev/null +++ b/vue-golang-mysql/server/controllers/read.go @@ -0,0 +1,26 @@ +package controllers + +import ( + "code-paste/database" + "code-paste/model" + "net/http" + "time" + + "github.com/gin-gonic/gin" +) + +func ReadPaste(c *gin.Context) { + id := c.Param("id") + var p model.Paste + res := database.DB.First(&p, "id = ?", id) + if res.Error != nil { + c.AbortWithStatus(http.StatusNotFound) + return + } + if p.ExpiredAt.Before(time.Now()) { + database.DB.Delete(&p) + ReadPaste(c) + return + } + c.JSON(http.StatusAccepted, p) +} diff --git a/vue-golang-mysql/server/cron/cron.go b/vue-golang-mysql/server/cron/cron.go new file mode 100644 index 0000000..c4896bf --- /dev/null +++ b/vue-golang-mysql/server/cron/cron.go @@ -0,0 +1,18 @@ +package cron + +import ( + "code-paste/database" + "code-paste/model" + "time" + + "github.com/robfig/cron/v3" +) + +func Start() { + c := cron.New() + + c.AddFunc("@midnight", func() { + now := time.Now().Format("2006-01-02 15:04:05") + database.DB.Where("expired_at <= ?", now).Delete(&model.Paste{}) + }) +} diff --git a/vue-golang-mysql/server/database/conn.go b/vue-golang-mysql/server/database/conn.go new file mode 100644 index 0000000..d06eed4 --- /dev/null +++ b/vue-golang-mysql/server/database/conn.go @@ -0,0 +1,38 @@ +package database + +import ( + "code-paste/model" + "fmt" + "log" + "os" + "time" + + "gorm.io/driver/mysql" + "gorm.io/gorm" + "gorm.io/gorm/logger" +) + +var DB *gorm.DB + +func Init(logFile *os.File, host, user, password string, port int) { + dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/code_paste?charset=utf8&parseTime=True&loc=Local", user, password, host, port) + newLogger := logger.New( + log.New(logFile, "\r\n", log.LstdFlags), + logger.Config{ + SlowThreshold: time.Second, + LogLevel: logger.Silent, + Colorful: false, + }, + ) + + db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{ + Logger: newLogger, + }) + if err != nil { + log.Fatal(err) + } + + DB = db + + db.AutoMigrate(model.Paste{}) +} diff --git a/vue-golang-mysql/server/go.mod b/vue-golang-mysql/server/go.mod new file mode 100644 index 0000000..a3a8b85 --- /dev/null +++ b/vue-golang-mysql/server/go.mod @@ -0,0 +1,42 @@ +module code-paste + +go 1.17 + +require ( + github.com/BurntSushi/toml v1.2.0 + github.com/gin-contrib/cors v1.4.0 + github.com/gin-gonic/gin v1.8.1 + github.com/google/uuid v1.3.0 + github.com/robfig/cron/v3 v3.0.1 + gorm.io/driver/mysql v1.3.6 + gorm.io/gorm v1.23.8 +) + +require ( + github.com/bradfitz/gomemcache v0.0.0-20220106215444-fb4bf637b56d // indirect + github.com/gin-contrib/cache v1.2.0 // indirect + github.com/gin-contrib/sse v0.1.0 // indirect + github.com/go-playground/locales v0.14.0 // indirect + github.com/go-playground/universal-translator v0.18.0 // indirect + github.com/go-playground/validator/v10 v10.10.0 // indirect + github.com/go-sql-driver/mysql v1.6.0 // indirect + github.com/goccy/go-json v0.9.7 // indirect + github.com/gomodule/redigo v1.8.9 // indirect + github.com/jinzhu/inflection v1.0.0 // indirect + github.com/jinzhu/now v1.1.5 // indirect + github.com/json-iterator/go v1.1.12 // indirect + github.com/leodido/go-urn v1.2.1 // indirect + github.com/mattn/go-isatty v0.0.14 // indirect + github.com/memcachier/mc/v3 v3.0.3 // indirect + github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect + github.com/modern-go/reflect2 v1.0.2 // indirect + github.com/pelletier/go-toml/v2 v2.0.1 // indirect + github.com/robfig/go-cache v0.0.0-20130306151617-9fc39e0dbf62 // indirect + github.com/ugorji/go/codec v1.2.7 // indirect + golang.org/x/crypto v0.0.0-20220214200702-86341886e292 // indirect + golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2 // indirect + golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069 // indirect + golang.org/x/text v0.3.6 // indirect + google.golang.org/protobuf v1.28.0 // indirect + gopkg.in/yaml.v2 v2.4.0 // indirect +) diff --git a/vue-golang-mysql/server/go.sum b/vue-golang-mysql/server/go.sum new file mode 100644 index 0000000..1c8f0f5 --- /dev/null +++ b/vue-golang-mysql/server/go.sum @@ -0,0 +1,122 @@ +github.com/BurntSushi/toml v1.2.0 h1:Rt8g24XnyGTyglgET/PRUNlrUeu9F5L+7FilkXfZgs0= +github.com/BurntSushi/toml v1.2.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ= +github.com/bradfitz/gomemcache v0.0.0-20220106215444-fb4bf637b56d h1:pVrfxiGfwelyab6n21ZBkbkmbevaf+WvMIiR7sr97hw= +github.com/bradfitz/gomemcache v0.0.0-20220106215444-fb4bf637b56d/go.mod h1:H0wQNHz2YrLsuXOZozoeDmnHXkNCRmMW0gwFWDfEZDA= +github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= +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/cache v1.2.0 h1:WA+AJR4kmHDTaLLShCHo/IeWVmmGRZ3Lsr3JQ46tFlE= +github.com/gin-contrib/cache v1.2.0/go.mod h1:2KkFL8PSnPF3Tt5E2Jpc3HWuBAUKqGZnClCFMm0tXQI= +github.com/gin-contrib/cors v1.4.0 h1:oJ6gwtUl3lqV0WEIwM/LxPF1QZ5qe2lGWdY2+bz7y0g= +github.com/gin-contrib/cors v1.4.0/go.mod h1:bs9pNM0x/UsmHPBWT2xZz9ROh8xYjYkiURUfmBoMlcs= +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.8.1 h1:4+fr/el88TOO3ewCmQr8cx/CtZ/umlIRIs5M4NTNjf8= +github.com/gin-gonic/gin v1.8.1/go.mod h1:ji8BvRH1azfM+SYow9zQ6SZMvR8qOMZHmsCuWR9tTTk= +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.14.0 h1:u50s323jtVGugKlcYeyzC0etD1HifMjqmJqb8WugfUU= +github.com/go-playground/locales v0.14.0/go.mod h1:sawfccIbzZTqEDETgFXqTho0QybSa7l++s0DH+LDiLs= +github.com/go-playground/universal-translator v0.18.0 h1:82dyy6p4OuJq4/CByFNOn/jYrnRPArHwAcmLoJZxyho= +github.com/go-playground/universal-translator v0.18.0/go.mod h1:UvRDBj+xPUEGrFYl+lu/H90nyDXpg0fqeB/AQUGNTVA= +github.com/go-playground/validator/v10 v10.10.0 h1:I7mrTYv78z8k8VXa/qJlOlEXn/nBh+BF8dHX5nt/dr0= +github.com/go-playground/validator/v10 v10.10.0/go.mod h1:74x4gJWsvQexRdW8Pn3dXSGrTK4nAUsbPlLADvpJkos= +github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE= +github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= +github.com/goccy/go-json v0.9.7 h1:IcB+Aqpx/iMHu5Yooh7jEzJk1JZ7Pjtmys2ukPr7EeM= +github.com/goccy/go-json v0.9.7/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= +github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= +github.com/gomodule/redigo v1.8.9 h1:Sl3u+2BI/kk+VEatbj0scLdrFhjPmbxOc1myhDP41ws= +github.com/gomodule/redigo v1.8.9/go.mod h1:7ArFNvsTjH8GMMzB4uy1snslv2BwmginuMs06a1uzZE= +github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= +github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= +github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +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.4/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= +github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= +github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= +github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= +github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= +github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= +github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= +github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0= +github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= +github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= +github.com/leodido/go-urn v1.2.1 h1:BqpAaACuzVSgi/VLzGZIobT2z4v53pjosyNd9Yv6n/w= +github.com/leodido/go-urn v1.2.1/go.mod h1:zt4jvISO2HfUBqxjfIshjdMTYS56ZS/qv49ictyFfxY= +github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y= +github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94= +github.com/memcachier/mc/v3 v3.0.3 h1:qii+lDiPKi36O4Xg+HVKwHu6Oq+Gt17b+uEiA0Drwv4= +github.com/memcachier/mc/v3 v3.0.3/go.mod h1:GzjocBahcXPxt2cmqzknrgqCOmMxiSzhVKPOe90Tpug= +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 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= +github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= +github.com/pelletier/go-toml/v2 v2.0.1 h1:8e3L2cCQzLFi2CR4g7vGFuFxX7Jl1kKX8gW+iV0GUKU= +github.com/pelletier/go-toml/v2 v2.0.1/go.mod h1:r9LEWfGN8R5k0VXJ+0BkIe7MYkRdwZOjgMj2KwnJFUo= +github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA= +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/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs= +github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro= +github.com/robfig/go-cache v0.0.0-20130306151617-9fc39e0dbf62 h1:pyecQtsPmlkCsMkYhT5iZ+sUXuwee+OvfuJjinEA3ko= +github.com/robfig/go-cache v0.0.0-20130306151617-9fc39e0dbf62/go.mod h1:65XQgovT59RWatovFwnwocoUxiI/eENTnOY5GK3STuY= +github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc= +github.com/rogpeppe/go-internal v1.8.0 h1:FCbCCtXNOY3UtUuHUYaghJg4y7Fd14rXifAYUAtL9R8= +github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/ugorji/go v1.2.7 h1:qYhyWUUd6WbiM+C6JZAUkIJt/1WrjzNHY9+KCIjVqTo= +github.com/ugorji/go v1.2.7/go.mod h1:nF9osbDWLy6bDVv/Rtoh6QgnvNDpmCalQV5urGCCS6M= +github.com/ugorji/go/codec v1.2.7 h1:YPXUKf7fYbp/y8xloBqZOw2qaVggbfwMlI8WM3wZUJ0= +github.com/ugorji/go/codec v1.2.7/go.mod h1:WGN1fab3R1fzQlVQTkfxVtIBhWDRqOviHU95kRgeqEY= +golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= +golang.org/x/crypto v0.0.0-20220214200702-86341886e292 h1:f+lwQ+GtmgoY+A2YaQxlSOnDjXcQ7ZRLWOHbC6HtRqE= +golang.org/x/crypto v0.0.0-20220214200702-86341886e292/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= +golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2 h1:CIJ76btIcR3eFI5EgSo6k1qKw9KJexJuRLI9G7Hp5wE= +golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069 h1:siQdpVirKtzPhKl3lZWozZraCFObP8S1v6PRp0bLrtU= +golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M= +golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= +google.golang.org/protobuf v1.28.0 h1:w43yiav+6bVFTBQFZX0r7ipe9JQ1QsbMgHwbBziscLw= +google.golang.org/protobuf v1.28.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= +gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= +gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= +gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gorm.io/driver/mysql v1.3.6 h1:BhX1Y/RyALb+T9bZ3t07wLnPZBukt+IRkMn8UZSNbGM= +gorm.io/driver/mysql v1.3.6/go.mod h1:sSIebwZAVPiT+27jK9HIwvsqOGKx3YMPmrA3mBJR10c= +gorm.io/gorm v1.23.8 h1:h8sGJ+biDgBA1AD1Ha9gFCx7h8npU7AsLdlkX0n2TpE= +gorm.io/gorm v1.23.8/go.mod h1:l2lP/RyAtc1ynaTjFksBde/O8v9oOGIApu2/xRitmZk= diff --git a/vue-golang-mysql/server/main.go b/vue-golang-mysql/server/main.go new file mode 100644 index 0000000..63e0e51 --- /dev/null +++ b/vue-golang-mysql/server/main.go @@ -0,0 +1,58 @@ +package main + +import ( + "code-paste/controllers" + "code-paste/cron" + "code-paste/database" + "fmt" + "os" + "time" + + "github.com/BurntSushi/toml" + "github.com/gin-contrib/cache" + "github.com/gin-contrib/cache/persistence" + "github.com/gin-contrib/cors" + "github.com/gin-gonic/gin" +) + +type configs struct { + Server serverConfig + Database databaseConfig +} + +type serverConfig struct { + Port int +} + +type databaseConfig struct { + Host string + User string + Password string + Port int +} + +func main() { + var config configs + toml.DecodeFile("./config.toml", &config) + + logFile, _ := os.Create("log.txt") + + database.Init(logFile, config.Database.Host, config.Database.User, config.Database.Password, config.Database.Port) + + cron.Start() + + e := gin.Default() + + e.Use(gin.LoggerWithWriter(logFile)) + + corsConfig := cors.DefaultConfig() + corsConfig.AllowAllOrigins = true + e.Use(cors.New(corsConfig)) + + store := persistence.NewInMemoryStore(time.Second) + + e.POST("/api/create", controllers.CreatePaste) + e.GET("/api/read/:id", cache.CachePage(store, time.Minute, controllers.ReadPaste)) + + e.Run(fmt.Sprintf(":%d", config.Server.Port)) +} diff --git a/vue-golang-mysql/server/model/paste.go b/vue-golang-mysql/server/model/paste.go new file mode 100644 index 0000000..4fcc642 --- /dev/null +++ b/vue-golang-mysql/server/model/paste.go @@ -0,0 +1,11 @@ +package model + +import "time" + +type Paste struct { + ID string `json:"id" gorm:"primaryKey"` + ExpiredAt time.Time `json:"expired_at"` + ExpiredDays int `json:"expired_days"` + Type string `json:"type"` + Data string `json:"data"` +} diff --git a/vue-golang-mysql/web/.dockerignore b/vue-golang-mysql/web/.dockerignore new file mode 100644 index 0000000..07e6e47 --- /dev/null +++ b/vue-golang-mysql/web/.dockerignore @@ -0,0 +1 @@ +/node_modules diff --git a/vue-golang-mysql/web/.gitignore b/vue-golang-mysql/web/.gitignore new file mode 100644 index 0000000..912c396 --- /dev/null +++ b/vue-golang-mysql/web/.gitignore @@ -0,0 +1,9 @@ +.DS_Store +.vite-ssg-dist +.vite-ssg-temp +*.local +dist +dist-ssr +node_modules +.idea/ +*.log diff --git a/vue-golang-mysql/web/.npmrc b/vue-golang-mysql/web/.npmrc new file mode 100644 index 0000000..cf04042 --- /dev/null +++ b/vue-golang-mysql/web/.npmrc @@ -0,0 +1,2 @@ +shamefully-hoist=true +strict-peer-dependencies=false diff --git a/vue-golang-mysql/web/.vscode/extensions.json b/vue-golang-mysql/web/.vscode/extensions.json new file mode 100644 index 0000000..26b0f74 --- /dev/null +++ b/vue-golang-mysql/web/.vscode/extensions.json @@ -0,0 +1,10 @@ +{ + "recommendations": [ + "antfu.vite", + "antfu.iconify", + "antfu.unocss", + "antfu.goto-alias", + "vue.volar", + "dbaeumer.vscode-eslint" + ] +} diff --git a/vue-golang-mysql/web/.vscode/settings.json b/vue-golang-mysql/web/.vscode/settings.json new file mode 100644 index 0000000..b994829 --- /dev/null +++ b/vue-golang-mysql/web/.vscode/settings.json @@ -0,0 +1,10 @@ +{ + "cSpell.words": ["Vitesse"], + "prettier.enable": false, + "editor.codeActionsOnSave": { + "source.fixAll.eslint": true + }, + "files.associations": { + "*.css": "postcss" + } +} diff --git a/vue-golang-mysql/web/Dockerfile b/vue-golang-mysql/web/Dockerfile new file mode 100644 index 0000000..8432640 --- /dev/null +++ b/vue-golang-mysql/web/Dockerfile @@ -0,0 +1,12 @@ +FROM node:16 +WORKDIR /web +RUN curl -f https://get.pnpm.io/v6.16.js | node - add --global pnpm +COPY .npmrc package.json pnpm-lock.yaml ./ +RUN pnpm install +COPY . . +ARG VITE_API_BASE_URL +RUN echo VITE_API_BASE_URL=${VITE_API_BASE_URL} > .env +RUN pnpm build +RUN npm install --global serve +EXPOSE 3000 +CMD [ "serve", "-n", "-s", "dist" ] \ No newline at end of file diff --git a/vue-golang-mysql/web/LICENSE b/vue-golang-mysql/web/LICENSE new file mode 100644 index 0000000..670b1b3 --- /dev/null +++ b/vue-golang-mysql/web/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2020-PRESENT Anthony Fu + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/vue-golang-mysql/web/README.md b/vue-golang-mysql/web/README.md new file mode 100644 index 0000000..26f47ca --- /dev/null +++ b/vue-golang-mysql/web/README.md @@ -0,0 +1 @@ +# Code Paste \ No newline at end of file diff --git a/vue-golang-mysql/web/README.zh-CN.md b/vue-golang-mysql/web/README.zh-CN.md new file mode 100644 index 0000000..3d42f66 --- /dev/null +++ b/vue-golang-mysql/web/README.zh-CN.md @@ -0,0 +1,86 @@ +
+ +
+ ++English | 简体中文 +
+ +