Browse Source

Switched configuration to Yaml

dependabot/npm_and_yarn/web/prismjs-1.21.0
Max Schmitt 8 years ago
parent
commit
7dbad1e262
  1. 3
      README.md
  2. 3
      config.yml
  3. 64
      main.go
  4. 2
      store/store.go

3
README.md

@ -116,9 +116,10 @@ For that you need to send a field `ID` to the backend.
Next changes sorted by priority Next changes sorted by priority
- [x] Fix handler unit tests - [x] Fix handler unit tests
- [ ] Switch configuration to TOML - [x] Switch configuration to Yaml
- [ ] Add Authorization (oAuth e.g. Google) - [ ] Add Authorization (oAuth e.g. Google)
- [ ] Add Deletion functionality (depends on the authorization) - [ ] Add Deletion functionality (depends on the authorization)
- [ ] 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
- [ ] Provide image on the docker hub - [ ] Provide image on the docker hub

3
config.yml

@ -0,0 +1,3 @@
DBPath: main.db
ListenAddr: :8080
ShortedIDLength: 4

64
main.go

@ -1,49 +1,63 @@
package main package main
import ( import (
"io/ioutil"
"log" "log"
"os" "os"
"os/signal" "os/signal"
"strconv" "path/filepath"
"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"
yaml "gopkg.in/yaml.v2"
) )
func main() { func main() {
dbPath := "main.db" stop := make(chan os.Signal, 1)
listenAddr := ":8080" signal.Notify(stop, os.Interrupt)
idLength := 4 close, err := initShortener()
if os.Getenv("SHORTENER_DB_PATH") != "" { if err != nil {
dbPath = os.Getenv("SHORTENER_DB_PATH") log.Fatalf("could not init shortener: %v", err)
} }
if os.Getenv("SHORTENER_LISTEN_ADDR") != "" { <-stop
listenAddr = os.Getenv("SHORTENER_LISTEN_ADDR") log.Println("Shutting down...")
close()
}
func initShortener() (func(), error) {
var config struct {
DBPath string `yaml:"DBPath"`
ListenAddr string `yaml:"ListenAddr"`
ShortedIDLength int `yaml:"ShortedIDLength"`
} }
if os.Getenv("SHORTENER_ID_LENGTH") != "" { ex, err := os.Executable()
var err error if err != nil {
idLength, err = strconv.Atoi(os.Getenv("SHORTENER_ID_LENGTH")) return nil, errors.Wrap(err, "could not get executable path")
if err != nil {
log.Fatalf("could not parse shortener ID length: %v", err)
}
} }
stop := make(chan os.Signal, 1) file, err := ioutil.ReadFile(filepath.Join(filepath.Dir(ex), "config.yml"))
signal.Notify(stop, os.Interrupt) if err != nil {
store, err := store.New(dbPath, idLength) return nil, errors.Wrap(err, "could not read configuration file: %v")
}
err = yaml.Unmarshal(file, &config)
if err != nil { if err != nil {
log.Fatalf("could not create store: %v", err) return nil, errors.Wrap(err, "could not unmarshal yaml file")
} }
handler := handlers.New(listenAddr, *store) store, err := store.New(config.DBPath, config.ShortedIDLength)
if err != nil {
return nil, errors.Wrap(err, "could not create store")
}
handler := handlers.New(config.ListenAddr, *store)
go func() { go func() {
err := handler.Listen() err := handler.Listen()
if err != nil { if err != nil {
log.Fatalf("could not listen to http handlers: %v", err) log.Fatalf("could not listen to http handlers: %v", err)
} }
}() }()
<-stop return func() {
log.Println("Shutting down...") err = handler.CloseStore()
err = handler.CloseStore() if err != nil {
if err != nil { log.Printf("failed to stop the handlers: %v", err)
log.Printf("failed to stop the handlers: %v", err) }
} }, nil
} }

2
store/store.go

@ -3,6 +3,7 @@ package store
import ( import (
"encoding/json" "encoding/json"
"fmt"
"math/rand" "math/rand"
"time" "time"
@ -120,6 +121,7 @@ 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

Loading…
Cancel
Save