Set up a go skeleton that works with the underlying FE js

This commit is contained in:
Barak Michener 2020-11-11 21:23:29 -08:00
parent f0321b3f49
commit c0e29a5b89
5 changed files with 116 additions and 6 deletions

2
go.mod
View file

@ -1,3 +1,3 @@
module github.com/barakmich/astaire
module git.barakmich.com/barakmich/changeme
go 1.15

0
go.sum Normal file
View file

109
main.go Normal file
View file

@ -0,0 +1,109 @@
package main
import (
"context"
"flag"
"log"
"net/http"
"os"
"os/exec"
"os/signal"
"path/filepath"
"sync"
"syscall"
"time"
)
var (
devMode = flag.Bool("dev", false, "Run yarn autoreload underneath Go")
)
const (
publicWebPath = "./web/public/"
)
func main() {
flag.Parse()
var wg sync.WaitGroup
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
ctx, cancel := context.WithCancel(context.Background())
go func() {
oscall := <-c
log.Printf("system call:%+v", oscall)
cancel()
}()
wg.Add(1)
go serveHTTP(ctx, &wg)
if *devMode {
wg.Add(1)
go runYarn(ctx, &wg)
}
wg.Wait()
}
func serveHTTP(ctx context.Context, wg *sync.WaitGroup) {
fs := http.FileServer(http.Dir(publicWebPath))
mux := http.NewServeMux()
mux.Handle("/", fs)
srv := &http.Server{
Addr: ":5000",
Handler: mux,
}
go func() {
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("listen:%+s\n", err)
}
}()
log.Printf("Listening on port :5000")
<-ctx.Done()
log.Printf("server stopped")
ctxShutDown, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := srv.Shutdown(ctxShutDown); err != nil {
if err != http.ErrServerClosed {
log.Fatalf("server Shutdown Failed:%+s", err)
}
}
log.Printf("server exited properly")
wg.Done()
return
}
func runYarn(ctx context.Context, wg *sync.WaitGroup) {
var err error
cmd := exec.Command("yarn", "dev")
cmd.Env = append(cmd.Env, "SKIP_SERVE=1")
cmd.Dir, err = filepath.Abs("./web/")
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err != nil {
panic(err)
}
err = cmd.Start()
if err != nil {
panic(err)
}
<-ctx.Done()
log.Println("closing")
err = syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL)
if err != nil {
panic(err)
}
wg.Done()
return
}

View file

@ -7,6 +7,7 @@ import sveltePreprocess from "svelte-preprocess"
import typescript from "@rollup/plugin-typescript"
const production = !process.env.ROLLUP_WATCH
const do_serve = !process.env.SKIP_SERVE
function serve() {
let server
@ -71,7 +72,7 @@ export default {
// In dev mode, call `npm run start` once
// the bundle has been generated
!production && serve(),
!production && do_serve && serve(),
// Watch the `public` directory and refresh the
// browser on changes when not in production

View file

@ -1,22 +1,22 @@
<script lang="ts">
import Tailwind from "./Tailwind.svelte"
const name: string = "World";
const name: string = "Something";
</script>
<style lang="scss">
main {
h1 {
font-size: 20px;
font-size: 25px;
}
}
</style>
<Tailwind />
<main>
<h1 class="text-blue-300">Hello {name}!</h1>
<h1 class="text-blue-200">Hello {name}!</h1>
<p>
Visit the
<a href="https://svelte.dev/tutorial">Svelte tutorial</a>
<a class="text-blue-700" href="https://svelte.dev/tutorial">Svelte tutorial</a>
to learn how to build Svelte apps.
</p>
</main>