feat: vercel-like server

This commit is contained in:
Gustavo "Guz" L. de Mello
2024-05-21 18:38:05 -03:00
parent 3bceac455a
commit dcf2cc8544
2 changed files with 61 additions and 22 deletions

View File

@@ -1,22 +0,0 @@
package main
import (
"context"
"log"
"net/http"
"guz.one/api"
"guz.one/pages"
)
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/api/hello", api.Hello)
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
err := pages.Homepage().Render(context.Background(), w)
_ = err
})
log.Fatal(http.ListenAndServe(":5432", mux))
}

61
cmd/vercel/main.go Normal file
View File

@@ -0,0 +1,61 @@
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"log"
"net/http"
"os"
"guz.one/config"
"guz.one/internals"
)
type VercelConfig struct {
OutputDirectory string `json:"outputDirectory"`
}
func main() {
configPath := flag.String("c", "./vercel.json", "the path to the vercel.json file")
staticDir := flag.String("s", "./static", "the directory to copy static files from")
port := flag.Int("p", 8080, "the port to run the server")
configFile, err := os.ReadFile(*configPath)
if err != nil {
log.Fatalf("Unable to read vercel.json file due to:\n%s", err)
}
var c VercelConfig
err = json.Unmarshal(configFile, &c)
if err != nil {
log.Fatalf("Unable to parse vercel.json file due to:\n%s", err)
}
w := internals.StaticWriter{
DistDir: &c.OutputDirectory,
StaticDir: staticDir,
Pages: config.ROUTES,
Context: context.Background(),
Logger: *log.Default(),
}
log.Print("Writing static files")
err = w.WriteAll()
if err != nil {
log.Fatal(err)
}
log.Print("Starting server")
mux := http.NewServeMux()
config.APIROUTES(mux)
mux.Handle("/", http.FileServer(http.Dir(c.OutputDirectory)))
log.Printf("Running server at port: %v", *port)
err = http.ListenAndServe(fmt.Sprintf(":%v", *port), mux)
if err != nil {
log.Fatalf("Server crashed due to:\n%s", err)
}
}