diff --git a/README.md b/README.md new file mode 100644 index 0000000..8122eea --- /dev/null +++ b/README.md @@ -0,0 +1,32 @@ +# go-project-template + +This is a template for go projects. It provides a postgres database connection and a webserver. + +## Template Usage + +This template is intended for use with [gonew](https://go.dev/blog/gonew), but can also be manually copied and the package name updated. + +After you run gonew or update the package name manually, you will also want to: + +* search for the string `go-project-template` and replace it with the name of your project. +* Rename the sample config file in the root from `go-project-template.sample.json` +* You might want to put a human-readable project name in the `` element in `httpserver/templates/base.html` +* Rewrite this README + +## Project Usage + +This project requires a postgres server. For development, it is usually easiest to create a temporary database in a container: + +```bash +docker run --rm --name postgresql -e POSTGRES_PASSWORD=password -p 5432:5432 library/postgres +``` + +Then make a copy of the sample config file: + +```bash +cp go-project-template.sample.json go-project-template.json +``` + +The database schema and migrations are defined in `db/migrations/`. Migrations are applied by [goose](https://github.com/pressly/goose) when the program starts. +Queries are written in `database/queries/`, which gets read by [sqlc](https://sqlc.dev) to generate much of the go code in `db/`. If you update a query, run +`sqlc generate` to update the generated go files. diff --git a/config/version.go b/config/version.go index 61d8750..66f02b4 100644 --- a/config/version.go +++ b/config/version.go @@ -6,7 +6,10 @@ import ( "github.com/sirupsen/logrus" ) -var BuildInfo *debug.BuildInfo +var ( + BuildInfo *debug.BuildInfo + Version string +) func init() { var ok bool @@ -15,4 +18,11 @@ func init() { logrus.Error("failed to read build info") return } + + for _, setting := range BuildInfo.Settings { + if setting.Key == "vcs.revision" { + Version = setting.Value + break + } + } } diff --git a/go-project-template.sample.json b/go-project-template.sample.json new file mode 100644 index 0000000..a47587e --- /dev/null +++ b/go-project-template.sample.json @@ -0,0 +1,3 @@ +{ + "database": "postgresql://postgres:password@localhost:5432/postgres" +} diff --git a/httpserver/index.go b/httpserver/index.go new file mode 100644 index 0000000..0c929a4 --- /dev/null +++ b/httpserver/index.go @@ -0,0 +1,11 @@ +package httpserver + +import ( + "net/http" + + echo "github.com/labstack/echo/v4" +) + +func index(c echo.Context) error { + return c.Render(http.StatusOK, "sample-page.html", nil) +} diff --git a/httpserver/server.go b/httpserver/server.go index 4dc9893..72e3be9 100644 --- a/httpserver/server.go +++ b/httpserver/server.go @@ -24,6 +24,8 @@ func ListenAndServe() { server.Use(accessLogMiddleware) server.RouteNotFound("/*", notFoundHandler) + server.GET("/", index) + server.StaticFS("/static", Static) logrus.WithField("address", config.C.HTTPBind).Info("starting http server") diff --git a/httpserver/templates.go b/httpserver/templates.go index 8b189e2..3f54903 100644 --- a/httpserver/templates.go +++ b/httpserver/templates.go @@ -18,7 +18,7 @@ var ( allTemplates *template.Template funcs = template.FuncMap{ "version": func() string { - return fmt.Sprintf("%s %s", config.BuildInfo.Main.Path, config.BuildInfo.Main.Version) + return fmt.Sprintf("%s %s", config.BuildInfo.Main.Path, config.Version) }, } diff --git a/httpserver/templates/base.html b/httpserver/templates/base.html index ef412f5..250b0e1 100644 --- a/httpserver/templates/base.html +++ b/httpserver/templates/base.html @@ -2,7 +2,7 @@ <html> <head> - <title>{{ block "title" .}}Everyone{{ end }} + {{ block "title" .}}go-project-template{{ end }} diff --git a/httpserver/templates/importer-process.html b/httpserver/templates/importer-process.html deleted file mode 100644 index c7c78e2..0000000 --- a/httpserver/templates/importer-process.html +++ /dev/null @@ -1,26 +0,0 @@ -{{template "base.html" . }} - -{{ define "body" }} -{{ range $_, $file := .Files }} -
-

{{ $file.Filename }}

-

- -

-

- only relevant for format=separated-values -

-
- raw first few lines - {{ range $file.Lines }}{{ . }}
{{ end }}
-
-
-{{ end}} -{{ end }} diff --git a/httpserver/templates/importer-upload.html b/httpserver/templates/importer-upload.html deleted file mode 100644 index e592cde..0000000 --- a/httpserver/templates/importer-upload.html +++ /dev/null @@ -1,12 +0,0 @@ -{{template "base.html" . }} - -{{ define "body" }} -
-
- -
-
- -
-
-{{ end }} diff --git a/httpserver/templates/sample-page.html b/httpserver/templates/sample-page.html new file mode 100644 index 0000000..a39c643 --- /dev/null +++ b/httpserver/templates/sample-page.html @@ -0,0 +1,5 @@ +{{template "base.html" . }} + +{{ define "body" }} +This is a sample page. You should put something here! +{{ end }}