Browse Source

- removed overpowered schema generation

- fixed unit test bug
- moved most of the docu to the github wiki
dependabot/npm_and_yarn/web/prismjs-1.21.0
Max Schmitt 8 years ago
parent
commit
dcfdeaa170
  1. 2
      Makefile
  2. 39
      README.md
  3. 1
      build/config.json
  4. 20
      build/schema.go
  5. BIN
      build/schema.md
  6. 25
      config/config.go
  7. 3
      handlers/auth_test.go
  8. 6
      handlers/handlers.go
  9. 2
      main.go

2
Makefile

@ -21,9 +21,7 @@ getGoDependencies:
go get -v ./... go get -v ./...
buildProject: buildProject:
go run build/schema.go
@mkdir releases @mkdir releases
gox -output="releases/{{.OS}}_{{.Arch}}/{{.OS}}_{{.Arch}}" 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/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 {} \; find releases -maxdepth 1 -mindepth 1 -type d -exec tar -cvjf {}.tar.bz2 {} \;

39
README.md

@ -19,25 +19,12 @@
- Easy [ShareX](https://github.com/ShareX/ShareX) integration - Easy [ShareX](https://github.com/ShareX/ShareX) integration
- Dockerizable - Dockerizable
## Server Installation ## Documenation
### Standard - [Installation](https://github.com/maxibanki/golang-url-shortener/wiki/Installation)
- [Configuration](https://github.com/maxibanki/golang-url-shortener/wiki/Configuration)
Download the package for your architecture and operating system from [bintray](https://bintray.com/maxibanki/golang-url-shortener/travis-ci) and extract it. - [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)
### 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.
## Clients ## 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. 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 ## 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: 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 - Makefiles
- Travis CI - Travis CI
- Key / Value databases - 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`.

1
build/config.json

@ -1,5 +1,4 @@
{ {
"$schema": "./schema.json",
"Store": { "Store": {
"DBPath": "main.db", "DBPath": "main.db",
"ShortedIDLength": 4 "ShortedIDLength": 4

20
build/schema.go

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

BIN
build/schema.md

Binary file not shown.

25
config/config.go

@ -12,29 +12,28 @@ import (
// Configuration holds all the needed parameters use // Configuration holds all the needed parameters use
// the URL Shortener // the URL Shortener
type Configuration struct { type Configuration struct {
Schema string `json:"$schema"` Store Store
Store Store `description:"Store holds the configuration values for the storage package"` Handlers Handlers
Handlers Handlers `description:"Handlers holds the configuration for the handlers package"`
} }
// Store contains the needed fields for the Store package // Store contains the needed fields for the Store package
type Store struct { type Store struct {
DBPath string `description:"relative or absolute path of your bolt DB"` DBPath string
ShortedIDLength uint `description:"Length of the random generated ID which is used for new shortened URLs"` ShortedIDLength uint
} }
// Handlers contains the needed fields for the Handlers package // Handlers contains the needed fields for the Handlers package
type Handlers struct { type Handlers struct {
ListenAddr string `description:"Consists of 'IP:Port', normally the value ':8080' e.g. is enough"` ListenAddr string
BaseURL string `description:"Required for the authentification via OAuth. E.g. 'http://mydomain.com'"` BaseURL string
EnableDebugMode bool `description:"Activates more detailed logging to the stdout"` EnableDebugMode bool
Secret []byte `description:"Used for encryption of the JWT and for the CookieJar. Will be randomly generated when it isn't set"` Secret []byte
OAuth struct { OAuth struct {
Google struct { Google struct {
ClientID string `description:"ClientID which you get from console.cloud.google.com"` ClientID string
ClientSecret string `description:"ClientSecret which get from console.cloud.google.com"` ClientSecret string
} `description:"Google holds the OAuth configuration for the Google provider"` }
} `description:"OAuth holds the OAuth specific settings"` }
} }
var ( var (

3
handlers/auth_test.go

@ -52,11 +52,10 @@ func TestCreateBackend(t *testing.T) {
ListenAddr: ":8080", ListenAddr: ":8080",
Secret: secret, Secret: secret,
BaseURL: "http://127.0.0.1", BaseURL: "http://127.0.0.1",
}, *store, logrus.New()) }, *store, logrus.New(), true)
if err != nil { if err != nil {
t.Fatalf("could not create handler: %v", err) t.Fatalf("could not create handler: %v", err)
} }
handler.DoNotCheckConfigViaGet = true
server = httptest.NewServer(handler.engine) server = httptest.NewServer(handler.engine)
closeServer = func() error { closeServer = func() error {
server.Close() server.Close()

6
handlers/handlers.go

@ -30,7 +30,7 @@ type Handler struct {
} }
// New initializes the http handlers // 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 { if !handlerConfig.EnableDebugMode {
gin.SetMode(gin.ReleaseMode) gin.SetMode(gin.ReleaseMode)
} }
@ -43,9 +43,11 @@ func New(handlerConfig config.Handlers, store store.Store, log *logrus.Logger) (
if err := h.setHandlers(); err != nil { if err := h.setHandlers(); err != nil {
return nil, errors.Wrap(err, "could not set handlers") return nil, errors.Wrap(err, "could not set handlers")
} }
if !testing {
if err := h.checkIfSecretExist(); err != nil { if err := h.checkIfSecretExist(); err != nil {
return nil, errors.Wrap(err, "could not check if secret exist") return nil, errors.Wrap(err, "could not check if secret exist")
} }
}
h.initOAuth() h.initOAuth()
return h, nil return h, nil
} }
@ -64,7 +66,7 @@ func (h *Handler) setTemplateFromFS(name string) error {
} }
func (h *Handler) checkIfSecretExist() error { func (h *Handler) checkIfSecretExist() error {
if h.DoNotCheckConfigViaGet { if !h.DoNotCheckConfigViaGet {
conf := config.Get() conf := config.Get()
if conf.Handlers.Secret == nil { if conf.Handlers.Secret == nil {
b := make([]byte, 128) b := make([]byte, 128)

2
main.go

@ -39,7 +39,7 @@ func initShortener(log *logrus.Logger) (func(), error) {
if err != nil { if err != nil {
return nil, errors.Wrap(err, "could not create store") 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 { if err != nil {
return nil, errors.Wrap(err, "could not create handlers") return nil, errors.Wrap(err, "could not create handlers")
} }

Loading…
Cancel
Save