serve frontend + backend as a one binary
This commit is contained in:
79
bin/migrate-up/main.go
Normal file
79
bin/migrate-up/main.go
Normal file
@@ -0,0 +1,79 @@
|
||||
// Copyright 2024 Patial Tech. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"io/fs"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"github.com/golang-migrate/migrate/v4"
|
||||
"github.com/golang-migrate/migrate/v4/database/postgres"
|
||||
"github.com/golang-migrate/migrate/v4/source/httpfs"
|
||||
"gitserver.in/patialtech/rano/db"
|
||||
entMigrate "gitserver.in/patialtech/rano/db/ent/migrate"
|
||||
"gitserver.in/patialtech/rano/db/migrations"
|
||||
"gitserver.in/patialtech/rano/util/logger"
|
||||
)
|
||||
|
||||
func main() {
|
||||
client := db.Client()
|
||||
defer client.Close()
|
||||
|
||||
err := client.Schema.Create(
|
||||
context.Background(),
|
||||
entMigrate.WithDropIndex(true),
|
||||
entMigrate.WithDropColumn(true),
|
||||
entMigrate.WithForeignKeys(true),
|
||||
)
|
||||
if err != nil {
|
||||
logger.Fatal("😡 migrate-up failed with error: %v", err)
|
||||
} else {
|
||||
logger.Info("✅ migrate-up")
|
||||
}
|
||||
|
||||
if err = sqlUp(); err != nil {
|
||||
logger.Fatal("😡 .sql migrate-up failed with error: %v", err)
|
||||
} else {
|
||||
logger.Info("✅ migrate-up .sql migrations")
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func sqlUp() error {
|
||||
m, err := mig()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err = m.Up(); err != nil && !errors.Is(err, migrate.ErrNoChange) {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func mig() (*migrate.Migrate, error) {
|
||||
// migrations files
|
||||
migrations, err := fs.Sub(migrations.FS, ".")
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to read embed files: %v", err)
|
||||
}
|
||||
|
||||
// create a source driver from the migrations file system
|
||||
src, err := httpfs.New(http.FS(migrations), ".")
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to create source driver: %v", err)
|
||||
}
|
||||
|
||||
driver, err := postgres.WithInstance(db.New(), &postgres.Config{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return migrate.NewWithInstance("httpfs", src, "postgres", driver)
|
||||
}
|
67
bin/server/handler/request.go
Normal file
67
bin/server/handler/request.go
Normal file
@@ -0,0 +1,67 @@
|
||||
// Copyright 2024 Patial Tech. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"gitserver.in/patialtech/rano/util/uid"
|
||||
)
|
||||
|
||||
const RequestIDKey = "RequestID"
|
||||
const RequestIPKey = "RequestIP"
|
||||
const RequestUserAgentKey = "RequestUA"
|
||||
|
||||
var defaultHeaders = []string{
|
||||
"True-Client-IP", // Cloudflare Enterprise plan
|
||||
"X-Real-IP",
|
||||
"X-Forwarded-For",
|
||||
}
|
||||
|
||||
// Request middleware that will do the following:
|
||||
// - pull session user
|
||||
// - set requestID
|
||||
// - set ctx RealIP and client userAgent info
|
||||
func Request() func(http.Handler) http.Handler {
|
||||
return func(h http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
|
||||
// ID
|
||||
requestID := r.Header.Get("X-Request-Id")
|
||||
if requestID == "" {
|
||||
requestID = uid.NewUUID()
|
||||
}
|
||||
ctx = context.WithValue(ctx, RequestIDKey, requestID)
|
||||
|
||||
// IP
|
||||
if ip := getRealIP(r, defaultHeaders); ip != "" {
|
||||
r.RemoteAddr = ip
|
||||
}
|
||||
ctx = context.WithValue(ctx, RequestIPKey, requestID)
|
||||
|
||||
// User Agent
|
||||
ctx = context.WithValue(ctx, RequestUserAgentKey, r.UserAgent())
|
||||
|
||||
h.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func getRealIP(r *http.Request, headers []string) string {
|
||||
for _, header := range headers {
|
||||
if ip := r.Header.Get(header); ip != "" {
|
||||
ips := strings.Split(ip, ",")
|
||||
if ips[0] == "" || net.ParseIP(ips[0]) == nil {
|
||||
continue
|
||||
}
|
||||
return ips[0]
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
84
bin/server/main.go
Normal file
84
bin/server/main.go
Normal file
@@ -0,0 +1,84 @@
|
||||
// Copyright 2024 Patial Tech. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"gitserver.in/patialtech/mux"
|
||||
"gitserver.in/patialtech/mux/middleware"
|
||||
"gitserver.in/patialtech/rano/bin/server/handler"
|
||||
"gitserver.in/patialtech/rano/config"
|
||||
"gitserver.in/patialtech/rano/graph"
|
||||
"gitserver.in/patialtech/rano/util/logger"
|
||||
"gitserver.in/patialtech/rano/web"
|
||||
)
|
||||
|
||||
func main() {
|
||||
r := mux.NewRouter()
|
||||
|
||||
r.Use(handler.Request())
|
||||
|
||||
// CORS
|
||||
r.Use(middleware.CORS(middleware.CORSOption{
|
||||
AllowedHeaders: []string{"Content-Type"},
|
||||
AllowedOrigins: []string{config.Read().WebURL},
|
||||
MaxAge: 60,
|
||||
}))
|
||||
// Secure Headers
|
||||
r.Use(middleware.Helmet(middleware.HelmetOption{
|
||||
ContentSecurityPolicy: middleware.CSP{
|
||||
ScriptSrc: []string{"self", "https://cdn.jsdelivr.net", "unsafe-inline"},
|
||||
},
|
||||
}))
|
||||
|
||||
// graphiql
|
||||
r.GET("/graphiql", graph.GraphiQL("/query"))
|
||||
// graph query
|
||||
r.POST("/query", graph.Query)
|
||||
|
||||
// catch all
|
||||
r.GET("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
var (
|
||||
resourcePath string
|
||||
path = r.URL.Path
|
||||
)
|
||||
|
||||
if strings.HasPrefix(path, "/_app") {
|
||||
resourcePath = "public/build" + path
|
||||
} else if strings.HasSuffix(path, ".png") ||
|
||||
strings.HasSuffix(path, ".ico") ||
|
||||
strings.HasSuffix(path, ".svg") ||
|
||||
strings.HasSuffix(path, "robot.txt") ||
|
||||
strings.HasSuffix(path, "site.webmanifest") {
|
||||
resourcePath = "public" + path
|
||||
} else {
|
||||
resourcePath = "public/build/fallback.html"
|
||||
}
|
||||
|
||||
if b, err := web.Public.ReadFile(resourcePath); err != nil {
|
||||
_, _ = w.Write([]byte("hello there"))
|
||||
} else {
|
||||
if strings.HasSuffix(path, ".js") {
|
||||
w.Header().Set("Content-Type", "text/javascript")
|
||||
} else if strings.HasSuffix(path, ".css") {
|
||||
w.Header().Set("Content-Type", "text/css")
|
||||
} else {
|
||||
w.Header().Set("Content-Type", http.DetectContentType(b))
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
_, _ = w.Write(b)
|
||||
}
|
||||
})
|
||||
|
||||
r.Serve(func(srv *http.Server) error {
|
||||
srv.Addr = fmt.Sprintf(":%d", config.Read().GraphPort)
|
||||
logger.Info("graph server listening on %s", srv.Addr)
|
||||
return srv.ListenAndServe()
|
||||
})
|
||||
}
|
Reference in New Issue
Block a user