Initial commit
commit
8d16099c10
@ -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 @@
|
||||
const l=function(){const t=document.createElement("link").relList;if(t&&t.supports&&t.supports("modulepreload"))return;for(const e of document.querySelectorAll('link[rel="modulepreload"]'))r(e);new MutationObserver(e=>{for(const i of e)if(i.type==="childList")for(const n of i.addedNodes)n.tagName==="LINK"&&n.rel==="modulepreload"&&r(n)}).observe(document,{childList:!0,subtree:!0});function s(e){const i={};return e.integrity&&(i.integrity=e.integrity),e.referrerpolicy&&(i.referrerPolicy=e.referrerpolicy),e.crossorigin==="use-credentials"?i.credentials="include":e.crossorigin==="anonymous"?i.credentials="omit":i.credentials="same-origin",i}function r(e){if(e.ep)return;e.ep=!0;const i=s(e);fetch(e.href,i)}};l();async function f(){(await fetch("/api/status")).ok&&console.log("Server online")}f();
|
@ -0,0 +1 @@
|
||||
*,*: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}h1{margin:0;font-size:56.32212978px;line-height:1.5}h2{margin:0;font-size:42.347466px;line-height:1.5}h3{margin:0;font-size:31.8402px;line-height:1.5}h4{margin:0;font-size:23.94px;line-height:1.5}h5{margin:0;font-size:18px;line-height:1.5}
|
@ -0,0 +1,18 @@
|
||||
<!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>
|
||||
|
||||
|
||||
<script type="module" crossorigin src="/assets/index.37324337.js"></script>
|
||||
<link rel="stylesheet" href="/assets/index.9acd5e50.css">
|
||||
</head>
|
||||
<body>
|
||||
<h1>Homepage</h1>
|
||||
|
||||
|
||||
</body>
|
||||
</html>
|
@ -0,0 +1,15 @@
|
||||
<!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>
|
||||
<main></main>
|
||||
<script type="module" src="/src/home.jsx"></script>
|
||||
</body>
|
||||
</html>
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,24 @@
|
||||
{
|
||||
"name": "frontend",
|
||||
"version": "1.0.0",
|
||||
"description": "Javascript frontend for this Golang server built using ViteJS",
|
||||
"main": "index.js",
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"dev": "node server.js",
|
||||
"build": "vite build",
|
||||
"start": "vite preview"
|
||||
},
|
||||
"license": "MIT",
|
||||
"devDependencies": {
|
||||
"@preact/preset-vite": "^2.3.0",
|
||||
"express": "^4.18.1",
|
||||
"fast-glob": "^3.2.11",
|
||||
"mime-types": "^2.1.35",
|
||||
"sass": "^1.53.0",
|
||||
"vite": "^2.9.13"
|
||||
},
|
||||
"dependencies": {
|
||||
"preact": "^10.8.2"
|
||||
}
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
import { dirname, resolve } from 'path'
|
||||
import express from 'express'
|
||||
import { createServer as createViteServer } from 'vite'
|
||||
import { fileURLToPath } from 'url'
|
||||
|
||||
const __dirname = dirname(fileURLToPath(import.meta.url))
|
||||
|
||||
async function createServer(customHtmlRoutes) {
|
||||
const app = express()
|
||||
|
||||
// In middleware mode, if you want to use Vite's own HTML serving logic
|
||||
// use `'html'` as the `middlewareMode` (ref https://vitejs.dev/config/#server-middlewaremode)
|
||||
const vite = await createViteServer({
|
||||
server: { middlewareMode: 'html' },
|
||||
})
|
||||
|
||||
for (const [route, file] of Object.entries(customHtmlRoutes)) {
|
||||
app.get(route, (req, res) => {
|
||||
const filePath = resolve(__dirname, file)
|
||||
console.log(`Custom Route: %s`, req.url)
|
||||
|
||||
return res.sendFile(filePath)
|
||||
})
|
||||
}
|
||||
|
||||
app.use(vite.middlewares)
|
||||
|
||||
app.listen(3000)
|
||||
}
|
||||
|
||||
createServer({
|
||||
'/': './index.html',
|
||||
})
|
@ -0,0 +1,9 @@
|
||||
import { render } from 'preact'
|
||||
|
||||
const App = () => (
|
||||
<>
|
||||
<h1>Homepage</h1>
|
||||
</>
|
||||
)
|
||||
|
||||
render(<App />, document.querySelector('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,18 @@
|
||||
import glob from 'fast-glob'
|
||||
import { defineConfig } from 'vite'
|
||||
|
||||
import preact from '@preact/preset-vite'
|
||||
|
||||
export default defineConfig({
|
||||
build: {
|
||||
rollupOptions: {
|
||||
input: glob.sync('*.html'),
|
||||
},
|
||||
},
|
||||
server: {
|
||||
proxy: {
|
||||
'/api': 'http://127.0.0.1:4000/',
|
||||
},
|
||||
},
|
||||
plugins: [preact()],
|
||||
})
|
@ -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,54 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"log"
|
||||
"os/exec"
|
||||
"server/database"
|
||||
"server/routes"
|
||||
"strings"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/gofiber/fiber/v2/middleware/logger"
|
||||
"github.com/gofiber/fiber/v2/middleware/recover"
|
||||
)
|
||||
|
||||
func main() {
|
||||
db := database.NewInMemoryDB()
|
||||
|
||||
router := &routes.Router{
|
||||
Database: db,
|
||||
}
|
||||
|
||||
app := fiber.New()
|
||||
|
||||
app.Use(logger.New())
|
||||
app.Use(recover.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"`)
|
||||
cmd := exec.Command("sh", "-c", "cd _frontend/ && npm run dev")
|
||||
cmdStdout, _ := cmd.StdoutPipe()
|
||||
|
||||
go func() {
|
||||
s := bufio.NewScanner(cmdStdout)
|
||||
for s.Scan() {
|
||||
log.Printf("[ViteJS] %s", s.Text())
|
||||
}
|
||||
if err := s.Err(); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}()
|
||||
|
||||
err := cmd.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…
Reference in New Issue