4 changed files with 46 additions and 26 deletions
@ -0,0 +1,3 @@ |
|||||
|
DBPath: main.db |
||||
|
ListenAddr: :8080 |
||||
|
ShortedIDLength: 4 |
||||
@ -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 |
||||
} |
} |
||||
|
|||||
Loading…
Reference in new issue