2018년 1월 18일 목요일

[GoLaNg] xml parse2

1. XML File 내용
<?xml version="1.0" encoding="UTF-8" ?>
<properties>
    <propertie key="HelloMsg"><![CDATA[Hello World!!]]></propertie>
    <propertie key="TestMsg"><![CDATA[Test!!]]></propertie>
</properties>


2. 프로그램
import (
    "encoding/xml"
    "fmt"
    "io/ioutil"
    "os"
)

/* XML  파싱 하기 위한 구조체 */
type Properties struct {
    Propertie []Propertie `xml:"propertie"`
}

/* XML  파싱 하기 위한 구조체 */
type Propertie struct {
    Key   string `xml:"key,attr"`
    Value string `xml:",cdata"`
}

/* XML 파일로 부터 프로퍼티를 읽어 반환 */
func GetPropFromXML(key string) string {

    fp, err := os.Open("conf/properties2.xml")
    if err != nil {
        panic(err)
    }
    defer fp.Close()

    // xml 파일 읽기
    data, err := ioutil.ReadAll(fp)

    prop := &Properties{}
    xmlErr := xml.Unmarshal(data, prop)
    if xmlErr != nil {
        fmt.Println(err)
    }
    fmt.Println(prop)
    fmt.Println(len(prop.Propertie))
    var result string
    for i := 0; i < len(prop.Propertie); i++ {
        if prop.Propertie[i].Key == key {
            result = prop.Propertie[i].Value
        }
        //        println("---> " + prop.Propertie[i].Key + "\t -> " + prop.Propertie[i].Value)
    }
    return result

}

2018년 1월 16일 화요일

[GoLaNg] sleep

import (
    "fmt"
    "time"
)

func init() {
    fmt.Println("2 초후 프로그램이 시작 합니다.")
}


func main() {
    time.Sleep(time.Second * 2)
    fmt.Println("Hello World!!")
}

[GoLaNg] 시스템 변수 출력 및 기록

import (
"os"
"fmt"
)

/* 시스템 변수를 출력 한다. */
func GetEnvironments() {
for index, env := range os.Environ() {
fmt.Println(index, env)
}
}

/* 시스템 변수를 반환 한다. */
func ReadSysEnv(env string) string {
return os.Getenv(env)
}

/* 시스템 변수를 기록 한다. */
func WriteSysEnv(env string, val string) {
os.Setenv(env, val)
}

[GoLaNg] 특정 디렉토리 목록 출력

import (
"fmt"
"io/ioutil"
"os"
"strconv"
)

/* 특정 디렉토리의 목록을 출력 한다. */
func DirectoryList(dir string) {
files, err := ioutil.ReadDir(dir)
if err != nil {
fmt.Println(err)
}

for _, f := range files {
if f.IsDir() {
fmt.Println(f.Name() + "\t" + "디렉토리")
} else {
fmt.Println(f.Name() + "\t" + "파일" + "\t" + strconv.FormatInt(f.Size(), 10) + "Byte")
}
}
}

[GoLaNg] xml parse

1. XML File 내용
<?xml version="1.0" encoding="UTF-8" ?>
<properties>
    <propertie key="HelloMsg">
        <value><![CDATA[Hello World!!]]></value>
    </propertie>
    <propertie key="TestMsg">
        <value><![CDATA[Test!!]]></value>
    </propertie>
</properties>


2. 프로그램
import (
    "encoding/xml"
    "fmt"
    "io/ioutil"
    "os"

)

/* XML 을 파싱 하기 위한 구조체 */
type Properties struct {
Propertie []Propertie `xml:"propertie"`
}

/* XML 을 파싱 하기 위한 구조체 */
type Propertie struct {
Key   string `xml:"key,attr"`
Value string `xml:"value"`
}

/* XML 파일로 부터 프로퍼티를 읽어 반환 */
func GetPropFromXML(key string) string {
    fp, err := os.Open("freecatz.pe.kr/Conf/properties.xml")
    if err != nil {
        panic(err)
    }
    defer fp.Close()

    // xml 파일 읽기
    data, err := ioutil.ReadAll(fp)

    // xml 디코딩
    var properties Properties
    xmlerr := xml.Unmarshal(data, &properties)
    if xmlerr != nil {
        panic(xmlerr)
    }

    var result string
    for i := 0; i < len(properties.Propertie); i++ {
        if properties.Propertie[i].Key == key {
            result = properties.Propertie[i].Value
        }
        //        fmt.Println("key: " + properties.Propertie[i].Key + "\t -> " + properties.Propertie[i].Value)
    }
    return result
}

[GoLaNg] 파일이 있는지 확인

/* 파일이 있는지 확인 한다. */
func FileExist(filePath string) bool {
    var result bool = false
    if _, err := os.Stat(filePath); os.IsNotExist(err) {
        fmt.Println("파일 없음")
        result = false
    } else {
        fmt.Println("파일 있음")
        result = true
    }
    return result

}

2018년 1월 12일 금요일

[EtC] spring-security login - jquery ajax 405 error


1. 기존 Spring Boot 프로젝트에 html5 부터 지원 되는 cache 기능을 적용
<html lang="ko" manifest="/default.manifest" xmlns:th="http://www.thymeleaf.org">

2. jquery 의 ajax 를 이용하여 spring-security 를 통해 로그인 시도

3. 405 Method Not Allowed 발생

4. cache 대상 파일에서 jquery-1.12.4.min.js 를 제외 하거나,
   spring-security 로그인 페이지에서 manifest="/default.manifest" 를 제거
      : jquery-1.12.4.min.js 를 disk cache 에서 읽어 오는 경우 문제가 발생
       크롬의 시크릿 모드에서는 정상적으로 동작

5. 기존과 같이 정상적으로 spring-security 를 통해 로그인

6. CORS Filter 를 적용 하면 된다는 이야기가 있어 기존 CORS Filter 에 추가해 보았으나 안됨

7. 실력이 개차반이라 아직까지 왜 그런지 이유는 모름

8. 어찌 되었던 해결