-
Notifications
You must be signed in to change notification settings - Fork 370
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: move templates to embeded files
- Loading branch information
Showing
22 changed files
with
462 additions
and
465 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,24 @@ | ||
package template | ||
|
||
import ( | ||
_ "embed" | ||
) | ||
|
||
//go:embed files/routes/chi.go.tmpl | ||
var chiRoutesTemplate []byte | ||
|
||
// ChiTemplates contains the methods used for building | ||
// an app that uses [github.com/go-chi/chi] | ||
type ChiTemplates struct{} | ||
|
||
func (c ChiTemplates) Main() []byte { | ||
return MainTemplate() | ||
return mainTemplate | ||
} | ||
|
||
func (c ChiTemplates) Server() []byte { | ||
return MakeHTTPServer() | ||
return standardServerTemplate | ||
} | ||
|
||
func (c ChiTemplates) Routes() []byte { | ||
return MakeChiRoutes() | ||
} | ||
|
||
// MakeChiRoutes returns a byte slice that represents | ||
// the internal/server/routes.go file when using Chi. | ||
func MakeChiRoutes() []byte { | ||
return []byte(`package server | ||
import ( | ||
"encoding/json" | ||
"log" | ||
"net/http" | ||
"github.com/go-chi/chi/v5" | ||
"github.com/go-chi/chi/v5/middleware" | ||
) | ||
func (s *Server) RegisterRoutes() http.Handler { | ||
r := chi.NewRouter() | ||
r.Use(middleware.Logger) | ||
r.Get("/", s.helloWorldHandler) | ||
return r | ||
} | ||
func (s *Server) helloWorldHandler(w http.ResponseWriter, r *http.Request) { | ||
resp := make(map[string]string) | ||
resp["message"] = "Hello World" | ||
jsonResp, err := json.Marshal(resp) | ||
if err != nil { | ||
log.Fatalf("error handling JSON marshal. Err: %v", err) | ||
} | ||
w.Write(jsonResp) | ||
} | ||
`) | ||
return chiRoutesTemplate | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,23 @@ | ||
package template | ||
|
||
import ( | ||
_ "embed" | ||
) | ||
|
||
//go:embed files/routes/echo.go.tmpl | ||
var echoRoutesTemplate []byte | ||
|
||
// EchoTemplates contains the methods used for building | ||
// an app that uses [github.com/labstack/echo] | ||
type EchoTemplates struct{} | ||
|
||
func (e EchoTemplates) Main() []byte { | ||
return MainTemplate() | ||
return mainTemplate | ||
} | ||
func (e EchoTemplates) Server() []byte { | ||
return MakeHTTPServer() | ||
return standardServerTemplate | ||
} | ||
|
||
func (e EchoTemplates) Routes() []byte { | ||
return MakeEchoRoutes() | ||
} | ||
|
||
// MakeEchoRoutes returns a byte slice that represents | ||
// the internal/server/routes.go file when using Echo. | ||
func MakeEchoRoutes() []byte { | ||
return []byte(`package server | ||
import ( | ||
"net/http" | ||
"github.com/labstack/echo/v4" | ||
"github.com/labstack/echo/v4/middleware" | ||
) | ||
func (s *Server) RegisterRoutes() http.Handler { | ||
e := echo.New() | ||
e.Use(middleware.Logger()) | ||
e.Use(middleware.Recover()) | ||
e.GET("/", s.helloWorldHandler) | ||
return e | ||
} | ||
func (s *Server) helloWorldHandler(c echo.Context) error { | ||
resp := map[string]string{ | ||
"message": "Hello World", | ||
} | ||
return c.JSON(http.StatusOK, resp) | ||
} | ||
`) | ||
return echoRoutesTemplate | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,84 +1,29 @@ | ||
package template | ||
|
||
import ( | ||
_ "embed" | ||
) | ||
|
||
//go:embed files/routes/fiber.go.tmpl | ||
var fiberRoutesTemplate []byte | ||
|
||
//go:embed files/server/fiber.go.tmpl | ||
var fiberServerTemplate []byte | ||
|
||
//go:embed files/main/fiber_main.go.tmpl | ||
var fiberMainTemplate []byte | ||
|
||
// FiberTemplates contains the methods used for building | ||
// an app that uses [github.com/gofiber/fiber] | ||
type FiberTemplates struct{} | ||
|
||
func (f FiberTemplates) Main() []byte { | ||
return MakeFiberMain() | ||
return fiberMainTemplate | ||
} | ||
func (f FiberTemplates) Server() []byte { | ||
return MakeFiberServer() | ||
return fiberServerTemplate | ||
} | ||
|
||
func (f FiberTemplates) Routes() []byte { | ||
return MakeFiberRoutes() | ||
} | ||
|
||
// MakeFiberServer returns a byte slice that represents | ||
// the internal/server/server.go file when using Fiber. | ||
func MakeFiberServer() []byte { | ||
return []byte(`package server | ||
import "github.com/gofiber/fiber/v2" | ||
type FiberServer struct { | ||
*fiber.App | ||
} | ||
func New() *FiberServer { | ||
server := &FiberServer{ | ||
App: fiber.New(), | ||
} | ||
return server | ||
} | ||
`) | ||
} | ||
|
||
// MakeFiberRoutes returns a byte slice that represents | ||
// the internal/server/routes.go file when using Fiber. | ||
func MakeFiberRoutes() []byte { | ||
return []byte(`package server | ||
import ( | ||
"github.com/gofiber/fiber/v2" | ||
) | ||
func (s *FiberServer) RegisterFiberRoutes() { | ||
s.App.Get("/", s.helloWorldHandler) | ||
} | ||
func (s *FiberServer) helloWorldHandler(c *fiber.Ctx) error { | ||
resp := map[string]string{ | ||
"message": "Hello World", | ||
} | ||
return c.JSON(resp) | ||
} | ||
`) | ||
} | ||
|
||
// MakeHTTPRoutes returns a byte slice that represents | ||
// the cmd/api/main.go file when using Fiber. | ||
func MakeFiberMain() []byte { | ||
return []byte(`package main | ||
import ( | ||
"{{.ProjectName}}/internal/server" | ||
) | ||
func main() { | ||
server := server.New() | ||
server.RegisterFiberRoutes() | ||
err := server.Listen(":8080") | ||
if err != nil { | ||
panic("cannot start server") | ||
} | ||
} | ||
`) | ||
return fiberRoutesTemplate | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# Project {{.ProjectName}} | ||
|
||
One Paragraph of project description goes here | ||
|
||
## Getting Started | ||
|
||
These instructions will get you a copy of the project up and running on your local machine for development and testing purposes. See deployment for notes on how to deploy the project on a live system. | ||
|
||
## MakeFile | ||
|
||
run all make commands with clean tests | ||
```bash | ||
make all build | ||
``` | ||
|
||
build the application | ||
```bash | ||
make build | ||
``` | ||
|
||
run the application | ||
```bash | ||
make run | ||
``` | ||
|
||
run the test suite | ||
```bash | ||
make test | ||
``` | ||
|
||
clean up binary from the last build | ||
```bash | ||
make clean | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
root = "." | ||
testdata_dir = "testdata" | ||
tmp_dir = "tmp" | ||
|
||
[build] | ||
args_bin = [] | ||
bin = "./tmp/main" | ||
cmd = "make run" | ||
delay = 1000 | ||
exclude_dir = ["assets", "tmp", "vendor", "testdata"] | ||
exclude_file = [] | ||
exclude_regex = ["_test.go"] | ||
exclude_unchanged = false | ||
follow_symlink = false | ||
full_bin = "" | ||
include_dir = [] | ||
include_ext = ["go", "tpl", "tmpl", "html"] | ||
include_file = [] | ||
kill_delay = "0s" | ||
log = "build-errors.log" | ||
poll = false | ||
poll_interval = 0 | ||
post_cmd = [] | ||
pre_cmd = [] | ||
rerun = false | ||
rerun_delay = 500 | ||
send_interrupt = false | ||
stop_on_error = false | ||
|
||
[color] | ||
app = "" | ||
build = "yellow" | ||
main = "magenta" | ||
runner = "green" | ||
watcher = "cyan" | ||
|
||
[log] | ||
main_only = false | ||
time = false | ||
|
||
[misc] | ||
clean_on_exit = false | ||
|
||
[screen] | ||
clear_on_rebuild = false | ||
keep_scroll = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package main | ||
|
||
import ( | ||
"{{.ProjectName}}/internal/server" | ||
) | ||
|
||
func main() { | ||
|
||
server := server.New() | ||
|
||
server.RegisterFiberRoutes() | ||
|
||
err := server.Listen(":8080") | ||
if err != nil { | ||
panic("cannot start server") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package main | ||
|
||
import ( | ||
"{{.ProjectName}}/internal/server" | ||
) | ||
|
||
func main() { | ||
|
||
server := server.NewServer() | ||
|
||
err := server.ListenAndServe() | ||
if err != nil { | ||
panic("cannot start server") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# Simple Makefile for a Go project | ||
|
||
# Build the application | ||
all: build | ||
|
||
build: | ||
@echo "Building..." | ||
@go build -o main cmd/api/main.go | ||
|
||
# Run the application | ||
run: | ||
@go run cmd/api/main.go | ||
|
||
# Test the application | ||
test: | ||
@echo "Testing..." | ||
@go test ./... | ||
|
||
# Clean the binary | ||
clean: | ||
@echo "Cleaning..." | ||
@rm -f main | ||
|
||
# Live Reload | ||
watch: | ||
@echo "Watching..." | ||
@air | ||
|
||
.PHONY: all build run test clean |
Oops, something went wrong.