Browse Source

Minor changes:

- Switched the config to JSON
- Added Frontend
- Added fist oAuth parts
dependabot/npm_and_yarn/web/prismjs-1.21.0
Max Schmitt 8 years ago
parent
commit
54cde0244c
  1. 2
      .gitignore
  2. 3
      .vscode/settings.json
  3. 25
      README.md
  4. 3
      config.yml
  5. 53
      config/config.go
  6. 122
      handlers/handlers.go
  7. 12
      handlers/handlers_test.go
  8. 25
      main.go
  9. 21
      static/.gitignore
  10. 2228
      static/README.md
  11. 15
      static/index.html
  12. 24
      static/package.json
  13. BIN
      static/public/favicon.ico
  14. 40
      static/public/index.html
  15. 15
      static/public/manifest.json
  16. 11
      static/src/App/App.css
  17. 70
      static/src/App/App.js
  18. 8
      static/src/App/App.test.js
  19. 5
      static/src/index.css
  20. 18
      static/src/index.html
  21. 9
      static/src/index.js
  22. 108
      static/src/registerServiceWorker.js
  23. 6505
      static/yarn.lock
  24. 9
      store/store.go
  25. 20
      store/store_test.go

2
.gitignore

@ -15,3 +15,5 @@
main.db main.db
debug debug
*db.lock *db.lock
static/bower_components
config.json

3
.vscode/settings.json

@ -0,0 +1,3 @@
{
"prettier.singleQuote": true
}

25
README.md

@ -1,4 +1,4 @@
# Golang URL Shortener # Golang URL Shortener (Work in Progress)
[![Build Status](https://travis-ci.org/maxibanki/golang-url-shortener.svg?branch=master)](https://travis-ci.org/maxibanki/golang-url-shortener) [![Build Status](https://travis-ci.org/maxibanki/golang-url-shortener.svg?branch=master)](https://travis-ci.org/maxibanki/golang-url-shortener)
[![GoDoc](https://godoc.org/github.com/maxibanki/golang-url-shortener?status.svg)](https://godoc.org/github.com/maxibanki/golang-url-shortener) [![GoDoc](https://godoc.org/github.com/maxibanki/golang-url-shortener?status.svg)](https://godoc.org/github.com/maxibanki/golang-url-shortener)
@ -36,10 +36,20 @@ Only execute the [docker-compose.yml](docker-compose.yml) and adjust the environ
The configuration is a yaml based file of key value pairs. It is located in the installation folder and is called `config.yml`: The configuration is a yaml based file of key value pairs. It is located in the installation folder and is called `config.yml`:
```yaml ```json
DBPath: main.db # Location of the bolt DB database {
ListenAddr: :8080 # RemoteAddr of the http server. (IP:Port) "General": {
ShortedIDLength: 4 # Length of the random generated ID "DBPath": "main.db", // Location of the bolt DB database
"ListenAddr": ":8080", // Listen address of the http server (IP:Port)
"ShortedIDLength": 4 // Length of the random generated ID
},
"OAuth": {
"Google": {
"ClientID": "", // Google client ID
"ClientSecret": "" // Google client secret
}
}
}
``` ```
## Clients: ## Clients:
@ -113,6 +123,10 @@ This handler returns the information about an entry. This includes:
For that you need to send a field `ID` to the backend. For that you need to send a field `ID` to the backend.
## Why did you built this?
Just only because I want to extend my current self hosted URL shorter and learn about new techniques like Go unit testing and react.
## TODO ## TODO
Next changes sorted by priority Next changes sorted by priority
@ -121,6 +135,7 @@ Next changes sorted by priority
- [x] Switch configuration to Yaml - [x] Switch configuration to Yaml
- [ ] Add Authorization (oAuth2 e.g. Google) - [ ] Add Authorization (oAuth2 e.g. Google)
- [ ] Add Deletion functionality (depends on the authorization) - [ ] Add Deletion functionality (depends on the authorization)
- [ ] Refactore Unit Tests
- [ ] Performance optimization - [ ] Performance optimization
- [ ] Add ability to track the visitors (Referrer, maybe also live) - [ ] Add ability to track the visitors (Referrer, maybe also live)
- [ ] Test docker-compose installation - [ ] Test docker-compose installation

3
config.yml

@ -1,3 +0,0 @@
DBPath: main.db # Location of the bolt DB database
ListenAddr: :8080 # RemoteAddr of the http server. (IP:Port)
ShortedIDLength: 4 # Length of the random generated ID

53
config/config.go

@ -0,0 +1,53 @@
package config
import (
"encoding/json"
"io/ioutil"
"os"
"path/filepath"
"github.com/pkg/errors"
)
// Configuration holds all the needed parameters use
// the URL Shortener
type Configuration struct {
Store Store
Handlers Handlers
}
// Store contains the needed fields for the Store package
type Store struct {
DBPath string
ShortedIDLength int
}
// Handlers contains the needed fields for the Handlers package
type Handlers struct {
ListenAddr string
EnableGinDebugMode bool
OAuth struct {
Google struct {
ClientID string
ClientSecret string
}
}
}
// Get returns the configuration from a given file
func Get() (*Configuration, error) {
var config *Configuration
ex, err := os.Executable()
if err != nil {
return nil, errors.Wrap(err, "could not get executable path")
}
file, err := ioutil.ReadFile(filepath.Join(filepath.Dir(ex), "config.json"))
if err != nil {
return nil, errors.Wrap(err, "could not read configuration file")
}
err = json.Unmarshal(file, &config)
if err != nil {
return nil, errors.Wrap(err, "could not unmarshal configuration file")
}
return config, nil
}

122
handlers/handlers.go

@ -2,19 +2,28 @@
package handlers package handlers
import ( import (
"crypto/rand"
"encoding/base64"
"encoding/json"
"fmt" "fmt"
"io/ioutil"
"net/http" "net/http"
"github.com/gin-gonic/contrib/sessions"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/maxibanki/golang-url-shortener/config"
"github.com/maxibanki/golang-url-shortener/store" "github.com/maxibanki/golang-url-shortener/store"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
) )
// Handler holds the funcs and attributes for the // Handler holds the funcs and attributes for the
// http communication // http communication
type Handler struct { type Handler struct {
addr string config config.Handlers
store store.Store store store.Store
engine *gin.Engine engine *gin.Engine
oAuthConf *oauth2.Config
} }
// URLUtil is used to help in- and outgoing requests for json // URLUtil is used to help in- and outgoing requests for json
@ -23,22 +32,117 @@ type URLUtil struct {
URL string URL string
} }
type oAuthUser struct {
Sub string `json:"sub"`
Name string `json:"name"`
GivenName string `json:"given_name"`
FamilyName string `json:"family_name"`
Profile string `json:"profile"`
Picture string `json:"picture"`
Email string `json:"email"`
EmailVerified bool `json:"email_verified"`
Gender string `json:"gender"`
Hd string `json:"hd"`
}
// New initializes the http handlers // New initializes the http handlers
func New(addr string, store store.Store) *Handler { func New(handlerConfig config.Handlers, store store.Store) *Handler {
h := &Handler{ h := &Handler{
addr: addr, config: handlerConfig,
store: store, store: store,
engine: gin.Default(), engine: gin.Default(),
} }
h.setHandlers() h.setHandlers()
h.initOAuth()
return h return h
} }
func (h *Handler) setHandlers() { func (h *Handler) setHandlers() {
if !h.config.EnableGinDebugMode {
gin.SetMode(gin.ReleaseMode)
}
h.engine.POST("/api/v1/create", h.handleCreate) h.engine.POST("/api/v1/create", h.handleCreate)
h.engine.POST("/api/v1/info", h.handleInfo) h.engine.POST("/api/v1/info", h.handleInfo)
h.engine.StaticFile("/", "static/index.html") // h.engine.Static("/static", "static/src")
h.engine.GET("/:id", h.handleAccess) h.engine.NoRoute(h.handleAccess)
}
func (h *Handler) initOAuth() {
store := sessions.NewCookieStore([]byte("secret"))
h.oAuthConf = &oauth2.Config{
ClientID: h.config.OAuth.Google.ClientID,
ClientSecret: h.config.OAuth.Google.ClientSecret,
RedirectURL: "http://127.0.0.1:3000/api/v1/auth/",
Scopes: []string{
"https://www.googleapis.com/auth/userinfo.email",
},
Endpoint: google.Endpoint,
}
h.engine.Use(sessions.Sessions("goquestsession", store))
h.engine.GET("/api/v1/login", h.handleGoogleLogin)
private := h.engine.Group("/api/v1/auth")
private.Use(h.handleGoogleAuth)
private.GET("/", h.handleGoogleCallback)
private.GET("/api", func(c *gin.Context) {
c.JSON(200, gin.H{"message": "Hello from private for groups"})
})
}
func (h *Handler) randToken() string {
b := make([]byte, 32)
rand.Read(b)
return base64.StdEncoding.EncodeToString(b)
}
func (h *Handler) handleGoogleAuth(c *gin.Context) {
// Handle the exchange code to initiate a transport.
session := sessions.Default(c)
retrievedState := session.Get("state")
if retrievedState != c.Query("state") {
c.JSON(http.StatusUnauthorized, gin.H{"error": fmt.Errorf("Invalid session state: %s", retrievedState)})
return
}
token, err := h.oAuthConf.Exchange(oauth2.NoContext, c.Query("code"))
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
client := h.oAuthConf.Client(oauth2.NoContext, token)
userinfo, err := client.Get("https://www.googleapis.com/oauth2/v3/userinfo")
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
defer userinfo.Body.Close()
data, err := ioutil.ReadAll(userinfo.Body)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Could not read body: %v", err)})
return
}
var user oAuthUser
err = json.Unmarshal(data, &user)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Decoding userinfo failed: %v", err)})
return
}
c.Set("user", user)
}
func (h *Handler) handleGoogleLogin(c *gin.Context) {
state := h.randToken()
session := sessions.Default(c)
session.Set("state", state)
session.Save()
c.Redirect(http.StatusTemporaryRedirect, h.oAuthConf.AuthCodeURL(state))
}
func (h *Handler) handleGoogleCallback(ctx *gin.Context) {
ctx.JSON(http.StatusOK, gin.H{"Hello": "from private", "user": ctx.MustGet("user").(oAuthUser)})
} }
// handleCreate handles requests to create an entry // handleCreate handles requests to create an entry
@ -107,10 +211,10 @@ func (h *Handler) handleAccess(c *gin.Context) {
// Listen starts the http server // Listen starts the http server
func (h *Handler) Listen() error { func (h *Handler) Listen() error {
return h.engine.Run(h.addr) return h.engine.Run(h.config.ListenAddr)
} }
// Stop stops the http server and the closes the db gracefully // CloseStore stops the http server and the closes the db gracefully
func (h *Handler) CloseStore() error { func (h *Handler) CloseStore() error {
return h.store.Close() return h.store.Close()
} }

12
handlers/handlers_test.go

@ -3,6 +3,7 @@ package handlers
import ( import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"fmt"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
@ -12,6 +13,7 @@ import (
"testing" "testing"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/maxibanki/golang-url-shortener/config"
"github.com/maxibanki/golang-url-shortener/store" "github.com/maxibanki/golang-url-shortener/store"
"github.com/pkg/errors" "github.com/pkg/errors"
) )
@ -90,6 +92,7 @@ func TestCreateEntry(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("could not unmarshal data: %v", err) t.Fatalf("could not unmarshal data: %v", err)
} }
fmt.Println(parsed.URL)
t.Run("test if shorted URL is correct", func(t *testing.T) { t.Run("test if shorted URL is correct", func(t *testing.T) {
testRedirect(t, parsed.URL, tc.requestBody.URL) testRedirect(t, parsed.URL, tc.requestBody.URL)
}) })
@ -237,11 +240,16 @@ func testRedirect(t *testing.T, shortURL, longURL string) {
} }
func getBackend() (func(), error) { func getBackend() (func(), error) {
store, err := store.New(testingDBName, 4) store, err := store.New(config.Store{
DBPath: testingDBName,
ShortedIDLength: 4,
})
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 := New(":8080", *store) handler := New(config.Handlers{
ListenAddr: ":8080",
}, *store)
if err != nil { if err != nil {
return nil, errors.Wrap(err, "could not create handler") return nil, errors.Wrap(err, "could not create handler")
} }

25
main.go

@ -1,16 +1,14 @@
package main package main
import ( import (
"io/ioutil"
"log" "log"
"os" "os"
"os/signal" "os/signal"
"path/filepath"
"github.com/maxibanki/golang-url-shortener/config"
"github.com/maxibanki/golang-url-shortener/handlers" "github.com/maxibanki/golang-url-shortener/handlers"
"github.com/maxibanki/golang-url-shortener/store" "github.com/maxibanki/golang-url-shortener/store"
"github.com/pkg/errors" "github.com/pkg/errors"
yaml "gopkg.in/yaml.v2"
) )
func main() { func main() {
@ -26,28 +24,15 @@ func main() {
} }
func initShortener() (func(), error) { func initShortener() (func(), error) {
var config struct { config, err := config.Get()
DBPath string `yaml:"DBPath"`
ListenAddr string `yaml:"ListenAddr"`
ShortedIDLength int `yaml:"ShortedIDLength"`
}
ex, err := os.Executable()
if err != nil {
return nil, errors.Wrap(err, "could not get executable path")
}
file, err := ioutil.ReadFile(filepath.Join(filepath.Dir(ex), "config.yml"))
if err != nil {
return nil, errors.Wrap(err, "could not read configuration file: %v")
}
err = yaml.Unmarshal(file, &config)
if err != nil { if err != nil {
return nil, errors.Wrap(err, "could not unmarshal yaml file") return nil, errors.Wrap(err, "could not get config")
} }
store, err := store.New(config.DBPath, config.ShortedIDLength) store, err := store.New(config.Store)
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 := handlers.New(config.ListenAddr, *store) handler := handlers.New(config.Handlers, *store)
go func() { go func() {
err := handler.Listen() err := handler.Listen()
if err != nil { if err != nil {

21
static/.gitignore

@ -0,0 +1,21 @@
# See https://help.github.com/ignore-files/ for more about ignoring files.
# dependencies
/node_modules
# testing
/coverage
# production
/build
# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local
npm-debug.log*
yarn-debug.log*
yarn-error.log*

2228
static/README.md

File diff suppressed because it is too large

15
static/index.html

@ -1,15 +0,0 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Great page!</title>
</head>
<body>
</body>
</html>

24
static/package.json

@ -0,0 +1,24 @@
{
"name": "golang-url-shortener",
"version": "0.1.0",
"private": true,
"proxy": {
"/api": {
"target": "http://127.0.0.1:8080",
"ws": true
}
},
"dependencies": {
"react": "^16.0.0",
"react-dom": "^16.0.0",
"react-scripts": "1.0.16",
"semantic-ui-css": "^2.2.12",
"semantic-ui-react": "^0.75.1"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
}
}

BIN
static/public/favicon.ico

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.8 KiB

40
static/public/index.html

@ -0,0 +1,40 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="theme-color" content="#000000">
<!--
manifest.json provides metadata used when your web app is added to the
homescreen on Android. See https://developers.google.com/web/fundamentals/engage-and-retain/web-app-manifest/
-->
<link rel="manifest" href="%PUBLIC_URL%/manifest.json">
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
<!--
Notice the use of %PUBLIC_URL% in the tags above.
It will be replaced with the URL of the `public` folder during the build.
Only files inside the `public` folder can be referenced from the HTML.
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>React App</title>
</head>
<body>
<noscript>
You need to enable JavaScript to run this app.
</noscript>
<div id="root"></div>
<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.
You can add webfonts, meta tags, or analytics to this file.
The build step will place the bundled scripts into the <body> tag.
To begin the development, run `npm start` or `yarn start`.
To create a production bundle, use `npm run build` or `yarn build`.
-->
</body>
</html>

15
static/public/manifest.json

@ -0,0 +1,15 @@
{
"short_name": "React App",
"name": "Create React App Sample",
"icons": [
{
"src": "favicon.ico",
"sizes": "64x64 32x32 24x24 16x16",
"type": "image/x-icon"
}
],
"start_url": "./index.html",
"display": "standalone",
"theme_color": "#000000",
"background_color": "#ffffff"
}

11
static/src/App/App.css

@ -0,0 +1,11 @@
@media only screen and (min-width: 768px) {
#rootContainer {
margin-top: 150px
}
}
@media only screen and (max-width: 767px) {
#rootContainer {
margin-top: 50px
}
}

70
static/src/App/App.js

@ -0,0 +1,70 @@
import React, { Component } from 'react'
import { Container, Input, Segment, Form, Modal, Button } from 'semantic-ui-react'
import './App.css';
class ContainerExampleContainer extends Component {
handleURLChange = (e, { value }) => this.url = value
handleURLSubmit() {
console.log(this.url)
}
componentWillMount() {
console.log("componentWillMount")
}
componentDidMount = () => {
console.log("componentDidMount")
}
state = {
open: true,
authorized: false
}
onOAuthClose = () => {
this.setState({ open: false })
}
onAuthClick = () => {
console.log("onAuthClick")
window.open("/api/v1/login", "", "width=600,height=400")
}
render() {
const { open, authorized } = this.state
if (authorized) {
return (
<Container id='rootContainer' >
<Segment raised>
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa strong. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede link mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet. Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi.</p>
<Form onSubmit={this.handleURLSubmit}>
<Form.Field>
<Input size='big' action={{ icon: 'arrow right' }} type='email' onChange={this.handleURLChange} name='url' placeholder='Enter your long URL here' />
</Form.Field>
</Form>
</Segment>
</Container>
)
} else {
return (
<Modal size="tiny" open={open} onClose={this.onOAuthClose}>
<Modal.Header>
OAuth2 Authentication
</Modal.Header>
<Modal.Content>
<p>Currently you are only able to use Google as authentification service:</p>
<div className="ui center aligned segment">
<Button className="ui google plus button" onClick={this.onAuthClick}>
<i className="google icon"></i>
Login with Google
</Button>
</div>
</Modal.Content>
</Modal>
)
}
}
}
export default ContainerExampleContainer;

8
static/src/App/App.test.js

@ -0,0 +1,8 @@
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
it('renders without crashing', () => {
const div = document.createElement('div');
ReactDOM.render(<App />, div);
});

5
static/src/index.css

@ -0,0 +1,5 @@
body {
margin: 0;
padding: 0;
font-family: sans-serif;
}

18
static/src/index.html

@ -0,0 +1,18 @@
<html>
<body>
<div id="App"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.0.1/react-with-addons.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.0.1/react-dom.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.7.7/babel.min.js"></script>
<script>
fetch("./index.js").then(function (response) {
response.text().then(function (js) {
eval(Babel.transform(js, { presets: ['es2015', 'react'] }).code);
});
});
</script>
</body>
</html>

9
static/src/index.js

@ -0,0 +1,9 @@
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import 'semantic-ui-css/semantic.min.css';
import App from './App/App';
import registerServiceWorker from './registerServiceWorker';
ReactDOM.render(<App />, document.getElementById('root'));
registerServiceWorker();

108
static/src/registerServiceWorker.js

@ -0,0 +1,108 @@
// In production, we register a service worker to serve assets from local cache.
// This lets the app load faster on subsequent visits in production, and gives
// it offline capabilities. However, it also means that developers (and users)
// will only see deployed updates on the "N+1" visit to a page, since previously
// cached resources are updated in the background.
// To learn more about the benefits of this model, read https://goo.gl/KwvDNy.
// This link also includes instructions on opting out of this behavior.
const isLocalhost = Boolean(
window.location.hostname === 'localhost' ||
// [::1] is the IPv6 localhost address.
window.location.hostname === '[::1]' ||
// 127.0.0.1/8 is considered localhost for IPv4.
window.location.hostname.match(
/^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/
)
);
export default function register() {
if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) {
// The URL constructor is available in all browsers that support SW.
const publicUrl = new URL(process.env.PUBLIC_URL, window.location);
if (publicUrl.origin !== window.location.origin) {
// Our service worker won't work if PUBLIC_URL is on a different origin
// from what our page is served on. This might happen if a CDN is used to
// serve assets; see https://github.com/facebookincubator/create-react-app/issues/2374
return;
}
window.addEventListener('load', () => {
const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`;
if (isLocalhost) {
// This is running on localhost. Lets check if a service worker still exists or not.
checkValidServiceWorker(swUrl);
} else {
// Is not local host. Just register service worker
registerValidSW(swUrl);
}
});
}
}
function registerValidSW(swUrl) {
navigator.serviceWorker
.register(swUrl)
.then(registration => {
registration.onupdatefound = () => {
const installingWorker = registration.installing;
installingWorker.onstatechange = () => {
if (installingWorker.state === 'installed') {
if (navigator.serviceWorker.controller) {
// At this point, the old content will have been purged and
// the fresh content will have been added to the cache.
// It's the perfect time to display a "New content is
// available; please refresh." message in your web app.
console.log('New content is available; please refresh.');
} else {
// At this point, everything has been precached.
// It's the perfect time to display a
// "Content is cached for offline use." message.
console.log('Content is cached for offline use.');
}
}
};
};
})
.catch(error => {
console.error('Error during service worker registration:', error);
});
}
function checkValidServiceWorker(swUrl) {
// Check if the service worker can be found. If it can't reload the page.
fetch(swUrl)
.then(response => {
// Ensure service worker exists, and that we really are getting a JS file.
if (
response.status === 404 ||
response.headers.get('content-type').indexOf('javascript') === -1
) {
// No service worker found. Probably a different app. Reload the page.
navigator.serviceWorker.ready.then(registration => {
registration.unregister().then(() => {
window.location.reload();
});
});
} else {
// Service worker found. Proceed as normal.
registerValidSW(swUrl);
}
})
.catch(() => {
console.log(
'No internet connection found. App is running in offline mode.'
);
});
}
export function unregister() {
if ('serviceWorker' in navigator) {
navigator.serviceWorker.ready.then(registration => {
registration.unregister();
});
}
}

6505
static/yarn.lock

File diff suppressed because it is too large

9
store/store.go

@ -3,12 +3,12 @@ package store
import ( import (
"encoding/json" "encoding/json"
"fmt"
"math/rand" "math/rand"
"time" "time"
"github.com/asaskevich/govalidator" "github.com/asaskevich/govalidator"
"github.com/boltdb/bolt" "github.com/boltdb/bolt"
"github.com/maxibanki/golang-url-shortener/config"
"github.com/pkg/errors" "github.com/pkg/errors"
) )
@ -41,8 +41,8 @@ var ErrGeneratingTriesFailed = errors.New("could not generate unique id, db full
var ErrIDIsEmpty = errors.New("id is empty") var ErrIDIsEmpty = errors.New("id is empty")
// New initializes the store with the db // New initializes the store with the db
func New(dbName string, idLength int) (*Store, error) { func New(storeConfig config.Store) (*Store, error) {
db, err := bolt.Open(dbName, 0644, &bolt.Options{Timeout: 1 * time.Second}) db, err := bolt.Open(storeConfig.DBPath, 0644, &bolt.Options{Timeout: 1 * time.Second})
if err != nil { if err != nil {
return nil, errors.Wrap(err, "could not open bolt DB database") return nil, errors.Wrap(err, "could not open bolt DB database")
} }
@ -56,7 +56,7 @@ func New(dbName string, idLength int) (*Store, error) {
} }
return &Store{ return &Store{
db: db, db: db,
idLength: idLength, idLength: storeConfig.ShortedIDLength,
bucketName: bucketName, bucketName: bucketName,
}, nil }, nil
} }
@ -121,7 +121,6 @@ func (s *Store) CreateEntry(URL, remoteAddr string) (string, error) {
for i := 1; i <= 10; i++ { for i := 1; i <= 10; i++ {
id, err := s.createEntry(URL, remoteAddr) id, err := s.createEntry(URL, remoteAddr)
if err != nil { if err != nil {
fmt.Println(err)
continue continue
} }
return id, nil return id, nil

20
store/store_test.go

@ -4,12 +4,19 @@ import (
"os" "os"
"strings" "strings"
"testing" "testing"
"github.com/maxibanki/golang-url-shortener/config"
) )
const ( const (
testingDBName = "test.db" testingDBName = "test.db"
) )
var validConfig = config.Store{
DBPath: testingDBName,
ShortedIDLength: 4,
}
func TestGenerateRandomString(t *testing.T) { func TestGenerateRandomString(t *testing.T) {
tt := []struct { tt := []struct {
name string name string
@ -34,13 +41,13 @@ func TestGenerateRandomString(t *testing.T) {
func TestNewStore(t *testing.T) { func TestNewStore(t *testing.T) {
t.Run("create store without file name provided", func(r *testing.T) { t.Run("create store without file name provided", func(r *testing.T) {
_, err := New("", 4) _, err := New(config.Store{})
if !strings.Contains(err.Error(), "could not open bolt DB database") { if !strings.Contains(err.Error(), "could not open bolt DB database") {
t.Fatalf("unexpected error: %v", err) t.Fatalf("unexpected error: %v", err)
} }
}) })
t.Run("create store with correct arguments", func(r *testing.T) { t.Run("create store with correct arguments", func(r *testing.T) {
store, err := New(testingDBName, 4) store, err := New(validConfig)
if err != nil { if err != nil {
t.Fatalf("unexpected error: %v", err) t.Fatalf("unexpected error: %v", err)
} }
@ -49,7 +56,10 @@ func TestNewStore(t *testing.T) {
} }
func TestCreateEntry(t *testing.T) { func TestCreateEntry(t *testing.T) {
store, err := New(testingDBName, 1) store, err := New(config.Store{
DBPath: testingDBName,
ShortedIDLength: 1,
})
if err != nil { if err != nil {
t.Fatalf("unexpected error: %v", err) t.Fatalf("unexpected error: %v", err)
} }
@ -67,7 +77,7 @@ func TestCreateEntry(t *testing.T) {
} }
func TestGetEntryByID(t *testing.T) { func TestGetEntryByID(t *testing.T) {
store, err := New(testingDBName, 1) store, err := New(validConfig)
if err != nil { if err != nil {
t.Fatalf("unexpected error: %v", err) t.Fatalf("unexpected error: %v", err)
} }
@ -83,7 +93,7 @@ func TestGetEntryByID(t *testing.T) {
} }
func TestIncreaseVisitCounter(t *testing.T) { func TestIncreaseVisitCounter(t *testing.T) {
store, err := New(testingDBName, 4) store, err := New(validConfig)
if err != nil { if err != nil {
t.Fatalf("could not create store: %v", err) t.Fatalf("could not create store: %v", err)
} }

Loading…
Cancel
Save