initial commit

next
Antonio De Lucreziis 2 years ago
commit 552c004151

10
.gitignore vendored

@ -0,0 +1,10 @@
.env
*.local*
bin/
.out/
out/
dist/
node_modules/
.vscode/

@ -0,0 +1,4 @@
.PHONY: test
test:
PROJECT_DIR="$(shell pwd)" go test -v ./...

@ -0,0 +1,24 @@
# Website 2
## Development
```bash shell
# First start in background the go backend on port :4000
$ go run -v ./cmd/server
# The start the frontend server on port :3000
$ pnpm run dev
```
## Production
```bash shell
# scaffold the whole server without actually starting the server, this will generate a JSON file containing all routes mount-points for the ViteJS build
$ go run -v ./cmd/build
# build all frontend pages and assets
$ pnpm run build
# Now we have all the files and could also ship everything in a single binary
$ go build -o ./out/bin/server ./cmd/server
```

@ -0,0 +1,51 @@
package main
import (
"encoding/json"
"log"
"os"
"phc/website/services/config"
"phc/website/services/database"
"phc/website/services/server"
"phc/website/services/server/dev"
lista_utenti "phc/website/services/server/lista-utenti"
"phc/website/sl"
)
func main() {
l := sl.New()
// sl.Inject[config.Interface](l, &config.EnvConfig{})
sl.InjectValue[config.Interface](l, &config.Custom{
ModeValue: "production",
HostValue: ":4000",
})
sl.InjectValue[database.Database](l, &database.Memory{})
sl.Inject(l, &dev.Dev{})
sl.Inject(l, &lista_utenti.ListaUtenti{})
sl.Inject(l, &server.Server{})
_, err := sl.Use[*server.Server](l)
if err != nil {
log.Fatal(err)
}
dev, err := sl.Use[*dev.Dev](l)
if err != nil {
log.Fatal(err)
}
f, err := os.Create("out/routes.json")
if err != nil {
log.Fatal(err)
}
enc := json.NewEncoder(f)
enc.SetIndent("", " ")
if err := enc.Encode(dev.HtmlRouteBindings); err != nil {
log.Fatal(err)
}
}

@ -0,0 +1,51 @@
package main
import (
"log"
"phc/website/model"
"phc/website/services/config"
"phc/website/services/database"
"phc/website/services/server"
"phc/website/services/server/dev"
lista_utenti "phc/website/services/server/lista-utenti"
"phc/website/sl"
)
func main() {
l := sl.New()
// sl.Inject[config.Interface](l, &config.EnvConfig{})
config := sl.InjectValue[config.Interface](l, &config.Custom{
ModeValue: "production",
HostValue: ":4000",
})
sl.InjectValue[database.Database](l, &database.Memory{
Users: []model.User{
{
Id: "claire",
FullName: "Claire Doe",
Nickname: "claire-doe",
AuthSources: map[string]model.AuthSource{},
},
{
Id: "john",
FullName: "John Smith",
Nickname: "john-smith",
AuthSources: map[string]model.AuthSource{},
},
},
})
sl.Inject(l, &dev.Dev{})
sl.Inject(l, &lista_utenti.ListaUtenti{})
sl.Inject(l, &server.Server{})
server, err := sl.Use[*server.Server](l)
if err != nil {
log.Fatal(err)
}
log.Fatal(server.Router.Listen(config.Host()))
}

@ -0,0 +1,12 @@
<!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>
</head>
<body>
Homepage
</body>
</html>

@ -0,0 +1,13 @@
<!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>ListaUtenti</title>
</head>
<body>
<main class="page-lista-utenti"></main>
<script type="module" src="./src/main.jsx"></script>
</body>
</html>

@ -0,0 +1,13 @@
import { render } from 'preact'
import './lista-utenti.scss'
render(
<>
<h1>Lista Utenti</h1>
<div class="list">
Lorem ipsum dolor sit, amet consectetur adipisicing elit. Odit, illo molestiae. Sapiente cumque saepe maxime, temporibus ad
nulla id officiis impedit ut dolorem asperiores voluptate illo, molestiae facilis inventore. Ea.
</div>
</>,
document.querySelector('main')
)

@ -0,0 +1,22 @@
module phc/website
go 1.19
require (
github.com/gofiber/fiber/v2 v2.41.0
github.com/joho/godotenv v1.4.0
github.com/valyala/fasthttp v1.43.0
)
require (
github.com/alecthomas/repr v0.2.0 // indirect
github.com/andybalholm/brotli v1.0.4 // indirect
github.com/klauspost/compress v1.15.9 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/mattn/go-runewidth v0.0.14 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/tcplisten v1.0.0 // indirect
golang.org/x/sys v0.1.0 // indirect
)

@ -0,0 +1,40 @@
github.com/alecthomas/repr v0.2.0 h1:HAzS41CIzNW5syS8Mf9UwXhNH1J9aix/BvDRf1Ml2Yk=
github.com/alecthomas/repr v0.2.0/go.mod h1:Fr0507jx4eOXV7AlPV6AVZLYrLIuIeSOWtW57eE/O/4=
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.41.0 h1:YhNoUS/OTjEz+/WLYuQ01xI7RXgKEFnGBKMagAu5f0M=
github.com/gofiber/fiber/v2 v2.41.0/go.mod h1:RdebcCuCRFp4W6hr3968/XxwJVg0K+jr9/Ae0PFzZ0Q=
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.9 h1:wKRjX6JRtDdrE9qwa4b/Cip7ACOshUI4smpCQanqjSY=
github.com/klauspost/compress v1.15.9/go.mod h1:PhcZ0MbTNciWF3rruxRgKxI5NkcHHrHUDtV4Yw2GlzU=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng=
github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU=
github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
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.43.0 h1:Gy4sb32C98fbzVWZlTM1oTMdLWGyvxR03VhM6cBIU4g=
github.com/valyala/fasthttp v1.43.0/go.mod h1:f6VbjjoI3z1NDOZOv17o6RvtRSWxC77seBFc2uWtgiY=
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-20220906165146-f3363e06e74c/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk=
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-20220728004956-3c1f35247d10/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.1.0 h1:kunALQeHf1/185U1i0GOB/fy1IPRDDpuoOOqRReG57U=
golang.org/x/sys v0.1.0/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,15 @@
package model
type User struct {
Id string
FullName string
Nickname string
AuthSources map[string]AuthSource
}
type AuthSource struct {
Provider string
AuthToken string
}

@ -0,0 +1,19 @@
{
"name": "website",
"version": "1.0.0",
"type": "module",
"scripts": {
"dev": "vite --clearScreen false",
"build": "vite build"
},
"devDependencies": {
"@preact/preset-vite": "^2.5.0",
"axios": "^1.2.6",
"node-fetch": "^3.3.0",
"sass": "^1.57.1",
"vite": "^4.0.4"
},
"dependencies": {
"preact": "^10.11.3"
}
}

File diff suppressed because it is too large Load Diff

@ -0,0 +1,52 @@
package config
import (
"phc/website/sl"
"github.com/joho/godotenv"
)
type Interface interface {
sl.Service
Mode() string
Host() string
}
type Custom struct {
ModeValue string
HostValue string
}
func (c Custom) Mode() string { return c.ModeValue }
func (c Custom) Host() string { return c.HostValue }
func (Custom) Initialize(l *sl.ServiceLocator) error {
return nil
}
type EnvConfig struct {
mode string
host string
}
func (c EnvConfig) Mode() string { return c.mode }
func (c EnvConfig) Host() string { return c.host }
func (c *EnvConfig) Initialize(l *sl.ServiceLocator) error {
m, err := godotenv.Read(".env")
if err != nil {
return err
}
c.mode = "production"
if v, ok := m["MODE"]; ok {
c.mode = v
}
c.host = ":4000"
if v, ok := m["HOST"]; ok {
c.host = v
}
return nil
}

@ -0,0 +1,64 @@
package database
import (
"fmt"
"phc/website/model"
"phc/website/sl"
)
type Database interface {
sl.Service
CreateUser(user model.User) error
ReadUser(id string) (model.User, error)
ReadUsers() ([]model.User, error)
UpdateUser(id string, user model.User) error
DeleteUser(id string) error
}
type Memory struct {
Users []model.User
}
func (m *Memory) Initialize(l *sl.ServiceLocator) error { return nil }
func (m *Memory) CreateUser(user model.User) error {
m.Users = append(m.Users, user)
return nil
}
func (m *Memory) ReadUser(id string) (model.User, error) {
for _, u := range m.Users {
if u.Id == id {
return u, nil
}
}
return model.User{}, fmt.Errorf(`no user with id "%s"`, id)
}
func (m *Memory) ReadUsers() ([]model.User, error) {
return m.Users, nil
}
func (m *Memory) UpdateUser(id string, user model.User) error {
for i, u := range m.Users {
if u.Id == id {
m.Users[i] = user
return nil
}
}
return fmt.Errorf(`no user with id "%s"`, id)
}
func (m *Memory) DeleteUser(id string) error {
for i, u := range m.Users {
if u.Id == id {
m.Users = append(m.Users[:i], m.Users[i+1:]...)
return nil
}
}
return fmt.Errorf(`no user with id "%s"`, id)
}

@ -0,0 +1,47 @@
package dev
import (
"log"
"path"
"phc/website/services/server/routes"
"phc/website/sl"
"github.com/gofiber/fiber/v2"
)
type Dev struct {
HtmlRouteBindings map[string]string
}
func (m *Dev) Initialize(l *sl.ServiceLocator) error {
m.HtmlRouteBindings = map[string]string{}
router, err := sl.Use[*routes.Router](l)
if err != nil {
return err
}
router.Get("/api/dev/routes", func(c *fiber.Ctx) error {
return c.JSON(m.HtmlRouteBindings)
})
return nil
}
// UseVitePage this hook will link the provided "mountPoint" to the "frontendHtml" page
func UseVitePage[T any](l *sl.ServiceLocator, mountPoint, frontendHtml string) func(c *fiber.Ctx) error {
log.Printf(`registering vite route %q for %q`, frontendHtml, mountPoint)
dev, err := sl.Use[*Dev](l)
if err != nil {
log.Fatal(err)
}
frontendPath := path.Join("./frontend/", frontendHtml)
dev.HtmlRouteBindings[mountPoint] = frontendPath
return func(c *fiber.Ctx) error {
return c.SendFile(frontendPath)
}
}

@ -0,0 +1,39 @@
package lista_utenti
import (
"phc/website/services/database"
"phc/website/services/server/dev"
"phc/website/services/server/routes"
"phc/website/sl"
"github.com/gofiber/fiber/v2"
)
type ListaUtenti struct{}
func (s *ListaUtenti) Initialize(l *sl.ServiceLocator) error {
db, err := sl.Use[database.Database](l)
if err != nil {
return err
}
router, err := sl.Use[*routes.Router](l)
if err != nil {
return err
}
router.Get("/utenti",
dev.UseVitePage[ListaUtenti](l, "/utenti", "pages/lista-utenti/index.html"),
)
router.Get("/api/lista-utenti", func(c *fiber.Ctx) error {
users, err := db.ReadUsers()
if err != nil {
return err
}
return c.JSON(users)
})
return nil
}

@ -0,0 +1,82 @@
package lista_utenti_test
import (
"context"
"fmt"
"io"
"net"
"net/http"
"phc/website/model"
"phc/website/services/database"
"phc/website/services/server/dev"
lista_utenti "phc/website/services/server/lista-utenti"
"phc/website/services/server/routes"
"phc/website/sl"
"testing"
"github.com/gofiber/fiber/v2"
"github.com/valyala/fasthttp"
"github.com/valyala/fasthttp/fasthttputil"
)
func Test1(t *testing.T) {
l := sl.New()
sl.InjectValue[database.Database](l, &database.Memory{
Users: []model.User{
{
Id: "claire",
FullName: "Claire Doe",
Nickname: "claire-doe",
AuthSources: map[string]model.AuthSource{},
},
{
Id: "john",
FullName: "John Smith",
Nickname: "john-smith",
AuthSources: map[string]model.AuthSource{},
},
},
})
sl.Inject(l, &dev.Dev{})
sl.Inject(l, &lista_utenti.ListaUtenti{})
r := fiber.New()
sl.InjectValue(l, routes.NewRouter(r))
req, err := http.NewRequest("GET", "http://localhost:4000/api/lista-utenti", nil)
if err != nil {
t.Error(err)
}
ln := fasthttputil.NewInmemoryListener()
defer ln.Close()
go func() {
err := fasthttp.Serve(ln, r.Handler())
if err != nil {
panic(fmt.Errorf("failed to serve: %v", err))
}
}()
client := http.Client{
Transport: &http.Transport{
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
return ln.Dial()
},
},
}
res, err := client.Do(req)
if err != nil {
t.Error(err)
}
body, err := io.ReadAll(res.Body)
if err != nil {
t.Error(err)
}
t.Log(string(body))
}

@ -0,0 +1,15 @@
package routes
import (
"phc/website/sl"
"github.com/gofiber/fiber/v2"
)
func NewRouter(r fiber.Router) *Router {
return &Router{r}
}
type Router struct{ fiber.Router }
func (s *Router) Initialize(l *sl.ServiceLocator) error { return nil }

@ -0,0 +1,26 @@
package server
import (
"phc/website/services/server/dev"
lista_utenti "phc/website/services/server/lista-utenti"
"phc/website/services/server/routes"
"phc/website/sl"
"github.com/gofiber/fiber/v2"
)
type Server struct{ Router *fiber.App }
func (s *Server) Initialize(l *sl.ServiceLocator) error {
s.Router = fiber.New(fiber.Config{})
sl.InjectValue(l, routes.NewRouter(s.Router))
if _, err := sl.Use[*dev.Dev](l); err != nil {
return err
}
if _, err := sl.Use[*lista_utenti.ListaUtenti](l); err != nil {
return err
}
return nil
}

@ -0,0 +1,73 @@
package sl
import (
"fmt"
"log"
)
type provider struct {
initialized bool
service Service
}
type ServiceLocator struct {
providers map[string]*provider
hooks map[string][]Service
}
type Service interface {
Initialize(l *ServiceLocator) error
}
func New() *ServiceLocator {
return &ServiceLocator{
providers: map[string]*provider{},
hooks: map[string][]Service{},
}
}
func getTypeName[T any]() string {
var v T
return fmt.Sprintf(`%T`, &v)[1:]
}
// Inject will set the implementation for "S" to "value" (the service will be initialized when needed after all of its dependencies)
func Inject[S Service](l *ServiceLocator, value S) {
key := getTypeName[S]()
log.Printf(`injecting value of type %T for interface %s`, value, key)
l.providers[key] = &provider{false, value}
}
// InjectValue will set the implementation for "S" to "value" and mark this service as already initialized (as this is just a constant)
func InjectValue[S Service](l *ServiceLocator, value S) S {
key := getTypeName[S]()
log.Printf(`injecting value of type %T for interface %s`, value, key)
l.providers[key] = &provider{true, value}
return value
}
// Use will retrive from the service locator the implementation set for the type "T" and initialize the service if yet to be initialized
func Use[T Service](l *ServiceLocator) (T, error) {
provider, ok := l.providers[getTypeName[T]()]
if !ok {
var zero T
return zero, fmt.Errorf(`no injected value for type "%T"`, zero)
}
if provider.initialized {
service := provider.service.(T)
return service, nil
}
log.Printf(`initializing %T`, provider.service)
if err := provider.service.Initialize(l); err != nil {
var zero T
return zero, err
}
provider.initialized = true
service := provider.service.(T)
return service, nil
}

@ -0,0 +1,69 @@
import { defineConfig } from 'vite'
import fetch from 'node-fetch'
import { readFile } from 'fs/promises'
import { dirname, resolve } from 'path'
import preactPlugin from '@preact/preset-vite'
const retriveGoRoutes = {
async build() {
console.log('Loading routes from disk...')
const routesRaw = await readFile('out/routes.json', 'utf8')
return JSON.parse(routesRaw)
},
async serve() {
console.log('Loading routes from go server...')
const routesReq = await fetch('http://127.0.0.1:4000/api/dev/routes')
const routes = await routesReq.json()
console.dir(routes, { depth: null })
return routes
},
}
export default defineConfig(async config => {
let routes = await retriveGoRoutes[config.command]()
console.dir(routes)
return {
root: 'frontend',
build: {
outDir: '../out/frontend',
rollupOptions: {
input: {
'main': resolve(__dirname, 'frontend/pages/index.html'),
'lista-utenti': resolve(__dirname, 'frontend/pages/lista-utenti/index.html'),
},
},
},
server: {
port: 3000,
proxy: {
'/api': 'http://localhost:4000/',
},
},
plugins: [
preactPlugin(),
{
name: 'custom-router',
configureServer(server) {
Object.entries(routes).forEach(([route, file]) => {
server.middlewares.use(route, async (req, res, next) => {
let htmlPage = await readFile(resolve(__dirname, file), 'utf8')
htmlPage = htmlPage.replace(/\.\//g, dirname(file) + '/')
const url = file
const html = await server.transformIndexHtml(url, htmlPage)
console.log(url)
res.writeHead(200, { 'Content-Type': 'text/html' }).end(html)
})
})
},
},
],
}
})
Loading…
Cancel
Save