Skip to content

Commit

Permalink
fix(repl): server not response markdown
Browse files Browse the repository at this point in the history
  • Loading branch information
axetroy committed Nov 26, 2020
1 parent 4f27a9f commit 45b6ad2
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 166 deletions.
10 changes: 4 additions & 6 deletions 4_generator/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,7 @@ import (
giturls "github.com/whilp/git-urls"
)

func Generate(g *client.GitClient, contexts []*transformer.TemplateContext, format option.Format, preset option.Preset, templateFile string) ([]byte, error) {
var (
templateStr string
)

func Generate(g *client.GitClient, contexts []*transformer.TemplateContext, format option.Format, preset option.Preset, templateFile string, templateStr string) ([]byte, error) {
remote, err := g.GetRemote()

if err != nil {
Expand Down Expand Up @@ -73,7 +69,9 @@ func Generate(g *client.GitClient, contexts []*transformer.TemplateContext, form
return nil, errors.WithStack(err)
}

if templateFile == "" {
if templateStr != "" {
// ignore
} else if templateFile == "" {
switch preset {
case option.PresetDefault:
templateStr = DEFAULT_TEMPLATE
Expand Down
14 changes: 3 additions & 11 deletions 5_formatter/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,14 @@ import (
"github.com/shurcooL/markdownfmt/markdown"
)

func Format(src []byte, format option.Format, templateFile string) ([]byte, error) {
func Format(src []byte, format option.Format) ([]byte, error) {

switch format {
case option.FormatJSON:
return src, nil
case option.FormatMarkdown:
var (
filename string
isTerminal = true
)
if templateFile == "" {
filename = "CHANGELOG.md"
isTerminal = false
}
return markdown.Process(filename, src, &markdown.Options{
Terminal: isTerminal,
return markdown.Process("CHANGELOG.md", src, &markdown.Options{
Terminal: false,
})
default:
return nil, errors.Errorf("invalid format '%s'", format)
Expand Down
142 changes: 0 additions & 142 deletions cmd/whatchanged-remote/main.go

This file was deleted.

3 changes: 0 additions & 3 deletions cmd/whatchanged-remote/run.sh

This file was deleted.

76 changes: 76 additions & 0 deletions cmd/whatchanged-server/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package main

import (
"bytes"
"fmt"
"log"
"net/http"
"os"

"github.com/axetroy/whatchanged"
"github.com/axetroy/whatchanged/option"
"github.com/pkg/errors"
)

func handler(w http.ResponseWriter, r *http.Request) {
var (
err error
output = bytes.NewBuffer([]byte{})
)
// cors
w.Header().Set("Access-Control-Allow-Origin", r.Header.Get("Origin"))
w.Header().Set("Access-Control-Allow-Methods", "GET")

if r.Method == http.MethodOptions {
w.WriteHeader(200)
_, _ = w.Write([]byte{})
return
}

defer func() {
if r, ok := recover().(error); ok {
err = r
}

if err != nil {
b := []byte(fmt.Sprintf("%+v\n", err))
w.Header().Set("Content-Type", "text/plain; charset=UTF-8")
w.WriteHeader(http.StatusInternalServerError)
_, _ = w.Write(b)
} else {
w.Header().Set("Content-Type", "text/markdown; charset=UTF-8")
w.WriteHeader(http.StatusOK)
_, _ = w.Write(output.Bytes())
}
}()

query := r.URL.Query()

username := query.Get("username")
repo := query.Get("repo")
version := query.Get("version")
template := query.Get("template")
preset := query.Get("preset")

url := fmt.Sprintf("https://github.com/%s/%s", username, repo)

if err = whatchanged.Generate(url, output, &option.Options{
Version: version,
Template: template,
Preset: option.Preset(preset),
}); err != nil {
err = errors.WithStack(err)
}
}

func main() {
port := os.Getenv("PORT")

if port == "" {
port = "8080"
}

http.HandleFunc("/", handler)
print("Listen on port ", port, "\n")
log.Fatal(http.ListenAndServe(":"+port, nil))
}
3 changes: 3 additions & 0 deletions cmd/whatchanged-server/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#! /bin/sh
# run main.go
go run -mod=vendor ./cmd/whatchanged-server/main.go "$@"
5 changes: 3 additions & 2 deletions option/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ const (
type Options struct {
Version string
Format Format
Preset Preset
TemplateFile string // Priority is higher than preset
Preset Preset // Priority: 1 The higher the level, the more priority
TemplateFile string // Priority: 2
Template string // Priority: 3
Silent bool
}
4 changes: 2 additions & 2 deletions whatchanged.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,13 @@ func Generate(project string, w io.Writer, options *option.Options) error {
if err != nil {
return errors.WithStack(err)
}
output, err := generator.Generate(g, ctxs, options.Format, options.Preset, options.TemplateFile)
output, err := generator.Generate(g, ctxs, options.Format, options.Preset, options.TemplateFile, options.Template)

if err != nil {
return errors.WithStack(err)
}

formattedOutput, err := formatter.Format(output, options.Format, options.TemplateFile)
formattedOutput, err := formatter.Format(output, options.Format)

if err != nil {
return errors.WithStack(err)
Expand Down

0 comments on commit 45b6ad2

Please sign in to comment.