Coder Social home page Coder Social logo

zipzippy / svelteapp-typescript-go Goto Github PK

View Code? Open in Web Editor NEW

This project forked from alankrantas/svelteapp-typescript-go

0.0 0.0 0.0 1.84 MB

The Essential TypeScript Demo in Svelte/SvelteKit Version with Go-Gin Backend and SQLite Database

License: MIT License

Shell 0.76% JavaScript 5.46% Go 30.78% TypeScript 23.48% HTML 1.52% Dockerfile 3.60% Svelte 34.39%

svelteapp-typescript-go's Introduction

A Svelte Demo Web App with Golang Backend Service and SQLite

This is a simple shopping demo app, inspired by the same Angular/React/Vue.js examples in Essential Typescript by Adam Freeman:

  • The frontend is built with Svelte and SvelteKit using TypeScript. @sveltejs/adapter-static is used to generate a static JavaScript production. The CSS styles are provided by Bootstrap.
  • The backend is built with Golang - a web server/REST APIs using gin plus the go-sqlite3 package as the SQLite database driver. In theory you can swap the DB driver to the ones you like.
  • A Dockerfile that creates a single container with multi-stage builds (image size ~26 MB).

Adam Freeman's original projects use json-server on an Express server as mock API services. I did not change the spec of the services for the sake of demonstration. Right now, like all the original examples, the app only reads product lists and write order data.

The purpose of project is an experiment to build a small, modern and self-contained full-stack monolithic application, but it is not meant to be a practical template for any real world applications.

ezgif-5-22d3d39425

A similar version using Vue.js, Express, MongoDB and Docker Compose can be found here.

Setup Local Project

For local development you'll need

If you are using Windows the go-sqlite3 package needs GCC to compile, which can be installed with MinGW (choose MinGW32-base, MinGW32-gcc-g++ and MinGW32-gcc-objc package, then add \MinGW\bin to $PATH). On Linux you can get it by installing package build-essential.

Prepare the project:

git clone https://github.com/alankrantas/svelteapp-typescript-go.git
cd svelteapp-typescript-go
npm run setup

Run in Dev Mode (no backend required)

npm run dev

Run the Svelte app in dev mode: the app will not call any backend APIs, instead it returns built-in mock product data and the returned order number is always 1.

Build and Run Production

npm run setup-all

Install Svelte and Golang app dependencies. Equivalent to

npm i
npm prune
go get ./...

You can run npm run setup to install Svelte app's dependencies only.

npm run upgrade-all

Upgrade all Svelte/Go dependencies. Equivalent to

npx npm-check-updates -u
npm i
npm prune
go get -u ./...

You can run npm run upgrade to upgrade the Svelte app packages only.

npm run build-all

Build the Svelte production and Golang executable binary. Equivalent to

npm run check
npx vite build
go build .

You can run npm run build to build the Svelte app only.

npm run serve or npm run serve-win

npm run serve-win is for Windows users since NPM would use CMD to run bash scripts.

Start a server at http://localhost:8080. Equivalent to

./main

Then open http://localhost:8080. You can also use custom address and port like

./main -host 127.0.0.1 -port 8080

Build and Run the Docker Container

npm run docker and npm run docker-run

Does not require to install local packages or build productions first

Generate a Docker image then run it. Equivalent to

docker build . -t svelte-ts-go -f Dockerfile
docker run -p 8080:8080 --rm svelte-ts-go

Then open http://localhost:8080.

Open Project in DevContainer/CodeSpace

Does not require to install local packages or build productions first. You need Docker and VS Code (with the DevContainer extension installed).

The project has a .devcontainer/devcontainer.json which can create a Ubuntu container with both Node.js and Golang installed. It would also run a bash script to install dependencies and build the app for you.

SQLite DB Schemes and Data

The database (./db/data.sqlite3) in this repo already contains the table products with 9 product records (copied from here) and an empty table orders. You can use DB Browser for SQLite to read the database.

Here's the SQL statements to recreate them:

CREATE TABLE "products" (
	"id"	INTEGER NOT NULL UNIQUE,
	"name"	TEXT NOT NULL,
	"category"	TEXT NOT NULL,
	"description"	TEXT,
	"price"	REAL NOT NULL,
	PRIMARY KEY("id" AUTOINCREMENT)
);

CREATE TABLE "orders" (
	"id"	INTEGER NOT NULL,
	"product_id"	INTEGER NOT NULL,
	"quantity"	INTEGER NOT NULL
);

INSERT INTO "main"."products" ("id", "name", "category", "description", "price") VALUES ('1', 'Kayak', 'Watersports', 'A boat for one person', '275.0');
INSERT INTO "main"."products" ("id", "name", "category", "description", "price") VALUES ('2', 'Lifejacket', 'Watersports', 'Protective and fashionable', '48.95');
INSERT INTO "main"."products" ("id", "name", "category", "description", "price") VALUES ('3', 'Soccer Ball', 'Soccer', 'FIFA-approved size and weight', '19.5');
INSERT INTO "main"."products" ("id", "name", "category", "description", "price") VALUES ('4', 'Corner Flags', 'Soccer', 'Give your playing field a professional touch', '34.95');
INSERT INTO "main"."products" ("id", "name", "category", "description", "price") VALUES ('5', 'Stadium', 'Soccer', 'Flat-packed 35,000-seat stadium', '79500.0');
INSERT INTO "main"."products" ("id", "name", "category", "description", "price") VALUES ('6', 'Thinking Cap', 'Chess', 'Improve brain efficiency by 75%', '16.0');
INSERT INTO "main"."products" ("id", "name", "category", "description", "price") VALUES ('7', 'Unsteady Chair', 'Chess', 'Secretly give your opponent a disadvantage', '29.95');
INSERT INTO "main"."products" ("id", "name", "category", "description", "price") VALUES ('8', 'Human Chess Board', 'Chess', 'A fun game for the family', '75.0');
INSERT INTO "main"."products" ("id", "name", "category", "description", "price") VALUES ('9', 'Bling Bling King', 'Chess', 'Gold-plated, diamond-studded King', '1200.0');

svelteapp-typescript-go's People

Contributors

alankrantas avatar dependabot[bot] avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.