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"
) )

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

@ -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) {

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"

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"
@ -21,7 +20,6 @@ var conf = &common.Config{
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",
@ -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 {

View File

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

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...