diff --git a/build.sh b/build.sh index 9dbfffd..6d8799b 100755 --- a/build.sh +++ b/build.sh @@ -10,7 +10,6 @@ MYDIR=`pwd`; cd src; # Build echo "Building..." -export RELEASE_MODE=1; -CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o ../dist/freepad . +GIN_MODE=release CGO_ENABLED=0 GOOS=linux GIN_MODE=release go build -a -installsuffix cgo -o ../dist/freepad . # Go back! cd $MYDIR; \ No newline at end of file diff --git a/dist/freepad b/dist/freepad index 5682a80..7124947 100755 Binary files a/dist/freepad and b/dist/freepad differ diff --git a/dist/static/js/main.js b/dist/static/js/main.js index 1b3c9d0..0e6aec1 100644 --- a/dist/static/js/main.js +++ b/dist/static/js/main.js @@ -80,3 +80,27 @@ function goToPost() { } } } + +function getQr(link = `https://justkato.me/`) { + return new Promise((_r, _e) => { + let formData = new FormData(); + formData.append('link', link); + + // Send out a fetch request + fetch("/api/qr", { + method: "post", + body: formData, + }) + .then( result => { + result.json() + .then( rez => { + return _r(rez); + }) + }) + .catch( error => { + console.error(error); + alert(error); + return _e(error); + }) + }) +} \ No newline at end of file diff --git a/dist/templates/pages/page.html b/dist/templates/pages/page.html index 49b0742..a721ab3 100644 --- a/dist/templates/pages/page.html +++ b/dist/templates/pages/page.html @@ -1,9 +1,16 @@ {{ template "inc/header.html" .}} + +

FreePad

+

Reading from {{.domain_base}}/{{.title}}


@@ -11,7 +18,8 @@
Back Home - Save + QR + Save

Status: Loaded

@@ -19,10 +27,29 @@
{{ template "inc/footer.html" .}} - \ No newline at end of file + + + \ No newline at end of file diff --git a/rundev.sh b/rundev.sh index 92c2ff7..2f1a9a7 100755 --- a/rundev.sh +++ b/rundev.sh @@ -3,8 +3,9 @@ echo "Removing old"; rm dist/freepad; +source ../.env # Yeah, this is my solution -export DOMAIN_BASE CACHE_MAP_LIMIT API_BAN_LIMIT DATABASE_DRIVER MYSQL_ROOT_PASSWORD MYSQL_DATABASE MYSQL_USER MYSQL_PASSWORD MYSQL_URL MYSQL_PORT +export DOMAIN_BASE CACHE_MAP_LIMIT API_BAN_LIMIT DATABASE_DRIVER MYSQL_ROOT_PASSWORD MYSQL_DATABASE MYSQL_USER MYSQL_PASSWORD MYSQL_URL MYSQL_PORT IS_DEV # Remember current path MYDIR=`pwd`; @@ -12,14 +13,11 @@ MYDIR=`pwd`; cd src; # Build echo "Building..." -unset RELEASE_MODE; + go build -o ../dist/freepad . # Go back! cd $MYDIR; -MYPATH=`pwd`; - cd dist -source ../.env -./freepad && cd $MYPATH; \ No newline at end of file +./freepad && cd $MYDIR; \ No newline at end of file diff --git a/src/go.mod b/src/go.mod index 2a6aff4..ca92a91 100644 --- a/src/go.mod +++ b/src/go.mod @@ -7,5 +7,6 @@ require ( github.com/go-sql-driver/mysql v1.6.0 github.com/golang-migrate/migrate/v4 v4.15.2 github.com/mattn/go-sqlite3 v1.14.13 + github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e // indirect github.com/ulule/limiter/v3 v3.10.0 ) diff --git a/src/go.sum b/src/go.sum index f92cba2..36374bb 100644 --- a/src/go.sum +++ b/src/go.sum @@ -1014,6 +1014,8 @@ github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrf github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE= github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= +github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e h1:MRM5ITcdelLK2j1vwZ3Je0FKVCfqOLp5zO6trqMLYs0= +github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e/go.mod h1:XV66xRDqSt+GTGFMVlhk3ULuV0y9ZmzeVGR4mloJI3M= github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc= github.com/smartystreets/goconvey v0.0.0-20190330032615-68dc04aab96a/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA= github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA= diff --git a/src/main.go b/src/main.go index 212f771..bd22282 100644 --- a/src/main.go +++ b/src/main.go @@ -10,8 +10,8 @@ import ( func main() { - _, isRelease := os.LookupEnv("RELEASE_MODE") - if isRelease { + _, isDevelopment := os.LookupEnv("IS_DEV") + if isDevelopment { gin.SetMode(gin.ReleaseMode) } diff --git a/src/routes/routes_api.go b/src/routes/routes_api.go index a5624c8..d9d5aed 100644 --- a/src/routes/routes_api.go +++ b/src/routes/routes_api.go @@ -1,6 +1,7 @@ package routes import ( + "encoding/base64" "fmt" "net/url" @@ -8,6 +9,7 @@ import ( "github.com/JustKato/FreePad/helper" "github.com/JustKato/FreePad/types" "github.com/gin-gonic/gin" + "github.com/skip2/go-qrcode" ) func ApiRoutes(route *gin.RouterGroup) { @@ -66,6 +68,31 @@ func ApiRoutes(route *gin.RouterGroup) { // Add in health checks route.GET("/health", healthCheck) + route.POST("/qr", func(ctx *gin.Context) { + + // Get the name of the post + link := ctx.PostForm("link") + + // store the png somewhere + var png []byte + + // Encode the link into a qr code + png, err := qrcode.Encode(link, qrcode.High, 512) + if err != nil { + ctx.JSON(200, types.FreeError{ + Error: fmt.Sprint(err), + Message: "Failed to convert qr Code", + }) + return + } + + // Write the png to the response + ctx.JSON(200, gin.H{ + "message": "Succesfully generated the QR", + "qr": "data:image/jpeg;base64," + base64.StdEncoding.EncodeToString(png), + }) + }) + } func healthCheck(ctx *gin.Context) {