*: add code to serialize templates into go code
Add a small program to iterate over the templates directory and generate a go map of name to file data. This is so we can compile these templates into the dex binary instead of requiring files on disk. Also add a Makefile rule to do this automatically.
This commit is contained in:
		
							
								
								
									
										5
									
								
								Makefile
									
									
									
									
									
								
							
							
						
						
									
										5
									
								
								Makefile
									
									
									
									
									
								
							| @@ -19,7 +19,7 @@ GOARCH=$(shell go env GOARCH) | |||||||
|  |  | ||||||
| build: bin/dex bin/example-app | build: bin/dex bin/example-app | ||||||
|  |  | ||||||
| bin/dex: FORCE | bin/dex: FORCE server/templates_default.go | ||||||
| 	@go install -ldflags $(LD_FLAGS) $(REPO_PATH)/cmd/dex | 	@go install -ldflags $(LD_FLAGS) $(REPO_PATH)/cmd/dex | ||||||
|  |  | ||||||
| bin/example-app: FORCE | bin/example-app: FORCE | ||||||
| @@ -42,6 +42,9 @@ lint: | |||||||
|       golint $$package; \ |       golint $$package; \ | ||||||
| 	done | 	done | ||||||
|  |  | ||||||
|  | server/templates_default.go: $(wildcard web/templates/**) | ||||||
|  | 	@go run server/templates_default_gen.go | ||||||
|  |  | ||||||
| .PHONY: docker-build | .PHONY: docker-build | ||||||
| docker-build: bin/dex | docker-build: bin/dex | ||||||
| 	@docker build -t $(DOCKER_IMAGE) . | 	@docker build -t $(DOCKER_IMAGE) . | ||||||
|   | |||||||
							
								
								
									
										79
									
								
								server/templates_default_gen.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										79
									
								
								server/templates_default_gen.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,79 @@ | |||||||
|  | // +build ignore | ||||||
|  |  | ||||||
|  | package main | ||||||
|  |  | ||||||
|  | import ( | ||||||
|  | 	"bytes" | ||||||
|  | 	"fmt" | ||||||
|  | 	"io/ioutil" | ||||||
|  | 	"log" | ||||||
|  | 	"os/exec" | ||||||
|  | 	"path/filepath" | ||||||
|  | ) | ||||||
|  |  | ||||||
|  | // ignoreFile uses "git check-ignore" to determine if we should ignore a file. | ||||||
|  | func ignoreFile(p string) (ok bool, err error) { | ||||||
|  | 	err = exec.Command("git", "check-ignore", p).Run() | ||||||
|  | 	if err == nil { | ||||||
|  | 		return true, nil | ||||||
|  | 	} | ||||||
|  | 	exitErr, ok := err.(*exec.ExitError) | ||||||
|  | 	if ok { | ||||||
|  | 		if sys := exitErr.Sys(); sys != nil { | ||||||
|  | 			e, ok := sys.(interface { | ||||||
|  | 				// Is the returned value something that returns an exit status? | ||||||
|  | 				ExitStatus() int | ||||||
|  | 			}) | ||||||
|  | 			if ok && e.ExitStatus() == 1 { | ||||||
|  | 				return false, nil | ||||||
|  | 			} | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | 	return false, err | ||||||
|  | } | ||||||
|  |  | ||||||
|  | type fileData struct { | ||||||
|  | 	name string | ||||||
|  | 	data string | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func main() { | ||||||
|  | 	dir, err := ioutil.ReadDir("web/templates") | ||||||
|  | 	if err != nil { | ||||||
|  | 		log.Fatal(err) | ||||||
|  | 	} | ||||||
|  | 	files := []fileData{} | ||||||
|  | 	for _, file := range dir { | ||||||
|  | 		p := filepath.Join("web/templates", file.Name()) | ||||||
|  | 		ignore, err := ignoreFile(p) | ||||||
|  | 		if err != nil { | ||||||
|  | 			log.Fatal(err) | ||||||
|  | 		} | ||||||
|  | 		if ignore { | ||||||
|  | 			continue | ||||||
|  | 		} | ||||||
|  |  | ||||||
|  | 		data, err := ioutil.ReadFile(p) | ||||||
|  | 		if err != nil { | ||||||
|  | 			log.Fatal(err) | ||||||
|  | 		} | ||||||
|  | 		files = append(files, fileData{file.Name(), string(data)}) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	f := new(bytes.Buffer) | ||||||
|  |  | ||||||
|  | 	fmt.Fprintln(f, "// This file was generated by the makefile. Do not edit.") | ||||||
|  | 	fmt.Fprintln(f) | ||||||
|  | 	fmt.Fprintln(f, "package server") | ||||||
|  | 	fmt.Fprintln(f) | ||||||
|  | 	fmt.Fprintln(f, "// defaultTemplates is a key for file name to file data of the files in web/templates.") | ||||||
|  | 	fmt.Fprintln(f, "var defaultTemplates = map[string]string{") | ||||||
|  | 	for _, file := range files { | ||||||
|  | 		fmt.Fprintf(f, "\t%q: %q,\n", file.name, file.data) | ||||||
|  | 	} | ||||||
|  | 	fmt.Fprintln(f, "}") | ||||||
|  |  | ||||||
|  | 	if err := ioutil.WriteFile("server/templates_default.go", f.Bytes(), 0644); err != nil { | ||||||
|  | 		log.Fatal(err) | ||||||
|  | 	} | ||||||
|  | } | ||||||
		Reference in New Issue
	
	Block a user