Initial commit

This commit is contained in:
2025-03-07 11:12:53 +07:00
commit 5a7e718666
22 changed files with 4527 additions and 0 deletions

57
cmd/api/main.go Normal file
View File

@@ -0,0 +1,57 @@
package main
import (
"context"
"fmt"
"log"
"net/http"
"os/signal"
"syscall"
"time"
"blueprint_fix/internal/server"
)
func gracefulShutdown(apiServer *http.Server, done chan bool) {
// Create context that listens for the interrupt signal from the OS.
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer stop()
// Listen for the interrupt signal.
<-ctx.Done()
log.Println("shutting down gracefully, press Ctrl+C again to force")
// The context is used to inform the server it has 5 seconds to finish
// the request it is currently handling
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := apiServer.Shutdown(ctx); err != nil {
log.Printf("Server forced to shutdown with error: %v", err)
}
log.Println("Server exiting")
// Notify the main goroutine that the shutdown is complete
done <- true
}
func main() {
server := server.NewServer()
// Create a done channel to signal when the shutdown is complete
done := make(chan bool, 1)
// Run graceful shutdown in a separate goroutine
go gracefulShutdown(server, done)
err := server.ListenAndServe()
if err != nil && err != http.ErrServerClosed {
panic(fmt.Sprintf("http server error: %s", err))
}
// Wait for the graceful shutdown to complete
<-done
log.Println("Graceful shutdown complete.")
}

View File

@@ -0,0 +1,3 @@
@tailwind base;
@tailwind components;
@tailwind utilities;

View File

3476
cmd/web/assets/js/htmx.min.js vendored Normal file

File diff suppressed because it is too large Load Diff

18
cmd/web/base.templ Normal file
View File

@@ -0,0 +1,18 @@
package web
templ Base() {
<!DOCTYPE html>
<html lang="en" class="h-screen">
<head>
<meta charset="utf-8"/>
<title>Go Blueprint Hello</title>
<link href="assets/css/output.css" rel="stylesheet"/>
<script src="assets/js/htmx.min.js"></script>
</head>
<body class="bg-gray-100">
<main class="max-w-sm mx-auto p-4">
{ children... }
</main>
</body>
</html>
}

6
cmd/web/efs.go Normal file
View File

@@ -0,0 +1,6 @@
package web
import "embed"
//go:embed "assets"
var Files embed.FS

21
cmd/web/hello.go Normal file
View File

@@ -0,0 +1,21 @@
package web
import (
"log"
"net/http"
)
func HelloWebHandler(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
if err != nil {
http.Error(w, "Bad Request", http.StatusBadRequest)
}
name := r.FormValue("name")
component := HelloPost(name)
err = component.Render(r.Context(), w)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
log.Fatalf("Error rendering in HelloWebHandler: %e", err)
}
}

17
cmd/web/hello.templ Normal file
View File

@@ -0,0 +1,17 @@
package web
templ HelloForm() {
@Base() {
<form hx-post="/hello" method="POST" hx-target="#hello-container">
<input class="bg-gray-200 text-black p-2 border border-gray-400 rounded-lg"id="name" name="name" type="text"/>
<button type="submit" class="bg-orange-500 hover:bg-orange-700 text-white py-2 px-4 rounded">Submit</button>
</form>
<div id="hello-container"></div>
}
}
templ HelloPost(name string) {
<div class="bg-green-100 p-4 shadow-md rounded-lg mt-6">
<p>Hello, { name }</p>
</div>
}