Skip to content

Commit

Permalink
Switch chromad to go:embed and show version.
Browse files Browse the repository at this point in the history
  • Loading branch information
alecthomas committed Apr 24, 2021
1 parent 7410d4a commit c44c690
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 20 deletions.
4 changes: 2 additions & 2 deletions .circleci/config.yml
Expand Up @@ -15,7 +15,7 @@ jobs:
environment:
GO111MODULE: "on"
docker:
- image: circleci/golang:1.15
- image: circleci/golang:1.16
working_directory: /go/src/github.com/alecthomas/chroma
steps:
- checkout
Expand All @@ -36,7 +36,7 @@ jobs:
./bin/golangci-lint run
release:
docker:
- image: circleci/golang:1.15
- image: circleci/golang:1.16
steps:
- checkout
- run: curl -sL https://git.io/goreleaser | bash
6 changes: 3 additions & 3 deletions Makefile
@@ -1,5 +1,7 @@
.PHONY: chromad upload all

VERSION ?= $(shell git describe --tags --dirty --always)

all: README.md tokentype_string.go

README.md: lexers/*/*.go
Expand All @@ -9,10 +11,8 @@ tokentype_string.go: types.go
go generate

chromad:
(cd ./cmd/chromad && go get github.com/GeertJohan/go.rice/rice@master && go install github.com/GeertJohan/go.rice/rice)
rm -f chromad
(export CGOENABLED=0 GOOS=linux ; cd ./cmd/chromad && go build -o ../../chromad .)
rice append -i ./cmd/chromad --exec=./chromad
(export CGOENABLED=0 GOOS=linux ; cd ./cmd/chromad && go build -ldflags="-X 'main.version=$(VERSION)'" -o ../../chromad .)

upload: chromad
scp chromad root@swapoff.org: && \
Expand Down
2 changes: 1 addition & 1 deletion cmd/chromad/go.mod
@@ -1,6 +1,6 @@
module github.com/alecthomas/chroma/cmd/chromad

go 1.13
go 1.16

require (
github.com/GeertJohan/go.rice v1.0.2
Expand Down
6 changes: 6 additions & 0 deletions cmd/chromad/go.sum
@@ -1,6 +1,8 @@
github.com/GeertJohan/go.incremental v1.0.0 h1:7AH+pY1XUgQE4Y1HcXYaMqAI0m9yrFqo/jt0CW30vsg=
github.com/GeertJohan/go.incremental v1.0.0/go.mod h1:6fAjUhbVuX1KcMD3c8TEgVUqmo4seqhv0i0kdATSkM0=
github.com/GeertJohan/go.rice v1.0.2 h1:PtRw+Tg3oa3HYwiDBZyvOJ8LdIyf6lAovJJtr7YOAYk=
github.com/GeertJohan/go.rice v1.0.2/go.mod h1:af5vUNlDNkCjOZeSGFgIJxDje9qdjsO6hshx0gTmZt4=
github.com/akavel/rsrc v0.8.0 h1:zjWn7ukO9Kc5Q62DOJCcxGpXC18RawVtYAGdz2aLlfw=
github.com/akavel/rsrc v0.8.0/go.mod h1:uLoCtb9J+EyAqh+26kdrTgmzRBFPGOolLWKpdxkKq+c=
github.com/alecthomas/assert v0.0.0-20170929043011-405dbfeb8e38 h1:smF2tmSOzy2Mm+0dGI2AIUHY+w0BUc+4tn40djz7+6U=
github.com/alecthomas/assert v0.0.0-20170929043011-405dbfeb8e38/go.mod h1:r7bzyVFMNntcxPZXK3/+KdruV1H5KSlyVY0gc+NgInI=
Expand Down Expand Up @@ -32,11 +34,13 @@ github.com/gorilla/securecookie v1.1.1 h1:miw7JPhV+b/lAHSXz4qd/nN9jRiAFV5FwjeKyC
github.com/gorilla/securecookie v1.1.1/go.mod h1:ra0sb63/xPlUeL+yeDciTfxMRAA+MP+HVt/4epWDjd4=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/jessevdk/go-flags v1.4.0 h1:4IU2WS7AumrZ/40jfhf4QVDMsQwqA7VEHozFRrGARJA=
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
github.com/mattn/go-colorable v0.1.6/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/nkovacs/streamquote v1.0.0 h1:PmVIV08Zlx2lZK5fFZlMZ04eHcDTIFJCv/5/0twVUow=
github.com/nkovacs/streamquote v1.0.0/go.mod h1:BN+NaZ2CmdKqUuTUXUEm9j95B2TRbpOWpxbJYzzgUsc=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
Expand All @@ -50,7 +54,9 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasttemplate v1.0.1 h1:tY9CJiPnMXf1ERmG2EyK7gNUd+c6RKGD0IfU8WdUSz8=
github.com/valyala/fasttemplate v1.0.1/go.mod h1:UQGH1tvbgY+Nz5t2n7tXsz52dQxojPUpymEIMZ47gx8=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand Down
28 changes: 16 additions & 12 deletions cmd/chromad/main.go
@@ -1,15 +1,14 @@
package main

import (
"embed"
"encoding/json"
"fmt"
"html/template"
"log"
"net/http"
"sort"
"strings"

rice "github.com/GeertJohan/go.rice"
"github.com/alecthomas/kong"
konghcl "github.com/alecthomas/kong-hcl"
"github.com/gorilla/csrf"
Expand All @@ -23,10 +22,14 @@ import (
)

var (
templateFiles = rice.MustFindBox("templates")
staticFiles = rice.MustFindBox("static")
version = "devel"

htmlTemplate = template.Must(template.New("html").Parse(templateFiles.MustString("index.html.tmpl")))
//go:embed templates/index.html.tmpl
indexTemplate string
//go:embed static
staticFiles embed.FS

htmlTemplate = template.Must(template.New("html").Parse(indexTemplate))
)

type context struct {
Expand Down Expand Up @@ -124,7 +127,7 @@ func newContext(r *http.Request) context {
ctx := context{
SelectedStyle: "monokailight",
CSRFField: csrf.TemplateField(r),
Version: fmt.Sprintf("%d", staticFiles.Time().Unix()),
Version: version,
}
style := styles.Get(ctx.SelectedStyle)
if style == nil {
Expand All @@ -147,18 +150,19 @@ func newContext(r *http.Request) context {

func main() {
var cli struct {
Config kong.ConfigFlag `help:"Load configuration." placeholder:"FILE"`
Bind string `help:"HTTP bind address." default:"127.0.0.1:8080"`
CSRFKey string `help:"CSRF key." default:""`
Version kong.VersionFlag `help:"Show version."`
Config kong.ConfigFlag `help:"Load configuration." placeholder:"FILE"`
Bind string `help:"HTTP bind address." default:"127.0.0.1:8080"`
CSRFKey string `help:"CSRF key." default:""`
}
ctx := kong.Parse(&cli, kong.Configuration(konghcl.Loader))
ctx := kong.Parse(&cli, kong.Configuration(konghcl.Loader), kong.Vars{"version": version})

log.Printf("Starting on http://%s\n", cli.Bind)
log.Printf("Starting chromad %s on http://%s\n", version, cli.Bind)

router := mux.NewRouter()
router.Handle("/", http.HandlerFunc(index)).Methods("GET")
router.Handle("/api/render", http.HandlerFunc(renderHandler)).Methods("POST")
router.Handle("/static/{file:.*}", http.StripPrefix("/static/", http.FileServer(staticFiles.HTTPBox()))).Methods("GET")
router.Handle("/static/{file:.*}", http.FileServer(http.FS(staticFiles))).Methods("GET")

options := []csrf.Option{}
if cli.CSRFKey == "" {
Expand Down
4 changes: 2 additions & 2 deletions cmd/chromad/templates/index.html.tmpl
@@ -1,7 +1,7 @@
<!doctype html>
<html>
<head>
<title>Chroma Playground</title>
<title>Chroma Playground ({{.Version}})</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.7.5/css/bulma.min.css"/>
<style>
textarea {
Expand All @@ -21,7 +21,7 @@
<body>
<div class="container">

<h1 class="title">Chroma Playground</h1>
<h1 class="title">Chroma Playground ({{.Version}})</h1>

<div class="notification">
<a href="https://github.com/alecthomas/chroma">Chroma</a> is a general purpose syntax highlighter in pure Go.
Expand Down

0 comments on commit c44c690

Please sign in to comment.