Add initial backend
This commit is contained in:
parent
a93864445a
commit
5cc000b25a
3
backend/go.mod
Normal file
3
backend/go.mod
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
module git.chandlerswift.com/chandlerswift/fishfryfinder
|
||||||
|
|
||||||
|
go 1.23.0
|
64
backend/main.go
Normal file
64
backend/main.go
Normal file
|
@ -0,0 +1,64 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"embed"
|
||||||
|
"encoding/json"
|
||||||
|
"flag"
|
||||||
|
"fmt"
|
||||||
|
"io/fs"
|
||||||
|
"net/http"
|
||||||
|
"runtime/debug"
|
||||||
|
)
|
||||||
|
|
||||||
|
//go:embed dist/*
|
||||||
|
var embeddedFiles embed.FS
|
||||||
|
|
||||||
|
var buildInfoJSON []byte
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
port := flag.Int("port", 8000, "Port to listen on")
|
||||||
|
flag.Parse()
|
||||||
|
|
||||||
|
frontend, err := fs.Sub(embeddedFiles, "dist")
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
http.Handle("GET /", http.FileServer(http.FS(frontend)))
|
||||||
|
|
||||||
|
initializeBuildInfoJSON()
|
||||||
|
http.HandleFunc("GET /api/v1/version", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
w.Write(buildInfoJSON)
|
||||||
|
})
|
||||||
|
|
||||||
|
fmt.Printf("Serving on :%v\n", *port)
|
||||||
|
panic(http.ListenAndServe(fmt.Sprintf(":%v", *port), nil))
|
||||||
|
}
|
||||||
|
|
||||||
|
func initializeBuildInfoJSON() {
|
||||||
|
rawBuildInfo, _ := debug.ReadBuildInfo()
|
||||||
|
var revision, time string
|
||||||
|
var dirty bool
|
||||||
|
for _, buildSetting := range rawBuildInfo.Settings {
|
||||||
|
if buildSetting.Key == "vcs.revision" {
|
||||||
|
revision = buildSetting.Value
|
||||||
|
} else if buildSetting.Key == "vcs.time" {
|
||||||
|
time = buildSetting.Value
|
||||||
|
} else if buildSetting.Key == "vcs.modified" {
|
||||||
|
dirty = buildSetting.Value == "true"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
buildInfo := map[string]interface{}{
|
||||||
|
"GitRev": revision,
|
||||||
|
"GitDirty": dirty,
|
||||||
|
"GitTime": time,
|
||||||
|
"GoVersion": rawBuildInfo.GoVersion,
|
||||||
|
"AllBuildInfo": rawBuildInfo.String(),
|
||||||
|
}
|
||||||
|
|
||||||
|
var err error
|
||||||
|
buildInfoJSON, err = json.Marshal(buildInfo)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue