diff --git a/Makefile b/Makefile index 1ace631..0c75336 100644 --- a/Makefile +++ b/Makefile @@ -21,9 +21,7 @@ getGoDependencies: go get -v ./... buildProject: - go run build/schema.go @mkdir releases gox -output="releases/{{.OS}}_{{.Arch}}/{{.OS}}_{{.Arch}}" find releases -maxdepth 1 -mindepth 1 -type d -exec cp build/config.json {} \; - find releases -maxdepth 1 -mindepth 1 -type d -exec cp build/schema.json {} \; find releases -maxdepth 1 -mindepth 1 -type d -exec tar -cvjf {}.tar.bz2 {} \; diff --git a/README.md b/README.md index e5abe93..67bc05d 100644 --- a/README.md +++ b/README.md @@ -19,25 +19,12 @@ - Easy [ShareX](https://github.com/ShareX/ShareX) integration - Dockerizable -## Server Installation +## Documenation -### Standard - -Download the package for your architecture and operating system from [bintray](https://bintray.com/maxibanki/golang-url-shortener/travis-ci) and extract it. - -### Docker - -TODO - -## Configuration - -The configuration is a JSON file, an example is located [here](build/config.json). If your editor supports intellisense by using a schema (e.g. [VS Code](https://github.com/Microsoft/vscode)) then you can simply press space for auto completion. The config parameters should be really self explaining, but [here](build/schema.md) is a detailed description for all of these: - -## OAuth - -### Google - -Visit [console.cloud.google.com](https://console.cloud.google.com) and create or use an existing project, go to `APIs & Services` -> `Credentials` and create there an `OAuth Client-ID` for the application type `Webapplicaton`. There you get the Client-ID and the ClientSecret for your configuration. It's important, that you set in the Google Cloud Platform `YOUR_URL/api/v1/callback` as authorized redirect URL. +- [Installation](https://github.com/maxibanki/golang-url-shortener/wiki/Installation) +- [Configuration](https://github.com/maxibanki/golang-url-shortener/wiki/Configuration) +- [Setting up OAuth](https://github.com/maxibanki/golang-url-shortener/wiki/Setting-up-OAuth) +- [ShareX Usage](https://github.com/maxibanki/golang-url-shortener/wiki/ShareX) ## Clients @@ -51,10 +38,6 @@ In general the `POST` endpoints can be called, by using one of the following tec For all the endpoints which are on `/api/v1/protected` there is the `Authorization` header required. -### [ShareX](https://github.com/ShareX/ShareX) - -For ShareX usage, we refer to the menu item in the frontend where your configuration will be generated. There are further information for the detailed use. - ## Why did you built this Just only because I want to extend my current self hosted URL shorter with some features and learn about new techniques like: @@ -64,15 +47,3 @@ Just only because I want to extend my current self hosted URL shorter with some - Makefiles - Travis CI - Key / Value databases - -## Utils - -### Update Config Documentation - -``` -yarn global add jsonschema-md -go run build/schema.go -jsonschema-md.cmd build/schema.json > build/schema.md -``` - -After that adjust the title to `Configuration` and the description to `Golang URL Shortener Configuration`. \ No newline at end of file diff --git a/build/config.json b/build/config.json index 26edcd3..26590ad 100644 --- a/build/config.json +++ b/build/config.json @@ -1,5 +1,4 @@ { - "$schema": "./schema.json", "Store": { "DBPath": "main.db", "ShortedIDLength": 4 diff --git a/build/schema.go b/build/schema.go deleted file mode 100644 index b8c31d4..0000000 --- a/build/schema.go +++ /dev/null @@ -1,20 +0,0 @@ -package main - -import ( - "flag" - "io/ioutil" - "log" - "path/filepath" - - "github.com/maxibanki/golang-url-shortener/config" - "github.com/urakozz/go-json-schema-generator" -) - -func main() { - schemaPath := flag.String("path", filepath.Join("build", "schema.json"), "location of the converted schema") - flag.Parse() - schema := generator.Generate(&config.Configuration{}) - if err := ioutil.WriteFile(*schemaPath, []byte(schema), 755); err != nil { - log.Fatalf("could not write schema: %v", err) - } -} diff --git a/build/schema.md b/build/schema.md deleted file mode 100644 index d972ddf..0000000 Binary files a/build/schema.md and /dev/null differ diff --git a/config/config.go b/config/config.go index 554a6ce..38960cb 100644 --- a/config/config.go +++ b/config/config.go @@ -12,29 +12,28 @@ import ( // Configuration holds all the needed parameters use // the URL Shortener type Configuration struct { - Schema string `json:"$schema"` - Store Store `description:"Store holds the configuration values for the storage package"` - Handlers Handlers `description:"Handlers holds the configuration for the handlers package"` + Store Store + Handlers Handlers } // Store contains the needed fields for the Store package type Store struct { - DBPath string `description:"relative or absolute path of your bolt DB"` - ShortedIDLength uint `description:"Length of the random generated ID which is used for new shortened URLs"` + DBPath string + ShortedIDLength uint } // Handlers contains the needed fields for the Handlers package type Handlers struct { - ListenAddr string `description:"Consists of 'IP:Port', normally the value ':8080' e.g. is enough"` - BaseURL string `description:"Required for the authentification via OAuth. E.g. 'http://mydomain.com'"` - EnableDebugMode bool `description:"Activates more detailed logging to the stdout"` - Secret []byte `description:"Used for encryption of the JWT and for the CookieJar. Will be randomly generated when it isn't set"` + ListenAddr string + BaseURL string + EnableDebugMode bool + Secret []byte OAuth struct { Google struct { - ClientID string `description:"ClientID which you get from console.cloud.google.com"` - ClientSecret string `description:"ClientSecret which get from console.cloud.google.com"` - } `description:"Google holds the OAuth configuration for the Google provider"` - } `description:"OAuth holds the OAuth specific settings"` + ClientID string + ClientSecret string + } + } } var ( diff --git a/handlers/auth_test.go b/handlers/auth_test.go index 9433091..b41aa90 100644 --- a/handlers/auth_test.go +++ b/handlers/auth_test.go @@ -52,11 +52,10 @@ func TestCreateBackend(t *testing.T) { ListenAddr: ":8080", Secret: secret, BaseURL: "http://127.0.0.1", - }, *store, logrus.New()) + }, *store, logrus.New(), true) if err != nil { t.Fatalf("could not create handler: %v", err) } - handler.DoNotCheckConfigViaGet = true server = httptest.NewServer(handler.engine) closeServer = func() error { server.Close() diff --git a/handlers/handlers.go b/handlers/handlers.go index 3584ac9..6f462c1 100644 --- a/handlers/handlers.go +++ b/handlers/handlers.go @@ -30,7 +30,7 @@ type Handler struct { } // New initializes the http handlers -func New(handlerConfig config.Handlers, store store.Store, log *logrus.Logger) (*Handler, error) { +func New(handlerConfig config.Handlers, store store.Store, log *logrus.Logger, testing bool) (*Handler, error) { if !handlerConfig.EnableDebugMode { gin.SetMode(gin.ReleaseMode) } @@ -43,8 +43,10 @@ func New(handlerConfig config.Handlers, store store.Store, log *logrus.Logger) ( if err := h.setHandlers(); err != nil { return nil, errors.Wrap(err, "could not set handlers") } - if err := h.checkIfSecretExist(); err != nil { - return nil, errors.Wrap(err, "could not check if secret exist") + if !testing { + if err := h.checkIfSecretExist(); err != nil { + return nil, errors.Wrap(err, "could not check if secret exist") + } } h.initOAuth() return h, nil @@ -64,7 +66,7 @@ func (h *Handler) setTemplateFromFS(name string) error { } func (h *Handler) checkIfSecretExist() error { - if h.DoNotCheckConfigViaGet { + if !h.DoNotCheckConfigViaGet { conf := config.Get() if conf.Handlers.Secret == nil { b := make([]byte, 128) diff --git a/main.go b/main.go index 3b2d0a4..9574d4c 100644 --- a/main.go +++ b/main.go @@ -39,7 +39,7 @@ func initShortener(log *logrus.Logger) (func(), error) { if err != nil { return nil, errors.Wrap(err, "could not create store") } - handler, err := handlers.New(conf.Handlers, *store, log) + handler, err := handlers.New(conf.Handlers, *store, log, false) if err != nil { return nil, errors.Wrap(err, "could not create handlers") }