lytics / confl

Config parser for go, modeled after Nginx format, Nice lenient syntax with Comments
MIT License
138 stars 20 forks source link

the style of nginx's configuration, how to deal it? #10

Open lemonl2 opened 6 years ago

lemonl2 commented 6 years ago

location ~* .(otf|eot|woff|ttf|woff2)$ { types {font/opentype otf;} types {application/vnd.ms-fontobject eot;} types {font/truetype ttf;} types {application/font-woff woff;} types {font/woff2 woff2;} }

location / { try_files $uri $uri/ /index.html =404; expires off; add_header Cache-Control "no-cache, no-store, must-revalidate"; add_header Strict-Transport-Security "max-age=86400" always; }

Just like nginx's configuration, how do i deal it ?

araddon commented 6 years ago

Its definitely not 100% nginx config but only inspired by.

This would be closest i can think of:

package main

import (
    "fmt"
    "log"

    "github.com/lytics/confl"
)

type Location struct {
    Location  string   `confl:"location"`
    TryFiles  string   `confl:"try_files"`
    Expires   string   `confl:"expires"`
    AddHeader []string `confl:"add_header"`
    Types     []string `confl:"types"`
}

type Locations struct {
    Location []Location
}

func main() {
    var rawData = `
    location [
        {
            location   : "~* .(otf|eot|woff|ttf|woff2)$"
            types : [ 
                 "{font/opentype otf;}" ,
                 "types {application/vnd.ms-fontobject eot;}" ,
           ]
        }
        {
            location  "/"
            try_files  "$uri $uri/ /index.html =404"
            expires off
            add_header : [
                'Cache-Control "no-cache, no-store, must-revalidate"',
                'Strict-Transport-Security "max-age=86400" always',
            ]
        }
    ]
    `

    var locs Locations
    if _, err := confl.Decode(rawData, &locs); err != nil {
        log.Fatal(err)
    }

    for _, l := range locs.Location {
        fmt.Printf("%+v\n", l)
    }
}
lemonl2 commented 6 years ago

@araddon Thanks! maybe it can't deal more complex configuration just like nginx.conf, I should seek another program to deal it!

mingmingshiliyu commented 1 year ago

@araddon Thanks! maybe it can't deal more complex configuration just like nginx.conf, I should seek another program to deal it!

have you found a better one?