Initial commit

main
Antonio De Lucreziis 2 years ago
commit 7b9204db4a

9
.gitignore vendored

@ -0,0 +1,9 @@
# NodeJS
node_modules/
# Server
server
# Local Files
.env
*.local*

@ -0,0 +1,59 @@
# Go Vite Kit
Minimal boilerplate project for a Golang server using [Fiber](https://github.com/gofiber/fiber) and [ViteJS](https://vitejs.dev/) for static pages
## Features
- ⚡️ [Go Fiber](https://github.com/gofiber/fiber)
- 📦 [ViteJS](http://vitejs.dev/)
- 🎨 [Sass](https://sass-lang.com/)
- 🗄️ [Sqlite3](https://github.com/mattn/go-sqlite3)
## Architecture
- `_frontend/`
This is a Vite project for building all the static pages used by this app.
- `database/`
Module with a `Database` interface and two implementation: `memDB` is an in-memory database for testing purposes while `sqliteDB` is a wrapper for working with an SQLite database.
- `routes/`
Various functions for configuring all the server routes.
## Usage
First install the required npm packages
```bash
$ cd _frontend
_frontend/ $ npm install
```
### Development
```bash
# Development
$ MODE=dev go run -v .
# Development with watcher
$ fd -e go | MODE=dev entr -r go run -v .
```
### Production
First build the `_frontend/dist` folder using
```bash
$ cd _frontend
$ npm run build
```
and then
```bash
# Production
$ go run -v .
```

@ -0,0 +1,16 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Homepage</title>
<link rel="stylesheet" href="/styles/main.scss">
</head>
<body>
<h1>Homepage</h1>
<script type="module" src="/src/home.js"></script>
</body>
</html>

File diff suppressed because it is too large Load Diff

@ -0,0 +1,16 @@
{
"name": "frontend",
"version": "1.0.0",
"description": "Javascript frontend for this Golang server built using ViteJS",
"main": "index.js",
"scripts": {
"dev": "vite",
"build": "vite build",
"start": "vite preview"
},
"license": "MIT",
"devDependencies": {
"sass": "^1.53.0",
"vite": "^2.9.13"
}
}

@ -0,0 +1,9 @@
async function main() {
const res = await fetch('/api/status')
if (res.ok) {
console.log('Server online')
}
}
main()

@ -0,0 +1,43 @@
*,
*::before,
*::after {
box-sizing: border-box;
}
body {
margin: 0;
width: 100%;
min-height: 100vh;
font-family: 'Inter', 'Segoe UI', 'Helvetica', 'Arial', sans-serif;
font-size: 16px;
}
// Headings
$base-font-size: 18px;
$heading-scale: 1.33;
@function pow($number, $exponent) {
$value: 1;
@if $exponent > 0 {
@for $i from 1 through $exponent {
$value: $value * $number;
}
}
@return $value;
}
@for $i from 1 through 5 {
h#{$i} {
margin: 0;
$factor: pow($heading-scale, 5 - $i);
font-size: $base-font-size * $factor;
line-height: 1.5;
}
}

@ -0,0 +1,16 @@
import { defineConfig } from 'vite'
export default defineConfig({
build: {
rollupOptions: {
input: {
'/index': 'index.html',
},
},
},
server: {
proxy: {
'/api': 'http://127.0.0.1:4000/',
},
},
})

@ -0,0 +1,37 @@
package main
import (
"log"
"os"
"github.com/joho/godotenv"
)
var Config struct {
Mode string
Host string
BaseURL string
}
func loadEnv(key string, defaultValue ...string) string {
env := os.Getenv(key)
if len(defaultValue) > 0 && env == "" {
env = defaultValue[0]
}
log.Printf("Environment variable %s = %q", key, env)
return env
}
func init() {
// Setup logger
log.SetFlags(log.Lshortfile | log.Ltime | log.Ldate)
// Load Config
godotenv.Load()
Config.Mode = loadEnv(os.Getenv("MODE"), "development")
Config.Host = loadEnv(os.Getenv("HOST"), ":4000")
Config.BaseURL = loadEnv(os.Getenv("HOST"), "http://localhost:4000")
}

@ -0,0 +1,3 @@
package database
type Database interface{}

@ -0,0 +1,7 @@
package database
type memDB struct {}
func NewInMemoryDB() Database {
return &memDB{}
}

@ -0,0 +1,19 @@
package database
import "database/sql"
// Uncomment if actually using SQLite, the first build will be fairly slow
// import _ "github.com/mattn/go-sqlite3"
type sqliteDatabase struct {
Db *sql.DB
}
func NewSQLite(filename string) (Database, error) {
db, err := sql.Open("sqlite3", filename)
if err != nil {
return nil, err
}
return &sqliteDatabase{db}, nil
}

@ -0,0 +1,17 @@
module server
go 1.18
require (
github.com/gofiber/fiber/v2 v2.34.1
github.com/joho/godotenv v1.4.0
)
require (
github.com/andybalholm/brotli v1.0.4 // indirect
github.com/klauspost/compress v1.15.0 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasthttp v1.37.0 // indirect
github.com/valyala/tcplisten v1.0.0 // indirect
golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9 // indirect
)

@ -0,0 +1,28 @@
github.com/andybalholm/brotli v1.0.4 h1:V7DdXeJtZscaqfNuAdSRuRFzuiKlHSC/Zh3zl9qY3JY=
github.com/andybalholm/brotli v1.0.4/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
github.com/gofiber/fiber/v2 v2.34.1 h1:C6saXB7385HvtXX+XMzc5Dqj5S/aEXOfKCW7JNep4rA=
github.com/gofiber/fiber/v2 v2.34.1/go.mod h1:ozRQfS+D7EL1+hMH+gutku0kfx1wLX4hAxDCtDzpj4U=
github.com/joho/godotenv v1.4.0 h1:3l4+N6zfMWnkbPEXKng2o2/MR5mSwTrBih4ZEkkz1lg=
github.com/joho/godotenv v1.4.0/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/klauspost/compress v1.15.0 h1:xqfchp4whNFxn5A4XFyyYtitiWI8Hy5EW59jEwcyL6U=
github.com/klauspost/compress v1.15.0/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasthttp v1.37.0 h1:7WHCyI7EAkQMVmrfBhWTCOaeROb1aCBiTopx63LkMbE=
github.com/valyala/fasthttp v1.37.0/go.mod h1:t/G+3rLek+CyY9bnIE+YlMRddxVAAGjhxndDB4i4C0I=
github.com/valyala/tcplisten v1.0.0 h1:rBHj/Xf+E1tRGZyWIWwJDiRY0zc1Js+CV5DqwacVSA8=
github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc=
golang.org/x/crypto v0.0.0-20220214200702-86341886e292/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20220225172249-27dd8689420f/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
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-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9 h1:nhht2DYV/Sn3qOayu8lM+cU1ii9sTLUeBQwQQfUHtrs=
golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=

@ -0,0 +1,35 @@
package main
import (
"log"
"os/exec"
"server/database"
"server/routes"
"strings"
"github.com/gofiber/fiber/v2"
)
func main() {
db := database.NewInMemoryDB()
router := &routes.Router{
Database: db,
}
app := fiber.New()
app.Static("/", "/_frontend/dist")
app.Route("/api", router.Api)
if strings.HasPrefix(Config.Mode, "dev") {
log.Printf(`Running dev server for frontend: "npm run dev"`)
err := exec.Command("sh", "-c", "cd _frontend/ && npm run dev").Start()
if err != nil {
log.Fatal(err)
}
}
log.Fatal(app.Listen(Config.Host))
}

@ -0,0 +1,9 @@
package routes
import "github.com/gofiber/fiber/v2"
func (r *Router) Api(api fiber.Router) {
api.Get("/status", func(c *fiber.Ctx) error {
return c.JSON("ok")
})
}

@ -0,0 +1,7 @@
package routes
import "server/database"
type Router struct {
Database database.Database
}
Loading…
Cancel
Save