implement gofmt and add a filewatcher in Goland

This commit is contained in:
Wouter Groeneveld 2021-04-09 21:00:54 +02:00
parent 3933e4d43b
commit d4c854ef81
28 changed files with 208 additions and 199 deletions

29
.idea/watcherTasks.xml Normal file
View File

@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectTasksOptions">
<TaskOptions isEnabled="true">
<option name="arguments" value="fmt $FilePath$" />
<option name="checkSyntaxErrors" value="true" />
<option name="description" />
<option name="exitCodeBehavior" value="ERROR" />
<option name="fileExtension" value="go" />
<option name="immediateSync" value="false" />
<option name="name" value="go fmt" />
<option name="output" value="$FilePath$" />
<option name="outputFilters">
<array />
</option>
<option name="outputFromStdout" value="false" />
<option name="program" value="$GoExecPath$" />
<option name="runOnExternalChanges" value="false" />
<option name="scopeName" value="Project Files" />
<option name="trackOnlyRoot" value="true" />
<option name="workingDir" value="$ProjectFileDir$" />
<envs>
<env name="GOROOT" value="$GOROOT$" />
<env name="GOPATH" value="$GOPATH$" />
<env name="PATH" value="$GoBinDirs$" />
</envs>
</TaskOptions>
</component>
</project>

View File

@ -1,9 +1,8 @@
package index package index
import ( import (
"net/http"
"fmt" "fmt"
"net/http"
"brainbaking.com/go-jamming/common" "brainbaking.com/go-jamming/common"
) )
@ -14,4 +13,3 @@ func Handle(conf *common.Config) http.HandlerFunc {
fmt.Fprintf(w, "This is a Jamstack microservice endpoint.\nWanna start jammin' too? Go to https://github.com/wgroeneveld/go-jamming !") fmt.Fprintf(w, "This is a Jamstack microservice endpoint.\nWanna start jammin' too? Go to https://github.com/wgroeneveld/go-jamming !")
} }
} }

View File

@ -1,4 +1,3 @@
package app package app
import ( import (

View File

@ -1,9 +1,9 @@
package mf package mf
import ( import (
"brainbaking.com/go-jamming/common"
"crypto/md5" "crypto/md5"
"fmt" "fmt"
"brainbaking.com/go-jamming/common"
"net/url" "net/url"
) )
@ -17,7 +17,7 @@ func (wm *Mention) String() string {
} }
func (wm *Mention) AsPath(conf *common.Config) string { func (wm *Mention) AsPath(conf *common.Config) string {
filename := fmt.Sprintf("%x", md5.Sum([]byte("source=" + wm.Source+ ",target=" + wm.Target))) filename := fmt.Sprintf("%x", md5.Sum([]byte("source="+wm.Source+",target="+wm.Target)))
domain, _ := conf.FetchDomain(wm.Target) domain, _ := conf.FetchDomain(wm.Target)
return conf.DataPath + "/" + domain + "/" + filename + ".json" return conf.DataPath + "/" + domain + "/" + filename + ".json"
} }

View File

@ -1,10 +1,10 @@
package mf package mf
import ( import (
"brainbaking.com/go-jamming/common"
"strings" "strings"
"time" "time"
"willnorris.com/go/microformats" "willnorris.com/go/microformats"
"brainbaking.com/go-jamming/common"
) )
const ( const (

View File

@ -1,13 +1,12 @@
package pingback package pingback
import ( import (
"encoding/xml"
"github.com/rs/zerolog/log"
"brainbaking.com/go-jamming/app/mf" "brainbaking.com/go-jamming/app/mf"
"brainbaking.com/go-jamming/app/webmention/recv" "brainbaking.com/go-jamming/app/webmention/recv"
"brainbaking.com/go-jamming/common" "brainbaking.com/go-jamming/common"
"brainbaking.com/go-jamming/rest" "brainbaking.com/go-jamming/rest"
"encoding/xml"
"github.com/rs/zerolog/log"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
) )
@ -94,5 +93,3 @@ func pingbackError(w http.ResponseWriter, msg string) {
w.WriteHeader(200) w.WriteHeader(200)
w.Write([]byte(xml)) w.Write([]byte(xml))
} }

View File

@ -1,9 +1,9 @@
package send package send
import ( import (
"github.com/rs/zerolog/log"
"brainbaking.com/go-jamming/app/mf" "brainbaking.com/go-jamming/app/mf"
"brainbaking.com/go-jamming/rest" "brainbaking.com/go-jamming/rest"
"github.com/rs/zerolog/log"
"strings" "strings"
) )

View File

@ -1,9 +1,9 @@
package send package send
import ( import (
"github.com/stretchr/testify/assert"
"brainbaking.com/go-jamming/app/mf" "brainbaking.com/go-jamming/app/mf"
"brainbaking.com/go-jamming/mocks" "brainbaking.com/go-jamming/mocks"
"github.com/stretchr/testify/assert"
"testing" "testing"
) )

View File

@ -1,9 +1,9 @@
package pingback package pingback
import ( import (
"brainbaking.com/go-jamming/common"
"encoding/xml" "encoding/xml"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"brainbaking.com/go-jamming/common"
"testing" "testing"
) )

View File

@ -15,7 +15,7 @@ import "encoding/xml"
</param> </param>
</params> </params>
</methodCall> </methodCall>
*/ */
type XmlRPCMethodCall struct { type XmlRPCMethodCall struct {
XMLName xml.Name `xml:"methodCall"` XMLName xml.Name `xml:"methodCall"`
MethodName string `xml:"methodName"` MethodName string `xml:"methodName"`

View File

@ -1,4 +1,3 @@
package app package app
import ( import (
@ -19,4 +18,3 @@ func (s *server) routes() {
s.router.HandleFunc("/webmention/{domain}/{token}", s.authorizedOnly(webmention.HandleGet(cnf))).Methods("GET") s.router.HandleFunc("/webmention/{domain}/{token}", s.authorizedOnly(webmention.HandleGet(cnf))).Methods("GET")
s.router.HandleFunc("/webmention/{domain}/{token}", s.authorizedOnly(webmention.HandlePut(cnf))).Methods("PUT") s.router.HandleFunc("/webmention/{domain}/{token}", s.authorizedOnly(webmention.HandlePut(cnf))).Methods("PUT")
} }

View File

@ -1,4 +1,3 @@
package app package app
import ( import (
@ -17,7 +16,9 @@ type server struct {
} }
// mimicing NotFound: https://golang.org/src/net/http/server.go?s=64787:64830#L2076 // mimicing NotFound: https://golang.org/src/net/http/server.go?s=64787:64830#L2076
func unauthorized(w http.ResponseWriter, r *http.Request) { http.Error(w, "401 unauthorized", http.StatusUnauthorized) } func unauthorized(w http.ResponseWriter, r *http.Request) {
http.Error(w, "401 unauthorized", http.StatusUnauthorized)
}
func (s *server) authorizedOnly(h http.HandlerFunc) http.HandlerFunc { func (s *server) authorizedOnly(h http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
@ -41,5 +42,5 @@ func Start() {
r.Use(loggingMiddleware) r.Use(loggingMiddleware)
log.Info().Int("port", server.conf.Port).Msg("Serving...") log.Info().Int("port", server.conf.Port).Msg("Serving...")
http.ListenAndServe(":" + strconv.Itoa(server.conf.Port), nil) http.ListenAndServe(":"+strconv.Itoa(server.conf.Port), nil)
} }

View File

@ -1,9 +1,9 @@
package app package app
import ( import (
"brainbaking.com/go-jamming/common"
"github.com/gorilla/mux" "github.com/gorilla/mux"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"brainbaking.com/go-jamming/common"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"testing" "testing"
@ -11,7 +11,7 @@ import (
var conf = &common.Config{ var conf = &common.Config{
Token: "boemsjakkalakka", Token: "boemsjakkalakka",
AllowedWebmentionSources: []string{ "http://ewelja.be" }, AllowedWebmentionSources: []string{"http://ewelja.be"},
} }
func TestAuthorizedOnlyUnauthorizedWithWrongToken(t *testing.T) { func TestAuthorizedOnlyUnauthorizedWithWrongToken(t *testing.T) {

View File

@ -1,12 +1,11 @@
package webmention package webmention
import ( import (
"fmt"
"github.com/gorilla/mux"
"brainbaking.com/go-jamming/app/mf" "brainbaking.com/go-jamming/app/mf"
"brainbaking.com/go-jamming/app/webmention/recv" "brainbaking.com/go-jamming/app/webmention/recv"
"brainbaking.com/go-jamming/app/webmention/send" "brainbaking.com/go-jamming/app/webmention/send"
"fmt"
"github.com/gorilla/mux"
"net/http" "net/http"
"brainbaking.com/go-jamming/common" "brainbaking.com/go-jamming/common"
@ -71,4 +70,3 @@ func HandlePost(conf *common.Config) http.HandlerFunc {
rest.Accept(w) rest.Accept(w)
} }
} }

View File

@ -1,11 +1,10 @@
package recv package recv
import ( import (
"encoding/json"
"brainbaking.com/go-jamming/app/mf" "brainbaking.com/go-jamming/app/mf"
"brainbaking.com/go-jamming/common" "brainbaking.com/go-jamming/common"
"brainbaking.com/go-jamming/rest" "brainbaking.com/go-jamming/rest"
"encoding/json"
"io/fs" "io/fs"
"io/ioutil" "io/ioutil"
"os" "os"
@ -16,7 +15,6 @@ import (
"willnorris.com/go/microformats" "willnorris.com/go/microformats"
) )
// used as a "class" to iject dependencies, just to be able to test. Do NOT like htis. // used as a "class" to iject dependencies, just to be able to test. Do NOT like htis.
// Is there a better way? e.g. in validate, I just pass rest.Client as an arg. Not great either. // Is there a better way? e.g. in validate, I just pass rest.Client as an arg. Not great either.
type Receiver struct { type Receiver struct {
@ -50,7 +48,6 @@ func getHEntry(data *microformats.Data) *microformats.Microformat {
return nil return nil
} }
func (recv *Receiver) processSourceBody(body string, wm mf.Mention) { func (recv *Receiver) processSourceBody(body string, wm mf.Mention) {
if !strings.Contains(body, wm.Target) { if !strings.Contains(body, wm.Target) {
log.Warn().Str("target", wm.Target).Msg("ABORT: no mention of target found in html src of source!") log.Warn().Str("target", wm.Target).Msg("ABORT: no mention of target found in html src of source!")

View File

@ -1,10 +1,9 @@
package recv package recv
import ( import (
"brainbaking.com/go-jamming/app/mf"
"errors" "errors"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"brainbaking.com/go-jamming/app/mf"
"io/ioutil" "io/ioutil"
"os" "os"
"testing" "testing"
@ -15,13 +14,12 @@ import (
) )
var conf = &common.Config{ var conf = &common.Config{
AllowedWebmentionSources: []string { AllowedWebmentionSources: []string{
"jefklakscodex.com", "jefklakscodex.com",
}, },
DataPath: "testdata", DataPath: "testdata",
} }
func TestConvertWebmentionToPath(t *testing.T) { func TestConvertWebmentionToPath(t *testing.T) {
wm := mf.Mention{ wm := mf.Mention{
Source: "https://brainbaking.com", Source: "https://brainbaking.com",
@ -45,7 +43,7 @@ func TestReceive(t *testing.T) {
label string label string
wm mf.Mention wm mf.Mention
json string json string
} { }{
{ {
label: "receive a Webmention bookmark via twitter", label: "receive a Webmention bookmark via twitter",
wm: mf.Mention{ wm: mf.Mention{
@ -181,4 +179,3 @@ func TestProcessSourceBodyAbortsIfNoMentionOfTargetFoundInSourceHtml(t *testing.
receiver.processSourceBody("<html>my nice body</html>", wm) receiver.processSourceBody("<html>my nice body</html>", wm)
assert.NoFileExists(t, wm.AsPath(conf)) assert.NoFileExists(t, wm.AsPath(conf))
} }

View File

@ -1,11 +1,11 @@
package send package send
import ( import (
"github.com/rs/zerolog/log"
"brainbaking.com/go-jamming/app/mf" "brainbaking.com/go-jamming/app/mf"
"brainbaking.com/go-jamming/app/pingback/send" "brainbaking.com/go-jamming/app/pingback/send"
"brainbaking.com/go-jamming/common" "brainbaking.com/go-jamming/common"
"brainbaking.com/go-jamming/rest" "brainbaking.com/go-jamming/rest"
"github.com/rs/zerolog/log"
) )
type Sender struct { type Sender struct {

View File

@ -1,4 +1,3 @@
package webmention package webmention
import ( import (

View File

@ -1,10 +1,9 @@
package webmention package webmention
import ( import (
"testing"
"errors" "errors"
"net/http" "net/http"
"testing"
"brainbaking.com/go-jamming/common" "brainbaking.com/go-jamming/common"
"brainbaking.com/go-jamming/mocks" "brainbaking.com/go-jamming/mocks"
@ -17,14 +16,18 @@ type httpReqMock struct {
type httpHeaderMock struct { type httpHeaderMock struct {
contentType string contentType string
} }
func (mock *httpHeaderMock) Get(key string) string { func (mock *httpHeaderMock) Get(key string) string {
return mock.contentType return mock.contentType
} }
func (mock *httpReqMock) FormValue(key string) string { func (mock *httpReqMock) FormValue(key string) string {
switch key { switch key {
case "source": return mock.source case "source":
case "target": return mock.target return mock.source
default: return "" case "target":
return mock.target
default:
return ""
} }
} }
func buildHttpReq(source string, target string) *httpReqMock { func buildHttpReq(source string, target string) *httpReqMock {
@ -43,7 +46,7 @@ func TestValidate(t *testing.T) {
target string target string
contentType string contentType string
expected bool expected bool
} { }{
{ {
"is valid if source and target https urls", "is valid if source and target https urls",
"http://brainbaking.com/bla1", "http://brainbaking.com/bla1",
@ -105,7 +108,7 @@ func TestValidate(t *testing.T) {
for _, tc := range cases { for _, tc := range cases {
t.Run(tc.label, func(t *testing.T) { t.Run(tc.label, func(t *testing.T) {
httpReq := buildHttpReq(tc.source, tc.target) httpReq := buildHttpReq(tc.source, tc.target)
httpHeader := &httpHeaderMock{ contentType: tc.contentType } httpHeader := &httpHeaderMock{contentType: tc.contentType}
actual := validate(httpReq, httpHeader, config) actual := validate(httpReq, httpHeader, config)
if actual != tc.expected { if actual != tc.expected {

View File

@ -1,4 +1,3 @@
package common package common
import ( import (
@ -39,7 +38,7 @@ func (c *Config) FetchDomain(url string) (string, error) {
func (c *Config) SetupDataDirs() { func (c *Config) SetupDataDirs() {
for _, domain := range c.AllowedWebmentionSources { for _, domain := range c.AllowedWebmentionSources {
os.MkdirAll(c.DataPath + "/" + domain, os.ModePerm) os.MkdirAll(c.DataPath+"/"+domain, os.ModePerm)
log.Info().Str("allowedDomain", domain).Msg("Configured") log.Info().Str("allowedDomain", domain).Msg("Configured")
} }
} }
@ -60,8 +59,8 @@ func Configure() (c *Config) {
Token: token, Token: token,
UtcOffset: 60, UtcOffset: 60,
DataPath: "data", DataPath: "data",
AllowedWebmentionSources: []string{ "brainbaking.com", "jefklakscodex.com" }, AllowedWebmentionSources: []string{"brainbaking.com", "jefklakscodex.com"},
DisallowedWebmentionDomains: []string{ "youtube.com" }, DisallowedWebmentionDomains: []string{"youtube.com"},
} }
return return
} }

View File

@ -3,7 +3,7 @@ package common
import "testing" import "testing"
func TestIncludesElemInArrayTrue(t *testing.T) { func TestIncludesElemInArrayTrue(t *testing.T) {
arr := []string{ "one", "two"} arr := []string{"one", "two"}
result := Includes(arr, "two") result := Includes(arr, "two")
if result != true { if result != true {
@ -12,7 +12,7 @@ func TestIncludesElemInArrayTrue(t *testing.T) {
} }
func TestIncludesElemNotInArrayFalse(t *testing.T) { func TestIncludesElemNotInArrayFalse(t *testing.T) {
arr := []string{ "one", "two"} arr := []string{"one", "two"}
result := Includes(arr, "three") result := Includes(arr, "three")
if result != false { if result != false {

View File

@ -1,4 +1,3 @@
package main package main
import ( import (

View File

@ -1,11 +1,10 @@
package mocks package mocks
import ( import (
"strings"
"testing"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"strings"
"testing"
) )
// neat trick! https://medium.com/@matryer/meet-moq-easily-mock-interfaces-in-go-476444187d10 // neat trick! https://medium.com/@matryer/meet-moq-easily-mock-interfaces-in-go-476444187d10

View File

@ -2,9 +2,9 @@ package main
import ( import (
"fmt" "fmt"
"io/ioutil"
"log" "log"
"net/http" "net/http"
"io/ioutil"
) )
func mainz() { func mainz() {

View File

@ -1,4 +1,3 @@
package rest package rest
import ( import (
@ -45,7 +44,6 @@ func (client *HttpClient) GetBody(url string) (string, error) {
return string(body), nil return string(body), nil
} }
func (client *HttpClient) Get(url string) (*http.Response, error) { func (client *HttpClient) Get(url string) (*http.Response, error) {
return http.Get(url) return http.Get(url)
} }

View File

@ -1,4 +1,3 @@
package rest package rest
import ( import (

View File

@ -1,4 +1,3 @@
package rest package rest
// great, these are needed to do the structural typing for the tests... // great, these are needed to do the structural typing for the tests...