Provide sample config file, an HTML template index page and a README

This commit is contained in:
Finn 2024-07-24 14:42:51 -07:00
parent 6f409bf0f1
commit ef43c3af29
10 changed files with 66 additions and 41 deletions

32
README.md Normal file
View file

@ -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 `<title>` 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.

View file

@ -6,7 +6,10 @@ import (
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )
var BuildInfo *debug.BuildInfo var (
BuildInfo *debug.BuildInfo
Version string
)
func init() { func init() {
var ok bool var ok bool
@ -15,4 +18,11 @@ func init() {
logrus.Error("failed to read build info") logrus.Error("failed to read build info")
return return
} }
for _, setting := range BuildInfo.Settings {
if setting.Key == "vcs.revision" {
Version = setting.Value
break
}
}
} }

View file

@ -0,0 +1,3 @@
{
"database": "postgresql://postgres:password@localhost:5432/postgres"
}

11
httpserver/index.go Normal file
View file

@ -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)
}

View file

@ -24,6 +24,8 @@ func ListenAndServe() {
server.Use(accessLogMiddleware) server.Use(accessLogMiddleware)
server.RouteNotFound("/*", notFoundHandler) server.RouteNotFound("/*", notFoundHandler)
server.GET("/", index)
server.StaticFS("/static", Static) server.StaticFS("/static", Static)
logrus.WithField("address", config.C.HTTPBind).Info("starting http server") logrus.WithField("address", config.C.HTTPBind).Info("starting http server")

View file

@ -18,7 +18,7 @@ var (
allTemplates *template.Template allTemplates *template.Template
funcs = template.FuncMap{ funcs = template.FuncMap{
"version": func() string { "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)
}, },
} }

View file

@ -2,7 +2,7 @@
<html> <html>
<head> <head>
<title>{{ block "title" .}}Everyone{{ end }}</title> <title>{{ block "title" .}}go-project-template{{ end }}</title>
<meta charset="utf-8" /> <meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no" /> <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no" />
<link rel="stylesheet" href="/static/main.css" /> <link rel="stylesheet" href="/static/main.css" />

View file

@ -1,26 +0,0 @@
{{template "base.html" . }}
{{ define "body" }}
{{ range $_, $file := .Files }}
<div class="file">
<p>{{ $file.Filename }}</p>
<p>
<label for="format">
Format: <select name="format" id="format">
<option value="">please select</option>
{{ range $_, $option := $.Formats }}
<option value="{{ $option }}"{{ if eq $file.Format $option }} selected{{ end }}>{{ $option }}</option>
{{ end }}
</select>
</label>
</p>
<p>
<label for="separator">Separator <input type="text" id="separator" value="{{ $file.Separator }}" size="2"></label> <small>only relevant for format=separated-values</small>
</p>
<details>
<summary>raw first few lines</summary>
<code>{{ range $file.Lines }}{{ . }}<br />{{ end }}</code>
</details>
</div>
{{ end}}
{{ end }}

View file

@ -1,12 +0,0 @@
{{template "base.html" . }}
{{ define "body" }}
<form method="post" enctype="multipart/form-data">
<div>
<input type="file" name="files" multiple />
</div>
<div>
<button>Submit</button>
</div>
</form>
{{ end }}

View file

@ -0,0 +1,5 @@
{{template "base.html" . }}
{{ define "body" }}
This is a sample page. You should put something here!
{{ end }}