Github user alficles commented on a diff in the pull request:

    
https://github.com/apache/incubator-trafficcontrol/pull/729#discussion_r129993001
  
    --- Diff: traffic_ops/traffic_ops_golang/perlconfig.go ---
    @@ -0,0 +1,288 @@
    +package main
    +
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + *   http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing,
    + * software distributed under the License is distributed on an
    + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    + * KIND, either express or implied.  See the License for the
    + * specific language governing permissions and limitations
    + * under the License.
    + */
    +
    +import (
    +   "encoding/json"
    +   "fmt"
    +   "io/ioutil"
    +   "net/url"
    +   "regexp"
    +   "strconv"
    +   "strings"
    +
    +   
"github.com/apache/incubator-trafficcontrol/traffic_monitor_golang/common/log"
    +)
    +
    +const OldAccessLogPath = "/var/log/traffic_ops/access.log"
    +const NewLogPath = "/var/log/traffic_ops/traffic_ops_golang.log"
    +
    +func GetPerlConfigs(cdnConfPath string, dbConfPath string) (Config, error) 
{
    +   configBytes, err := ioutil.ReadFile(cdnConfPath)
    +   if err != nil {
    +           return Config{}, fmt.Errorf("reading CDN conf '%v': %v", 
cdnConfPath, err)
    +   }
    +   dbConfBytes, err := ioutil.ReadFile(dbConfPath)
    +   if err != nil {
    +           return Config{}, fmt.Errorf("reading db conf '%v': %v", 
dbConfPath, err)
    +   }
    +   return getPerlConfigsFromStrs(string(configBytes), string(dbConfBytes))
    +}
    +
    +func getPerlConfigsFromStrs(cdnConfBytes string, dbConfBytes string) 
(Config, error) {
    +   cfg, err := getCDNConf(cdnConfBytes)
    +   if err != nil {
    +           return Config{}, fmt.Errorf("parsing CDN conf '%v': %v", 
cdnConfBytes, err)
    +   }
    +
    +   dbconf, err := getDbConf(string(dbConfBytes))
    +   if err != nil {
    +           return Config{}, fmt.Errorf("parsing db conf '%v': %v", 
dbConfBytes, err)
    +   }
    +   cfg.DBUser = dbconf.User
    +   cfg.DBPass = dbconf.Password
    +   cfg.DBServer = dbconf.Hostname
    +   cfg.DBDB = dbconf.DBName
    +   cfg.DBSSL = false // TODO fix
    +   if dbconf.Port != "" {
    +           cfg.DBServer += ":" + dbconf.Port
    +   }
    +
    +   cfg.LogLocationInfo = OldAccessLogPath
    +   cfg.LogLocationError = NewLogPath
    +   cfg.LogLocationWarning = NewLogPath
    +   cfg.LogLocationEvent = NewLogPath
    +   cfg.LogLocationDebug = log.LogLocationNull
    +
    +   return cfg, nil
    +}
    +
    +func getCDNConf(s string) (Config, error) {
    +   cfg := Config{}
    +   obj, err := ParsePerlObj(s)
    +   if err != nil {
    +           return Config{}, fmt.Errorf("parsing Perl object: %v", err)
    +   }
    +
    +   if cfg.HTTPPort, err = getPort(obj); err != nil {
    +           return Config{}, err
    +   }
    +
    +   if cfg.TOSecret, err = getSecret(obj); err != nil {
    +           return Config{}, err
    +   }
    +
    +   oldPort, err := getOldPort(obj)
    +   if err != nil {
    +           return Config{}, err
    +   }
    +   cfg.TOURLStr = "https://127.0.0.1:"; + oldPort
    +   if cfg.TOURL, err = url.Parse(cfg.TOURLStr); err != nil {
    +           return Config{}, fmt.Errorf("Invalid Traffic Ops URL '%v': 
err", cfg.TOURL, err)
    +   }
    +
    +   cfg.CertPath, err = getConfigCert(obj)
    +   if err != nil {
    +           return Config{}, err
    +   }
    +
    +   cfg.KeyPath, err = getConfigKey(obj)
    +   if err != nil {
    +           return Config{}, err
    +   }
    +
    +   return cfg, nil
    +}
    +
    +func getPort(obj map[string]interface{}) (string, error) {
    +   portStrI, ok := obj["traffic_ops_golang_port"]
    +   if !ok {
    +           return "", fmt.Errorf("missing traffic_ops_golang_port key")
    +   }
    +   portStr, ok := portStrI.(string)
    +   if !ok {
    --- End diff --
    
    The reason Go uses strings for ports is to support things like "http" 
instead of "80". In general, you shouldn't use `strconv.Atoi` to convert port 
strings. Take a look at https://golang.org/pkg/net/#LookupPort


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

Reply via email to