Commit a1eeeb04 authored by Jürgen Enge's avatar Jürgen Enge
Browse files

initialization code

parent bb3358f3
# Default ignored files
/workspace.xml
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="JavaScriptSettings">
<option name="languageLevel" value="ES6" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/url-checker.iml" filepath="$PROJECT_DIR$/.idea/url-checker.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
package main
import (
"github.com/BurntSushi/toml"
"log"
"time"
)
type duration struct {
time.Duration
}
func (d *duration) UnmarshalText(text []byte) error {
var err error
d.Duration, err = time.ParseDuration(string(text))
return err
}
type CfgDBMySQL struct {
Dsn string
ConnMaxTimeout duration
Query string
}
type Config struct {
Logfile string
Loglevel string
CertPEM string
KeyPEM string
Addr string
JwtKey string
DB CfgDBMySQL
}
func LoadConfig(filepath string) Config {
var conf Config
_, err := toml.DecodeFile(filepath, &conf)
if err != nil {
log.Fatalln("Error on loading config: ", err)
}
return conf
}
package main
import (
"context"
"database/sql"
"flag"
_ "github.com/go-sql-driver/mysql"
"gitlab.switch.ch/memoriav/memobase-2020/url-checker/main/memocrawler"
"log"
"os"
"os/signal"
"path/filepath"
"syscall"
"time"
)
func main() {
configFile := flag.String("cfg", "./memocrawler.toml", "config file location")
flag.Parse()
var exPath = ""
// if configfile not found try path of executable as prefix
if !memocrawler.FileExists(*configFile) {
ex, err := os.Executable()
if err != nil {
panic(err)
}
exPath = filepath.Dir(ex)
if memocrawler.FileExists(filepath.Join(exPath, *configFile)) {
*configFile = filepath.Join(exPath, *configFile)
} else {
log.Fatalf("cannot find configuration file: %v", *configFile)
return
}
}
// configfile should exists at this place
var config Config
config = LoadConfig(*configFile)
// create logger instance
log, lf := memocrawler.CreateLogger("memostream", config.Logfile, config.Loglevel)
defer lf.Close()
db, err := sql.Open("mysql", config.DB.Dsn)
if err != nil {
// don't write dsn in error message due to password inside
log.Panicf("error connecting to database: %v", err)
return
}
defer db.Close()
if err := db.Ping(); err != nil {
log.Panicf("cannot ping database: %v", err)
return
}
db.SetConnMaxLifetime(time.Duration(config.DB.ConnMaxTimeout.Duration))
if err != nil {
// don't write dsn in error message due to password inside
log.Panicf("cannot initialize DBMySQL: %v", err)
return
}
defer db.Close()
cr := memocrawler.NewCrawler(db)
go cr.Runner()
end := make(chan bool, 1)
// process waiting for interrupt signal (TERM or KILL)
go func() {
sigint := make(chan os.Signal, 1)
// interrupt signal sent from terminal
signal.Notify(sigint, os.Interrupt)
signal.Notify(sigint, syscall.SIGTERM)
signal.Notify(sigint, syscall.SIGKILL)
<-sigint
// We received an interrupt signal, shut down.
log.Infof("shutdown requested")
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
cr.Shutdown(ctx)
end <- true
}()
<-end
log.Info("server stopped")
}
package memocrawler
import (
"context"
"database/sql"
)
type Crawler struct {
db *sql.DB
}
func NewCrawler(db *sql.DB ) *Crawler {
cr := &Crawler{db:db}
return cr
}
func (cr *Crawler) Runner() error {
return cr.crawl()
}
func (cr *Crawler) crawl() error {
return nil
}
func (cr *Crawler) Shutdown( ctx context.Context ) {
}
\ No newline at end of file
package memocrawler
import (
"github.com/op/go-logging"
"os"
)
var _logformat = logging.MustStringFormatter(
`%{time:2006-01-02T15:04:05.000} %{module}::%{shortfunc} [%{shortfile}] > %{level:.5s} - %{message}`,
)
func FileExists(filename string) bool {
info, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
}
return !info.IsDir()
}
func CreateLogger(module string, logfile string, loglevel string) (log *logging.Logger, lf *os.File) {
log = logging.MustGetLogger(module)
var err error
if logfile != "" {
lf, err = os.OpenFile(logfile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Errorf("Cannot open logfile %v: %v", logfile, err)
}
//defer lf.CloseInternal()
} else {
lf = os.Stderr
}
backend := logging.NewLogBackend(lf, "", 0)
backendLeveled := logging.AddModuleLevel(backend)
backendLeveled.SetLevel(logging.GetLevel(loglevel), "")
logging.SetFormatter(_logformat)
logging.SetBackend(backendLeveled)
return
}
logfile = "" # log file location
loglevel = "DEBUG" # CRITICAL|ERROR|WARNING|NOTICE|INFO|DEBUG
accesslog = "" # http access log file
addr = "localhost:81"
certpem = "" # tls client certificate file in PEM format
keypem = "" # tls client key file in PEM format
jwtkey = "swordfish"
[DB]
#if dsn is empty, the static resolver will be used
#[username[:password]@][protocol[(address)]]/dbname[?param1=value1&...&paramN=valueN]
dsn = "test:HDNQiaqNqu4IjmUPATJr@tcp(localhost:3306)/test"
#dsn = ""
# should be smaller than server connection timeout to allow controlled reconnect
connMaxTimeout = "4h"
# query has to return the fields uri, access and protocol. One parameter
query = "SELECT uri, access, proto AS protocol FROM test.test2 WHERE sig = ?"
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment