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
import (
"net/http"
"fmt"
"net/http"
"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 !")
}
}

View File

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

View File

@ -1,9 +1,9 @@
package mf
import (
"brainbaking.com/go-jamming/common"
"crypto/md5"
"fmt"
"brainbaking.com/go-jamming/common"
"net/url"
)
@ -17,7 +17,7 @@ func (wm *Mention) String() 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)
return conf.DataPath + "/" + domain + "/" + filename + ".json"
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,4 +1,3 @@
package app
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.HandlePut(cnf))).Methods("PUT")
}

View File

@ -1,4 +1,3 @@
package app
import (
@ -17,7 +16,9 @@ type server struct {
}
// 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 {
return func(w http.ResponseWriter, r *http.Request) {
@ -41,5 +42,5 @@ func Start() {
r.Use(loggingMiddleware)
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
import (
"brainbaking.com/go-jamming/common"
"github.com/gorilla/mux"
"github.com/stretchr/testify/assert"
"brainbaking.com/go-jamming/common"
"net/http"
"net/http/httptest"
"testing"
@ -11,7 +11,7 @@ import (
var conf = &common.Config{
Token: "boemsjakkalakka",
AllowedWebmentionSources: []string{ "http://ewelja.be" },
AllowedWebmentionSources: []string{"http://ewelja.be"},
}
func TestAuthorizedOnlyUnauthorizedWithWrongToken(t *testing.T) {

View File

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

View File

@ -1,11 +1,10 @@
package recv
import (
"encoding/json"
"brainbaking.com/go-jamming/app/mf"
"brainbaking.com/go-jamming/common"
"brainbaking.com/go-jamming/rest"
"encoding/json"
"io/fs"
"io/ioutil"
"os"
@ -16,7 +15,6 @@ import (
"willnorris.com/go/microformats"
)
// 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.
type Receiver struct {
@ -50,7 +48,6 @@ func getHEntry(data *microformats.Data) *microformats.Microformat {
return nil
}
func (recv *Receiver) processSourceBody(body string, wm mf.Mention) {
if !strings.Contains(body, wm.Target) {
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
import (
"brainbaking.com/go-jamming/app/mf"
"errors"
"github.com/stretchr/testify/assert"
"brainbaking.com/go-jamming/app/mf"
"io/ioutil"
"os"
"testing"
@ -15,13 +14,12 @@ import (
)
var conf = &common.Config{
AllowedWebmentionSources: []string {
AllowedWebmentionSources: []string{
"jefklakscodex.com",
},
DataPath: "testdata",
}
func TestConvertWebmentionToPath(t *testing.T) {
wm := mf.Mention{
Source: "https://brainbaking.com",
@ -45,7 +43,7 @@ func TestReceive(t *testing.T) {
label string
wm mf.Mention
json string
} {
}{
{
label: "receive a Webmention bookmark via twitter",
wm: mf.Mention{
@ -181,4 +179,3 @@ func TestProcessSourceBodyAbortsIfNoMentionOfTargetFoundInSourceHtml(t *testing.
receiver.processSourceBody("<html>my nice body</html>", wm)
assert.NoFileExists(t, wm.AsPath(conf))
}

View File

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

View File

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

View File

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

View File

@ -1,4 +1,3 @@
package common
import (
@ -39,7 +38,7 @@ func (c *Config) FetchDomain(url string) (string, error) {
func (c *Config) SetupDataDirs() {
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")
}
}
@ -60,8 +59,8 @@ func Configure() (c *Config) {
Token: token,
UtcOffset: 60,
DataPath: "data",
AllowedWebmentionSources: []string{ "brainbaking.com", "jefklakscodex.com" },
DisallowedWebmentionDomains: []string{ "youtube.com" },
AllowedWebmentionSources: []string{"brainbaking.com", "jefklakscodex.com"},
DisallowedWebmentionDomains: []string{"youtube.com"},
}
return
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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